How to Route Claude Code Through an LLM Router (Without Breaking Subagents)
How to Route Claude Code Through an LLM Router (Without Breaking Subagents) If you point Claude Code at an LLM router and only set ANTHROPIC_BASE_URL, the main loop often works, but background tasks and subagents ask f
How to Route Claude Code Through an LLM Router (Without Breaking Subagents)
If you point Claude Code at an LLM router and only set ANTHROPIC_BASE_URL, the main loop often works, but background tasks and subagents ask for a model name the gateway does not know, and fail with "model not found".
Claude Code can send its requests to an LLM router through the ANTHROPIC_BASE_URL variable. That one variable is what makes routers possible. This guide walks through a complete setup and explains each line so you can adapt it to whatever gateway you use.
What a router changes (and what it doesn't)
A coding session is not one request. Even a modest task β "add OAuth login and update the tests" β becomes a sequence of steps: understand the repository, design the flow, read configuration, edit the service, run tests, diagnose a failure, verify. Those steps do not all need the same intelligence.
A coding-agent router sits between Claude Code and the model providers and decides, per request, which model should handle it.
Full disclosure: we make AirLLM. Air Router, our routing layer, is used in the examples. Air Router gives every step the model it needs. Once an active task requires a stronger model tier, Air Router avoids moving that task to a weaker tier (Air Router).
Your commands, slash commands, permissions and workflow stay the same. What changes is where the model requests go. Claude Code still runs the agent loop and the tools on your machine. The router only answers the model calls.
Choosing airllm-auto or airllm-intelligent
Before you change any settings, choose a routing alias.
-
airllm-intelligentβ use airllm-intelligent to route between appropriate Claude tiers without leaving the Claude model family. -
airllm-autoβ the default for coding tools and agents. Picks a model per request across providers.
Either way, use your AirLLM API key as ANTHROPIC_AUTH_TOKEN. To use Air Router, set ANTHROPIC_BASE_URL to https://api.airllm.ai/, set ANTHROPIC_AUTH_TOKEN to your AirLLM API key, and set the model lines to a routing model. Requests are billed to your AirLLM account. Don't set only the base URL: AirLLM never needs Claude Code plan credentials.
Step 1: Get a key
Sign in to the AirLLM dashboard and create an API key under API Keys. (If you are using another gateway, get its key and base URL.) The Free plan is $0 / month. No credit card required to start (pricing).
The quickest route is the AirLLM setup script, which writes these settings to ~/.claude/settings.json for you. It merges into your existing config without overwriting other settings; back up those files first if you want a copy. Safe to re-run: it updates its own entries instead of adding duplicates. Signed-in admins can skip the editing: API Keys β Quick Setup downloads a copy with your key, model and tool already inside. Downloading the setup script from API Keys issues a new key, and the previous one stops working. The rest of this guide shows the same settings by hand, so you can see what each line does.
Step 2: Add the variables to your shell profile
Check that Claude Code is installed:
claude --version
Open ~/.zshrc (or ~/.bashrc on most Linux systems) and add:
export ANTHROPIC_BASE_URL="https://api.airllm.ai/"
export ANTHROPIC_AUTH_TOKEN="your_airllm_api_key"
export ANTHROPIC_MODEL="airllm-auto"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="airllm-auto"
export ANTHROPIC_DEFAULT_SONNET_MODEL="airllm-auto"
export ANTHROPIC_DEFAULT_OPUS_MODEL="airllm-auto"
export ANTHROPIC_DEFAULT_FABLE_MODEL="airllm-auto"
export CLAUDE_CODE_SUBAGENT_MODEL="airllm-auto"
export CLAUDE_CODE_SUBAGENT_MODEL_FORCE="1"
export API_TIMEOUT_MS="3000000"
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC="1"
Line by line:
-
ANTHROPIC_BASE_URLβ set it tohttps://api.airllm.ai/. Claude Code appends/v1/messagesitself. Do not add a trailing/v1; that is a common source of 404s. -
ANTHROPIC_AUTH_TOKENβ your AirLLM (or other gateway) API key. -
ANTHROPIC_MODELβ the model for the main loop.airllm-autolets the router pick per request; useairllm-intelligentto stay on Claude models. -
The four
ANTHROPIC_DEFAULT_*_MODELlines andCLAUDE_CODE_SUBAGENT_MODELβ these are the ones people forget. Claude Code's background tasks and subagents use separate model settings. If the gateway does not recognise those names, those requests fail with "model not found". Mapping every tier to the same routing alias fixes that (Developer Guide). -
CLAUDE_CODE_SUBAGENT_MODEL_FORCE,API_TIMEOUT_MS, andCLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFICβ included as shown in the official setup snippet.
Reload and start:
source ~/.zshrc
echo $ANTHROPIC_BASE_URL
claude
Windows
The Developer Guide documents the same variables as user-level environment variables. In PowerShell:
[Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://api.airllm.ai/", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", "your_airllm_api_key", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_MODEL", "airllm-auto", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_DEFAULT_HAIKU_MODEL", "airllm-auto", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_DEFAULT_SONNET_MODEL", "airllm-auto", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_DEFAULT_OPUS_MODEL", "airllm-auto", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_DEFAULT_FABLE_MODEL", "airllm-auto", "User")
[Environment]::SetEnvironmentVariable("CLAUDE_CODE_SUBAGENT_MODEL", "airllm-auto", "User")
[Environment]::SetEnvironmentVariable("CLAUDE_CODE_SUBAGENT_MODEL_FORCE", "1", "User")
[Environment]::SetEnvironmentVariable("API_TIMEOUT_MS", "3000000", "User")
[Environment]::SetEnvironmentVariable("CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", "1", "User")
Open a new PowerShell window, then:
echo $env:ANTHROPIC_BASE_URL
claude
If you are running Claude Code inside WSL, set the variables inside WSL with the macOS/Linux steps instead.
Step 3: Pick a routing alias
| Alias | What it routes between | When to use it |
|---|---|---|
airllm-auto |
Default for coding tools and agents. Picks a model per request across providers. | You want the router to choose freely across providers |
airllm-intelligent |
Routes each coding step between appropriate Claude tiers, without leaving the Claude model family | You want to stay on Claude models |
To use airllm-intelligent, replace airllm-auto in every model line of the snippet. Other routing aliases (airllm-advanced, airllm-agent) are documented in the Developer Guide; airllm-coding and airllm-thinking are chat aliases, not for coding agents.
Step 4: Check that it works
Send a small, verifiable prompt from Claude Code:
Write a simple Java method to validate an email address.
Then open your AirLLM dashboard: the request should appear in your usage logs. The setup is working when the request shows up there with the expected route and model, and Claude Code gets a response. If nothing shows up, Claude Code is still talking to another endpoint, usually because the shell was not reloaded or a different terminal is open.
If you want to test the key outside Claude Code first:
curl https://api.airllm.ai/v1/chat/completions \
-H "Authorization: Bearer your_airllm_api_key" \
-H "Content-Type: application/json" \
-d '{"model":"airllm-auto","messages":[{"role":"user","content":"Write a simple Java method to validate an email address."}]}'
Note: when you set a custom ANTHROPIC_BASE_URL, Claude Code turns MCP tool search off by default (set ENABLE_TOOL_SEARCH=true to turn it back on) and disables Remote Control.
Troubleshooting checklist
-
"model not found" from a subagent β one of the model lines still names a model the gateway does not recognise. Set all five extra model lines (the four
ANTHROPIC_DEFAULT_*_MODELvariables andCLAUDE_CODE_SUBAGENT_MODEL) to the sameairllm-*alias. -
No request in your AirLLM logs, or Claude Code still seems to use your subscription β confirm the variables are exported in the shell you launched from (
echo $ANTHROPIC_BASE_URLorecho $env:ANTHROPIC_BASE_URL). Starting from an IDE terminal that did not source your profile is a common cause; writing~/.claude/settings.jsonavoids it. Also check Windows vs WSL. -
404 on every request β the base URL is wrong. Claude Code takes
https://api.airllm.ai/without/v1. -
401 Unauthorized β the key is missing or wrong. Check
ANTHROPIC_AUTH_TOKEN. -
A model you named returns a message saying it is disabled by your organization β an admin has blocked it in Model Governance. On AirLLM this comes back as a normal 200 reply, in your tool's own format, whose text is:
The model '<id>' is disabled by your organization. Check with your administrator to enable it.The tool shows it instead of retrying.
Cost habits that still matter
A router is not a substitute for good session hygiene. Run /clear between unrelated tasks and /compact with a focus in long sessions. Keep CLAUDE.md short and turn off unused MCP servers.
Avoid switching models by hand in the middle of a long session β the new model cannot reuse the previous model's prompt cache. As Air Router puts it: The Lowest-Priced Model Isn't Always the Cheapest Request. (A follow-up article covers the arithmetic.)
Our guide on how to reduce Claude Code costs goes into more detail.
Try it
- Developer Guide, including every supported tool: https://airllm.ai/developer-doc?utm_source=devto&utm_medium=article&utm_campaign=claude-code-router
- Claude Code router page: https://airllm.ai/claude-code-router?utm_source=devto&utm_medium=article&utm_campaign=claude-code-router
- Start free (No credit card required to start): https://airllm.ai/pricing?utm_source=devto&utm_medium=article&utm_campaign=claude-code-router
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.