How to Run Claude Code Without Paying for Anthropic's API

AI

Claude Code is one of the best terminal agents out there. Using it isn't the problem — the bill is, if you don't have a Claude subscription and don't want to put a card on Anthropic's API.

There's a clean way out: Claude Code speaks one protocol (the Anthropic Messages API), and anything that speaks it can sit underneath as the engine. Swap Anthropic for a free model, a cheaper provider, or a local model on your laptop — without touching Claude Code itself.

The short path: 4 environment variables

Claude Code reads the endpoint and key from environment variables. Point them somewhere else:

``` export ANTHROPIC_BASE_URL="https://your-gateway" export ANTHROPIC_AUTH_TOKEN="your-gateway-key" export ANTHROPIC_MODEL="full-model-id" export ANTHROPIC_DEFAULT_HAIKU_MODEL="full-fast-model-id" ```

With that, every Claude Code call goes wherever you send it — OpenRouter, DeepSeek, Gemini, Kimi K3, whatever you want. The `sonnet` alias only works behind gateways that proxy Anthropic; on other models, use the full ID.

The router path: `claude-code-router`

If you want to switch models per task, `claude-code-router` (by `musistudio`, 36k GitHub stars, MIT, now on v3) is a local gateway that intercepts Claude Code's requests and re-routes them to OpenAI, Gemini, DeepSeek, OpenRouter, Mistral, Moonshot and more. It runs on your machine, it's free, and the rules are yours: a strong model to write code, a cheap one for the rest.

Local model, zero accounts

Since January 2026 Ollama serves the Anthropic Messages API directly. Install Ollama, pull a model, point `ANTHROPIC_BASE_URL` at `localhost` — and Claude Code runs offline, with no key at all. LM Studio does the same.

The catch — what nobody puts in the caption

Three things, and they're real:

  1. Weak model, weak agent. Claude Code leans hard on tool calling — editing files, running commands. The model's benchmark score matters less than its tool-use quality. Frontier models hold up; weak ones fumble edits and shell commands, and you spend more time fixing than producing.
  2. It stops being "free" in the subscription sense. The moment you activate a gateway, sessions are billed as API usage by that backend. Your Claude Pro/Max subscription doesn't apply there. "Without paying Anthropic's API" is not the same as "without paying anything" — unless the model is local.
  3. Your code passes through a third party. A cloud gateway sees everything Claude Code reads and writes. Before you route proprietary code through it, verify where the gateway runs and what it retains.

When it's worth it

  • Learning, experimenting, personal projects: router + cheap model, or local Ollama. Worth it now.
  • Work code, on deadlines: a frontier model via API usually costs less in lost time than a weak "free" one.
  • Sensitive code: local model, or nothing.

Want the calm version of AI news like this, once a week? Subscribe to the Sharp AI Hub newsletter →