Guides
Claude Code
Run Claude Code on AZRouter with your AZRouter key, on any paid usage tier.
Tested: Claude Code 2.1.302, 2026-10-19
Claude Code talks to /v1/messages, so it runs on AZRouter with an AZRouter key in place of an Anthropic login.
Set the base URL and your key: Claude Code's own model names work, and AZRouter fits each request's output cap to your usage tier, balance and key limit.
1. Install
npm install -g @anthropic-ai/claude-code2. Set the variables
The base URL has no /v1: Claude Code adds /v1/messages itself.
export ANTHROPIC_BASE_URL="https://api.azrouter.dev"
export ANTHROPIC_AUTH_TOKEN="$AZROUTER_API_KEY"
3. Check it
claude -p "Reply with exactly: Hello from AZRouter"Then start claude in your project as usual.
Run /status in a session: the Anthropic base URL line shows https://api.azrouter.dev, and the auth line names ANTHROPIC_AUTH_TOKEN.
Claude Code starts in auto mode unless you set another: see Safety for Manual mode, which asks before it acts and makes no extra paid requests.
Claude Code needs credits: on an account without them, every request gets a 402. Add credits to start, on any paid usage tier.
Which key variable
AZRouter accepts the key in either header, so either variable works.
Set one, not both.
| Variable | Header Claude Code sends | Notes |
|---|---|---|
ANTHROPIC_AUTH_TOKEN | Authorization: Bearer <key> | Used at once. The examples on this page use it. |
ANTHROPIC_API_KEY | x-api-key: <key> | In an interactive session, Claude Code asks once whether to use the key. Choose Yes: the answer it preselects is No. |
Either variable takes precedence over a saved claude.ai login, which stays saved and unused.
To remove the login, run /logout.
Keep the settings in a file
Shell exports last for one terminal.
To use AZRouter in every session, put the same variables in the env block of ~/.claude/settings.json.
A value in the settings file wins over a shell export.
{
"env": {
"ANTHROPIC_BASE_URL": "https://api.azrouter.dev",
"ANTHROPIC_AUTH_TOKEN": "sk-your-azrouter-key"
},
"permissions": {
"defaultMode": "default"
}
}"defaultMode": "default"starts every session in Manual mode, which asks before each edit and command: see Safety. The first time, Claude Code offers to make auto mode your default. Choose No, keep manual mode: the preselected Yes writes"defaultMode": "auto"into this file.- To keep the key out of the file, delete the
ANTHROPIC_AUTH_TOKENline and add"apiKeyHelper": "printenv AZROUTER_API_KEY"at the top level. Claude Code then reads the key from your environment. - Never put the key in a project's
.claude/settings.json. That file is committed with the repository.
Pick a model
Claude Code's own model names are AZRouter's Claude models: claude-opus-5-5 is anthropic/, with or without [1m], a date or -latest, and a bare family name such as claude-sonnet-5 is the newest of that family on sale.
Claude Code starts on Claude Opus 5.5; --model sonnet, --model haiku and /model switch as usual.
Any AZRouter text model works in ANTHROPIC_MODEL too, by its slug.
These run Claude Code's tools (reading and editing files, running commands):
| Model | Slug |
|---|---|
| Claude Haiku 5.5 | anthropic/ |
| Claude Sonnet 5.5 | anthropic/ |
| Claude Opus 5.5 | anthropic/ |
| GLM 5.3 | z-ai/ |
| Kimi K3 | moonshotai/ |
| GPT-6 Luna | openai/ |
| Qwen3.8 Flash | qwen/ |
- Claude Opus 5.5 sets aside the most credit per request, and more as a session grows, since every request resends the conversation so far. A short session runs on a $5 balance; a long one, with requests near 1 MB, needs about $10. Below that a request gets a 402 while part of the balance is still there: add credits, or run
/clearto start a new session. With $1, a session gets a few Opus turns. - GLM 5.3 ran a full tool-using task in 4 seconds in our test.
- Kimi K3's reasoning counts toward the output cap.
- Qwen3.8 Flash can't be made to call one specific tool (a forced
tool_choice). Claude Code's own requests don't force one.
Prices and context windows are on each model's page in the catalog.
To switch to a model by slug inside a session, run /model with it, for example /model z-ai/glm-5.3.
Claude Code's opus, sonnet and haiku names can point at other AZRouter models, so /model sonnet and --model sonnet pick them:
export ANTHROPIC_DEFAULT_OPUS_MODEL="anthropic/claude-opus-5.5"
export ANTHROPIC_DEFAULT_SONNET_MODEL="z-ai/glm-5.3"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="anthropic/claude-haiku-5.5"Limits on your tier
Your usage tier sets how many requests and tokens your account can use a minute and a day.
Claude Code feels them more than a single API call does:
- Output cap. Claude Code asks for up to 128,000 output tokens per request. AZRouter lowers that to what your tier's output tokens left this minute, your balance and the key's limit allow, half of what each has left, so parallel requests each get a share. The
x-azrouter-max-tokensresponse header says what the model got. A long answer can stop at that cap; ask Claude Code to continue. You pay for the tokens the model writes, not for the cap. - Requests per minute. At its busiest, with subagents and auto mode's reviews, Claude Code sends dozens of requests a minute. When your tier's limit is reached, a request gets a 429 with a
Retry-Afterwait; Claude Code waits and retries by itself, so you see a pause, not an error. A higher tier pauses less. - Input per minute. Cache reads of a prompt your key already cached don't count, so a long session doesn't use up the limit with the prompt it resends. After 5 idle minutes the cache has expired, and the whole prompt counts once.
- Key spending limit. Before a request runs, AZRouter sets aside its worst case against your balance and the key's spending limit. With little room left, the output cap is lowered; under 4,096 tokens the request gets a 402 instead.
Prompt caching
Claude Code marks its system prompt and tools for caching, so you pay full input price for them once and cache reads after that.
- On Claude models the cache lasts 5 minutes. A request for a one-hour cache is served and billed as a 5-minute cache write. After 5 idle minutes, the next request writes the cache again.
- The first request of a session on Claude Haiku 5.5 wrote 38,457 tokens to the cache in our test, about $0.005.
See Prompt caching for prices.
Known limits
- Approximate context meter. AZRouter has no
/v1/messages/count_tokens, and Claude Code doesn't need it: it estimates token counts itself, so/contextis approximate. Billing uses the counts the model reports. - No thinking blocks from Claude models. A Claude model answers, but Claude Code shows no thinking for it.
- Model slugs. Claude Code doesn't know AZRouter's slugs such as
z-ai/. It prints "Claude Code doesn't recognize the model … assumes a context window of 200k tokens" and carries on; its model configuration docs (opens in a new tab) show how to set the real window.glm-5.3 - The cost Claude Code prints. Claude Code prices requests with its own table, not AZRouter's prices. The real cost of each request is on your Activity page.
Troubleshooting
Claude Code prints the API's error after "API Error:", and the sentence after the status says why.
Before a 401 or 403 it puts "Failed to authenticate" in claude -p and "Please run /login ·" in an interactive session; both are about your AZRouter key, not a claude.ai login.
| What you see | Cause | Fix |
|---|---|---|
| "There's an issue with the selected model (…). It may not exist or you may not have access to it." | AZRouter answered 404: the model name is misspelled, or that model isn't on sale | Pick another with /model; the catalog lists what is on sale. |
| A pause with "Retrying in …", then the answer | 429: your tier's per-minute limit; Claude Code waits for Retry-After | Nothing to fix. A higher tier pauses less. |
| 429 "This request alone is over your usage tier's limit …" | One request is larger than your tier takes in a minute, so waiting won't help | Run /clear to start a new session (/compact resends the long conversation), or move to a higher tier. |
| 402 "This request requires more credits, or fewer max_tokens. …" | Your balance can't cover the request, or the account has no credits yet | Add credits. A long Claude Opus 5.5 session needs about $10; /clear starts a new, shorter one. |
| 402 "Key limit exceeded (total limit). …" | The key reached its own spending limit | Raise or remove the key's limit on the Keys page. |
"Failed to authenticate. API Error: 401 Invalid or expired API key." (-p), or "Please run /login · API Error: 401 Invalid or expired API key." (interactive) | The key is wrong, revoked or expired | Copy the key again, or create a new one. |
"Failed to authenticate. API Error: 403 This model is not enabled for this API key yet. …" (-p), or "Please run /login · API Error: 403 This model is not enabled …" (interactive) | The key's model list hasn't caught up with the catalog | Retry in a minute. |
| 502 "The model provider could not serve this request; your API key and credits are not the cause." | The provider behind the model failed | Claude Code retries by itself. If it keeps failing, try another model, or try later. |
| "Auto mode could not evaluate this action and is blocking it for safety" | Auto mode's review request failed, for example on credits or the key's limit: each review sets aside about $0.60 | Keep more than $1 in your balance and in the key's limit, or switch to Manual mode with Shift+Tab: see Safety. |
| "Invalid API key · Please run /login", or a warning that two credentials are set | A saved claude.ai login or an old Anthropic key is still active | Keep only the AZRouter key; run /logout to drop the saved login. |
To tell AZRouter apart from your Claude Code setup, send one request with curl from the same shell.
A JSON reply that starts with {"id":"msg_ means the URL and key work:
curl -sS "$ANTHROPIC_BASE_URL/v1/messages" \
-H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "anthropic/claude-haiku-5.5",
"max_tokens": 16,
"messages": [{"role": "user", "content": "Reply with exactly: OK"}]
}'Safety
Review what Claude Code runs
Claude Code runs commands and edits files on your machine.
Any model, from any provider, can return a wrong or harmful command.
- Use Manual mode. Claude Code 2.1.283 and later starts in auto mode, which runs the actions a classifier model rates as low-risk without asking you. Manual mode asks before every file edit and command. To start every session in it, add
"permissions": {"defaultMode": "default"}to~/.claude/settings.json, as in Keep the settings in a file; for one session, runclaude --permission-mode default. The status line then shows⏸ manual mode on, and Shift+Tab switches modes inside a session. acceptEditsmode approves file edits and common file commands (mkdir,touch,mv,cp,rm,sed) inside your project without asking. Don't usebypassPermissionsmode or the--dangerously-skip-permissionsflag.- Turn on Claude Code's sandbox (opens in a new tab) to limit what shell commands can reach. It covers shell commands only, so run risky work in a dev container (opens in a new tab) or a virtual machine.
- Keep secrets such as
.envfiles and SSH keys out of the folders Claude Code can read. - Read each command and change before you approve it. See Claude Code's permission modes (opens in a new tab) and security (opens in a new tab) pages.
Auto mode on AZRouter
Auto mode reviews an action by sending a separate request through your base URL: Claude Code's reviewer instructions, about 40,000 tokens marked for caching, plus your conversation so far, to claude-sonnet-5, which is Claude Sonnet 5.5 on AZRouter.
- Each review is billed to your key at Claude Sonnet 5.5's prices, like any other request. Reviews within 5 minutes of each other read the instructions from the cache. Manual mode makes no reviews.
- The first time a review runs, Claude Code says the session "isn't eligible" for its free classifier requests because they go through a gateway, and that they "are billed as before". Press Enter: auto mode keeps working, and the reviews are billed to your key. AZRouter doesn't offer the free review; with
CLAUDE_CODE_AUTO_MODE_SERVER=0set, Claude Code doesn't ask for it and skips the notice. - Each review sets aside about $0.60 of your balance and of the key's limit while it runs, so auto mode needs more than $1 in both. Below that the review is refused and the action is blocked: see Troubleshooting. With $1 or less in either, use Manual mode, or top up and raise the key's limit above $1.
FAQ
- Do I need an Anthropic account or /login?
No.
Your AZRouter key is the only credential.
If Claude Code opens on its login screen, the key variable didn't reach it: check
/status, or put the variables in~/.claude/settings.json.- ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY?
Either.
AZRouter reads both headers.
ANTHROPIC_AUTH_TOKENis used at once;ANTHROPIC_API_KEYasks for approval once in an interactive session.See Which key variable.
- Do I need ANTHROPIC_MODEL or CLAUDE_CODE_MAX_OUTPUT_TOKENS?
No.
Claude Code's own model names work, and AZRouter fits the output cap to your tier.
Both still work if you set them:
ANTHROPIC_MODELto run another model, and an output cap lower than the one AZRouter would give.- Can Claude Code use models that aren't Claude?
Yes.
Set
ANTHROPIC_MODELto any AZRouter text model, for examplez-ai/.glm-5.3 See Pick a model.
- Why is the cost Claude Code shows different from my bill?
Claude Code prices requests with its own table.
Your bill uses AZRouter's prices; each request's cost is on the Activity page.