0 readers reading
How to Use Any OpenRouter Model in Claude Code: The GLM 5.2 Guide

How to Use Any OpenRouter Model in Claude Code: The GLM 5.2 Guide

Learn how to configure Claude Code to use OpenRouter models like GLM 5.2. Unlock a 1M context window for project-level coding without Anthropic billing lock-in.

Sham

Sham

AI Engineer & Founder, The Tech Archive

10 min read
0 views

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:

  1. Massive context. A 1M-token window lets you load an entire large project at once.
  2. 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.
  3. 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:

  1. 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.
  2. Cost Management: Use free-tier models (like Qwen 3.5 Flash or DeepSeek V4) for boilerplate and documentation tasks.
  3. 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.

  1. Head to OpenRouter.ai.
  2. Create a new API key (starts with sk-or-v1-).
  3. Ensure you have a small credit balance (e.g., $5) to access premium models, or use :free suffixed 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's ANTHROPIC_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.

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.

Sources
Updates & Corrections
  • 2026-08-07: Consolidated the separate GLM 5.2 setup guide into this one. Added the harness-versus-brain framing, the direct Z.ai route with both its Anthropic-compatible and OpenAI-compatible endpoints, the local Ollama path with real hardware numbers, the High/Max reasoning-effort control, the GLM Coding Plan price range, the frontier comparison table, the Obsidian persistent-memory layer, and four FAQ entries. claude-code-glm-5-2-setup-guide now redirects here.
  • 2026-08-07: Conflict resolved in favour of the primary source. The merged article's comparison table came from the LMMarketCap aggregator and gave Opus 4.8 84.4 on Terminal-Bench, 65.2 on SWE-bench Pro and a 500K context, with GPT-5.5 at 82.1 / 64.8 / 256K. Anthropic's and OpenAI's own pages, plus Z.ai's benchmark blog, give 85.0 / 69.2 / 1M for Opus 4.8 and 84.0 / 58.6 / 1.05M for GPT-5.5. The vendor figures are used; the aggregator's are recorded here and not presented as fact. Both sources agree on GLM 5.2's own scores (81.0 and 62.1).
  • 2026-06-24: Initial guide published; verified environment variable overrides in Claude Code v2.1.174.
  • 2026-06-30: GLM 5.2 integration re-verified after the June 13 Z.ai release.
  • 2026-06-21: Added independent benchmark scores from SWE-bench Pro.

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 →

Get the practical AI brief

Verified, no-hype AI tips you can actually use - in your inbox. Free.

No spam. We verify what we send. Unsubscribe anytime.

Discussion

0 comments