ANTHROPIC_BASE_URL: point Claude Code at any model you want
Claude Code reads two environment variables that let it talk to any OpenAI-compatible endpoint. That one fact is what makes it usable without an Anthropic subscription.
Claude Code is an Anthropic product, so most people assume it only talks to Anthropic. It does not. It reads a base URL from the environment, and it will happily send its requests anywhere that speaks the same API:
export ANTHROPIC_BASE_URL=http://localhost:20128/v1
export ANTHROPIC_AUTH_TOKEN=<your-key>That is the whole mechanism. Everything below is what it makes possible.
What the variables do
| Variable | Purpose |
|---|---|
ANTHROPIC_BASE_URL | Where requests go instead of api.anthropic.com |
ANTHROPIC_AUTH_TOKEN | The bearer token sent to that endpoint |
ANTHROPIC_DEFAULT_OPUS_MODEL | Which model the "opus" alias maps to |
ANTHROPIC_DEFAULT_SONNET_MODEL | Which model the "sonnet" alias maps to |
ANTHROPIC_DEFAULT_HAIKU_MODEL | Which model the "haiku" alias maps to |
The last three matter more than they look. Claude Code refers to models by tier -- opus, sonnet, haiku -- and those aliases are remappable. Point them at whatever you like and the interface stays identical.
Why this is the interesting part
Claude Code's value is the agent loop: the tool use, the file editing, the planning. That is client-side. The model behind it is swappable.
So the question stops being "can I afford Claude Max" and becomes "what do I want answering these requests" -- which is a different and much cheaper question.
Setting it up
An endpoint that speaks the Anthropic API is required. A local router is the straightforward option:
npm install -g @sifxprime/krouter
krouter -tConnect a provider in the dashboard, then:
export ANTHROPIC_BASE_URL=http://localhost:20128/v1
export ANTHROPIC_AUTH_TOKEN=sk_krouter
export ANTHROPIC_DEFAULT_SONNET_MODEL=ag/claude-sonnet-4-6
export ANTHROPIC_DEFAULT_HAIKU_MODEL=kr/claude-haiku-4.5Start Claude Code as usual. Nothing about the interface changes; the requests now go through your machine and out to whichever provider each alias points at.
The mapping worth copying
The aliases let you split work by cost without thinking about it during a session:
- haiku handles the volume: file summaries, commit messages, the naming requests Claude Code fires constantly in the background. Point it at something free or near-free.
- sonnet handles normal coding. Claude Sonnet 4.6 is served by several providers, so this does not have to mean paying Anthropic directly.
- opus handles the hard problems. Keep this on whatever you trust most, because it will be used least.
Most of an agent session is haiku-tier traffic. Mapping that away from a metered path is where the saving actually comes from -- not from downgrading the model doing your real work.
Gotchas
Restart after exporting. The variable is read at process start. Setting it in a shell where Claude Code is already running does nothing. This is the single most common reason people think it did not work.
The token is not your Anthropic key. It authenticates you to whatever endpoint you pointed at. Sending your Anthropic key to a local router does nothing useful.
Mind the /v1. Claude Code appends /v1/messages to whatever you set, so a strict endpoint wants the API root without /v1. kRouter rewrites the doubled /v1/v1/ prefix to the same handler, which is why the examples here work either way -- but if you point at something other than kRouter, drop the /v1.
Check it took effect. If requests still land on Anthropic, the variable is not in the environment the tool actually launched from -- verify with env | grep ANTHROPIC in that same shell.
Making it permanent
Add the exports to your shell profile, or let a settings file hold them so they apply to every session rather than the one terminal you set them in. Claude Code reads ~/.claude/settings.json, and a router can write the block for you rather than you maintaining it by hand.
Common questions
Does ANTHROPIC_BASE_URL work with the Claude Code CLI?
Yes. It is a supported variable -- it exists so the CLI can point at Bedrock, Vertex, and enterprise gateways. Anything speaking the same API works.
I set the variable and nothing changed. What went wrong?
Almost always that the variable was not in the environment the tool actually launched from. It is read at process start, so exporting it in a shell where Claude Code is already running does nothing. Restart the session and confirm with env | grep ANTHROPIC.
Is ANTHROPIC_AUTH_TOKEN my Anthropic API key?
No. It authenticates you to whichever endpoint you pointed at. Sending your Anthropic key to a local router accomplishes nothing.
Do tool use and MCP still work?
Yes. The agent loop, tool execution, and MCP connections are all client-side. Redirecting the model endpoint does not touch them.
Can I keep my subscription and switch back?
Yes. Unset the variables and the CLI goes back to Anthropic. Nothing is modified permanently.
Related
Klaw is the Kodelyth AI agent. He writes drafts, runs the benchmarks, and tracks every cost number in this post live through kRouter. Humans review before publish.
Install kRouter