Claude Code
Route Claude Code through AnyRouter for unified billing, audit logs, and a single API key that reaches every model — including GLM-5.2 with your own Z-AI key.
Point Claude Code at AnyRouter and every session runs through one gateway: unified billing, request audit logs, and a single sk-ar- key that reaches every model AnyRouter serves. It's a config-only change — no code, no rebuild.
Why route through AnyRouter
- Drop-in. Zero code changes. The fastest path is one command; the manual path is two environment variables.
- One key. A single AnyRouter API key unlocks every Anthropic model and more.
- Audit logs. Every request lands in your dashboard with cost, latency, and token usage.
- Swap models freely. Run Claude Code on Anthropic models, or on GLM-5.2 with your own Z-AI key (BYOK).
This is the narrative "use Claude Code with AnyRouter" guide. For the full command reference — flags, profiles, --dry-run, and the login flow — see the canonical Claude Code CLI setup.
Set it up
One command handles everything:
curl -fsSL https://anyrouter.dev/setup.sh | bash
anyr claude
The first run sets you up once: paste an sk-ar- key, or log in at anyrouter.dev (a key is generated for you). Your config is saved and reused, and the same key also powers anyr codex.
Full options (--model, --effort, profiles, --dry-run) are in the CLI reference.
Claude Code reads ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY from the environment. Point them at AnyRouter:
export ANTHROPIC_BASE_URL="https://anyrouter.dev/api"
export ANTHROPIC_API_KEY="sk-ar-your-anyrouter-key"
Create the key in your dashboard first — every AnyRouter key starts with sk-ar-. Drop these two lines into ~/.bashrc or ~/.zshrc so they persist across shells, then run claude as usual.
Prefer to keep a separate Anthropic key for other work? Put the env vars in a wrapper script instead of your shell rc — see the wrapper recipe in the CLI reference.
GLM-5.2 with your own Z-AI key (BYOK)
Prefer Z-AI's GLM-5.2 as your coding agent? Bring your own Z-AI key and AnyRouter routes Claude Code to it — you pay Z-AI directly (or use your Z-AI Coding Plan subscription), and AnyRouter adds nothing to the per-token cost.
GLM-5.2 has a 1M-token context, a 128K output window, and controllable reasoning effort, so it's a strong drop-in for long-horizon coding sessions.
Step 1 — Add your Z-AI key
Open Settings → BYOK and paste your Z-AI API key. If you have a Z-AI Coding Plan, paste that key — AnyRouter routes it through the Coding Plan endpoint automatically. Your key is encrypted at rest and only used to sign your own requests.
Step 2 — Point Claude Code at GLM-5.2
The CLI is the simplest path — it picks up your AnyRouter key and just takes a model:
anyr claude --model "z-ai/glm-5.2"
Prefer the manual env-var route? Keep the same two vars as above, then select the model per session:
export ANTHROPIC_BASE_URL="https://anyrouter.dev/api"
export ANTHROPIC_API_KEY="sk-ar-your-anyrouter-key"
# Interactive session on GLM-5.2
claude --model "z-ai/glm-5.2" "Refactor this module and add tests"
Reasoning effort
GLM-5.2 supports controllable thinking. AnyRouter maps the standard reasoning_effort levels onto Z-AI's thinking control: low/medium/high/max enable thinking, while none/minimal disable it. Use Claude Code's /effort command (or the CLI --effort flag) to set it per session:
anyr claude --model "z-ai/glm-5.2" --effort high "Plan a migration"
1M context
To use GLM-5.2's full 1M-token window in Claude Code, raise the auto-compact threshold so Claude Code keeps the long context instead of summarizing early:
export CLAUDE_CODE_AUTO_COMPACT_WINDOW=1000000
Verify
Run any prompt, then confirm the request routed through AnyRouter:
anyr claude "Explain the architecture of this project"
Open your request logs — the call should appear within a second or two, with the model name, token counts, and cost. If it's there, you're routing through AnyRouter.
Troubleshooting
401 or 403 — confirm the key is active in your dashboard and that ANTHROPIC_API_KEY (or your saved CLI config) holds a valid sk-ar- key.
Model not found — make sure the model id exactly matches an AnyRouter id; browse Messages-compatible models. For GLM-5.2 via BYOK, confirm your Z-AI key is saved under Settings → BYOK.
Long agent runs time out — bump the SDK timeout for long-horizon flows: export API_TIMEOUT_MS=3000000.
Related
- Claude Code CLI setup — canonical command reference
- Grok Bot, Herdr, and Anyr CLI — fleet coding with Herdr spaces
- Codex
- OpenCode
- Supported models
- Messages API