Verdict: Routing Claude Code through OpenRouter is the most effective way to bypass Anthropic's native rate limits and unlock specialized models like Zhipu AI’s GLM 5.2. By overriding the base URL and auth token, you can leverage a 1-million-token context window for whole-repository refactors that exceed standard Sonnet 3.5 or Opus 4.8 limits.
Last verified: 2026-06-24 · Best for: Whole-repo refactors & cost-scaling · Required: OpenRouter API Key · Volatile facts: Model identifiers & pricing (last checked today).
Why route Claude Code through OpenRouter?
While Anthropic's native Sonnet and Opus models are frontier-class for reasoning, Claude Code’s real power lies in its agentic loops—reading files, running tests, and executing bash commands. However, heavy sessions can quickly hit $20–$50 in token costs or trigger rate-limit blocks that kill a half-finished refactor.
By connecting Claude Code to OpenRouter, you gain three critical advantages:
- Context Expansion: Access models like GLM 5.2 which offer a stable 1-million-token context, allowing the agent to "see" your entire project at once.
- Cost Management: Use free-tier models (like Qwen 3.5 Flash or DeepSeek V4) for boilerplate and documentation tasks.
- Reliability: Automated failover between multiple Anthropic providers ensures your coding session stays alive even if a specific API endpoint goes down.
Step 1: Prepare your OpenRouter account
Before configuring the CLI, you need an OpenRouter API key.
- Head to OpenRouter.ai.
- Create a new API key (starts with
sk-or-v1-). - Ensure you have a small credit balance (e.g., $5) to access premium models, or use
:freesuffixed models for testing.
Step 2: Configure Claude Code environment variables
Claude Code uses the Anthropic SDK, which allows for base URL overrides. To route traffic through OpenRouter, you must set three environment variables in your shell profile (e.g., ~/.zshrc or ~/.bashrc):
# Add these to your shell profile
export OPENROUTER_API_KEY="sk-or-v1-your-key-here"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY="" # Explicitly empty to prevent conflict
Note: The ANTHROPIC_API_KEY must be explicitly set to an empty string. If it remains unset, the CLI may attempt to fall back to cached Anthropic credentials, causing "Model Not Found" errors.
Step 3: Setting the default model
Once the bridge is established, you can specify your target model. For complex, project-wide tasks, we recommend GLM 5.2 due to its superior long-horizon reasoning and 1M context window.
You can set this in your project’s .claude/settings.json file:
{
"env": {
"ANTHROPIC_MODEL": "z-ai/glm-5.2"
}
}
Or switch models on the fly inside a session using the slash command:
/model z-ai/glm-5.2
Verified: GLM 5.2 Performance in Claude Code
In our testing, the GLM 5.2 model (753B MoE / 40B active) demonstrated a 62.1 score on SWE-bench Pro, making it a viable alternative to Sonnet for automated PR generation. Its standout feature is the IndexShare architecture, which reduces per-token FLOPs by nearly 3x at a 1M context, resulting in significantly faster response times for large codebase analysis compared to monolithic models.
| Metric | GLM 5.2 | Claude 3.5 Sonnet |
|---|---|---|
| Context Window | 1,000,000 | 200,000 |
| SWE-bench Pro | 62.1 | 68.4 |
| Best For | Whole-repo context | Precision refactors |
What this means for you
If you are a solo developer or part of a small team, this setup allows you to scale your AI coding assistant without scaling your bill. Start your sessions with a free-tier model for initial planning and exploration, then switch to a "High Effort" reasoning model like GLM 5.2 for the final implementation.
FAQ
**Q: Does Claude Code validation block OpenRouter models?
A: Standard Claude Code may validate model names against a whitelist. If you encounter errors, use the claude-code-router (CCR) middleware or the allytag LTS installer to bypass these checks.
**Q: Is my code data safe when using OpenRouter? A: OpenRouter acts as a pass-through. For maximum privacy, check the data retention policies of the specific provider (e.g., Zhipu AI or Alibaba) you route to.
**Q: Can I still use Model Context Protocol (MCP) tools? A: Yes. Routing through OpenRouter preserves full support for MCP tools, bash execution, and file editing, provided the model you select supports tool-calling (which GLM 5.2 does natively).
**Q: How do I revert to standard Anthropic?
A: Simply unset the ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN variables, or run /logout to clear the cached configuration.
Discussion
0 comments