AnyRouter

Claude Code with AnyRouter — free models, one API key

Run Claude Code through AnyRouter — set ANTHROPIC_BASE_URL, use free models with anyrouter/free, or any model with one sk-ar- 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.

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

Free models and auto routing

Set the model to an AnyRouter router id instead of a fixed model:

Model idWhat it does
anyrouter/freeRoutes to the best free model available right now, with automatic fallback. No credits used.
anyrouter/autoPicks from top models (free and paid) with health-aware failover. Paid members use credits.
anyr claude --model anyrouter/free
# or, with the manual env vars set:
claude --model anyrouter/free

anyrouter/free needs no provider keys. It has no daily cap on any plan. Details: Free Tier.

If anyrouter/auto or anyrouter/free returns 400 no_eligible_models, your privacy or routing settings (for example Zero Data Retention or blocked providers) excluded every candidate — relax them in the dashboard or pick a specific model.

Bring your own keys

Already pay for a provider? Add its key in AnyRouter and Claude Code uses it through the same base URL. BYOK requests are billed by your provider. See Use your own provider keys and BYOK.

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.