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 — at roughly a tenth of the cost of native frontier usage.
Last verified: 2026-06-30 · Best for: Whole-repo refactors & cost-scaling · Required: OpenRouter API key (or a Z.ai key for the direct route) · Volatile facts: Model identifiers & pricing.
The "harness" versus the "brain"
In the early days of AI, a tool and its model were inseparable. In 2026 the industry has shifted toward agentic harnesses, and that separation is the whole trick here.
Claude Code is a world-class harness — it manages terminal sessions, file edits and multi-step planning. You do not have to use Anthropic's brain to run it. Swap in Zhipu AI's GLM 5.2 and you gain:
- Massive context. A 1M-token window lets you load an entire large project at once.
- Economic autonomy. GLM 5.2's MIT-licensed weights mean you can self-host, or use Z.ai's API at a fraction of frontier-tier prices.
- Reasoning flexibility. GLM 5.2's High and Max effort modes let you scale compute to task complexity instead of paying full price for trivial edits.
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
Step 4 (optional): skip OpenRouter and go direct to Z.ai
OpenRouter buys you failover and a single billing relationship across many models. If you only want GLM 5.2, you can point the harness straight at Z.ai instead. Two endpoints exist and they are not interchangeable:
- Anthropic-compatible:
https://api.z.ai/api/anthropic— use this with Claude Code'sANTHROPIC_BASE_URL. - OpenAI-compatible:
https://api.z.ai/v1— use this with tools that speak the OpenAI format.
claude-code --model "glm-5.2" --base-url "https://api.z.ai/v1" --api-key "YOUR_ZAI_KEY"
You will want an active GLM Coding Plan (Lite, Pro or Max) on the Z.ai side.
Or run it locally
ollama run glm-5.2:latest
Then use http://localhost:11434/v1 as the base URL. Check your hardware first: the model's MoE architecture activates roughly 40B parameters per token out of a much larger total, which in practice means a high-end workstation or cluster in the 80–160 GB VRAM range for quantized serving, and something closer to 4× A100 or 4× H100 for full precision.
Step 5: select the reasoning effort
GLM 5.2 exposes High and Max effort modes. For complex app builds, set Max:
claude-code set-config reasoning_effort=max
The point is not to run everything at Max. Keep routine edits on High and spend the extra compute where the task actually earns it.
Verified: GLM 5.2 Performance in Claude Code
In our testing, the GLM 5.2 model 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 |
Against the current frontier models, using Z.ai's and the vendors' own published figures:
| Metric | GLM 5.2 | Claude Opus 4.8 | GPT-5.5 |
|---|---|---|---|
| Terminal-Bench 2.1 | 81.0 | 85.0 | 84.0 |
| SWE-bench Pro | 62.1 | 69.2 | 58.6 |
| Context window | 1,000,000 | 1,000,000 | 1,050,000 |
| License | MIT (open weights) | Proprietary | Proprietary |
Opus 4.8 keeps an edge on complex abstract reasoning, but GLM 5.2's agent and tool-use performance sits within a few points of the frontier — more than enough for day-to-day coding, refactoring and agentic workflows. For the full head-to-head, see GLM 5.2 vs Claude Opus 4.8 for coding.
Advanced: a persistent memory layer with Obsidian
One of the more useful ways to run this setup is to wire it to persistent memory. With Obsidian in the loop, your GLM-powered Claude Code instance can log every build and decision, pull your private coding standards out of your own notes, and keep one memory layer shared across agents (Hermes, Claude and GLM).
For the full setup, see our guide on Agent OS and Obsidian orchestration.
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.
At the $10–$80/month range of the GLM Coding Plan, the token anxiety that comes with pointing a frontier model at a large refactor mostly goes away. You can afford to let an agent scan the whole repo, find bugs and propose architectural changes without worrying about a $500 API bill.
Recommendation: use Opus 4.8 for high-level architectural decisions, then switch to GLM 5.2 inside Claude Code for the heavy lifting of implementation and testing.
Related reading
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: Is GLM 5.2 safe for commercial use? A: Yes. It was released under the MIT license on June 13, 2026, which allows full commercial use, modification and private self-hosting.
Q: How does the 1M context window handle "needle in a haystack" tasks? A: Per Z.ai's June 16 developer documentation, the model uses a revised attention structure that avoids the degradation usually seen in ultra-long sequences, retrieving 99.8% of information placed randomly in a 1M-token block. That is a vendor-reported figure.
Q: What are the hardware requirements for self-hosting? A: The MoE architecture activates only ~40B parameters per token, which lets it run on high-end consumer clusters or AI workstations with roughly 80–160 GB of VRAM for quantized weights. Full precision wants multi-GPU server hardware.
Q: Can I use other models with this harness? A: Yes, the Claude Code harness is increasingly model-agnostic. You can plug in local models like Qwable 5 27B for private, offline work.
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.
Every claim here is traced to a primary source, dated, and listed under Sources. Research and drafting are AI-assisted; editing, verification and publication are human decisions, and a person is accountable for what appears on this page. How we work →

Discussion
0 comments