ES · Leer en español Explore for free

Recipe · Intermediate · 15 min

Claude Code for free with Ollama or OpenRouter: what works

How to connect Claude Code to Ollama or OpenRouter with ANTHROPIC_BASE_URL, what breaks with non-Claude models, and the legal low-cost option.

Tested with Claude Code 2.1.316 · reviewed

The problem

You want to try Claude Code without paying, and you read online that it "works for free with Ollama". That's half true: the claude program can connect to other servers, but then the one answering is no longer Claude. It's an open model running on your PC or on a third-party service. Here's what each party's official documentation says, how to set it up step by step, and where it breaks.

Honest note: this setup is verified against the official Claude Code, Ollama and OpenRouter documentation (Oct 31, 2026); it was not tested with Ollama on this machine.

The solution

1. Understand what changes and what doesn't

Claude Code talks to the Anthropic API. The ANTHROPIC_BASE_URL variable tells it to send requests to another server that "speaks" the same format (the Messages API, /v1/messages). Both of these do today:

OllamaOpenRouter
Where the model runsOn your PC (or on Ollama Cloud)On the servers of the providers OpenRouter picks
Anthropic API compatibility"A subset" of the Messages APICalled the "Anthropic Skin", compatible with the Messages API
CostFree locally (you pay with your hardware):free models with a daily cap; the rest with credits
Is it Claude?No, neverOnly if you pick an Anthropic model (and then you pay)

The Claude Code documentation is clear: Anthropic "doesn't endorse, maintain, or audit third-party gateway products, and doesn't support routing Claude Code to non-Claude models through any gateway". It works, but if something fails, you're on your own.

2. Option A: Ollama on your PC

Install Ollama from ollama.com and pull a model that supports tools. Ollama's official guide uses qwen3.5 as its example:

ollama pull qwen3.5

The easiest way is to let Ollama launch Claude Code already configured:

ollama launch claude

If you'd rather do it by hand, export these variables (they're the ones in Ollama's official guide) and start with the model:

export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_API_KEY=""
export ANTHROPIC_BASE_URL=http://localhost:11434
claude --model qwen3.5

ANTHROPIC_AUTH_TOKEN is required but Ollama ignores it; an empty ANTHROPIC_API_KEY stops Claude Code from using an Anthropic key you may have saved.

Raise the context. Ollama defaults to a 4k context if your GPU has less than 24 GiB of VRAM, and that's not enough for an agent. Its documentation asks for at least 64000 for coding tools:

OLLAMA_CONTEXT_LENGTH=64000 ollama serve

More context uses more memory: with 8 GB of RAM and no strong GPU, a useful local model will be very slow or won't fit.

Optional: so that background tasks (session titles and the like use the haiku alias) and the sonnet/opus aliases don't ask for a model Ollama doesn't have, point them at the same local model:

export ANTHROPIC_DEFAULT_HAIKU_MODEL=qwen3.5
export ANTHROPIC_DEFAULT_SONNET_MODEL=qwen3.5
export ANTHROPIC_DEFAULT_OPUS_MODEL=qwen3.5

3. Option B: OpenRouter

Create an account at openrouter.ai, generate a key and pick a model from its catalog with the :free suffix that supports tools. The variables come from OpenRouter's official Claude Code guide:

export OPENROUTER_API_KEY="your-openrouter-key"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY=""
export ANTHROPIC_DEFAULT_SONNET_MODEL="<model>:free"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="<model>:free"
claude

Replace <model>:free with the exact ID you copy from the catalog. ANTHROPIC_API_KEY must be empty on purpose: according to OpenRouter, this keeps Claude Code from authenticating directly against Anthropic.

Free model limits according to OpenRouter: 20 requests per minute and 50 per day if you've bought fewer than 10 credits; 1000 per day if you've bought 10 or more. Claude Code makes several requests per task (every read, edit or command is a turn), so 50 run out fast.

4. Save the configuration without leaking the key

To avoid exporting everything in each terminal, put the variables in the env block of ~/.claude/settings.json (all your projects) or .claude/settings.local.json (one project only):

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://openrouter.ai/api",
    "ANTHROPIC_AUTH_TOKEN": "your-openrouter-key",
    "ANTHROPIC_API_KEY": ""
  }
}

Never put it in the project's .claude/settings.json: that file gets committed and everyone who clones the repo sees it. If you create settings.local.json by hand, add it to .gitignore first.

5. Go back to Claude whenever you want

Remove the variables and Claude Code goes back to your claude.ai login, which stays saved:

unset ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN ANTHROPIC_API_KEY
unset ANTHROPIC_DEFAULT_HAIKU_MODEL ANTHROPIC_DEFAULT_SONNET_MODEL ANTHROPIC_DEFAULT_OPUS_MODEL

If you put them in a settings.json, delete them there too.

Check it

Before opening Claude Code, test the server with a minimal request (it's the test from the Claude Code documentation, with your URL):

curl -sS -w '\n%{http_code}\n' -X POST "$ANTHROPIC_BASE_URL/v1/messages" \
  -H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model": "qwen3.5", "max_tokens": 16, "messages": [{"role": "user", "content": "hello"}]}'

Replace qwen3.5 with your OpenRouter model if you use that option. A JSON response with a "content" field and the 200 code at the end means it works; a 401 means the key is wrong.

Then open claude from the same terminal and type /status. You should see something like this (OpenRouter example):

Auth token: ANTHROPIC_AUTH_TOKEN
Anthropic base URL: https://openrouter.ai/api

If the Anthropic base URL line doesn't show up, the variable didn't reach the session: open a new terminal and launch claude again.

Why it works

Claude Code sends each turn as a request to $ANTHROPIC_BASE_URL/v1/messages, with the credential from ANTHROPIC_AUTH_TOKEN in the Authorization: Bearer header. Ollama and OpenRouter accept that same format and translate the request for their model. Claude Code's parts (tools, permissions, CLAUDE.md, hooks) stay the same; what changes is the "brain" making the decisions.

That's why not everything fits. According to Ollama's compatibility table, it doesn't support token counting (/v1/messages/count_tokens), prompt caching (cache_control), PDFs or tool_choice; its Claude Code guide warns that "hosted WebSearch and advanced tool controls are not fully supported". OpenRouter, for its part, warns in its guide that Claude Code "is optimized for Anthropic models and may not work correctly with other providers".

Official Claude Code sources: Other LLM gateways, Connect to a gateway and Environment variables.

Common mistakes

FAQ

So Claude Code is free with Ollama?

The program is, and so is the local model, but you're no longer using Claude. You get Claude Code's interface and tools with an open model, usually a lot less capable at long coding tasks.

Can I use Claude (Anthropic's model) for free through OpenRouter?

No. OpenRouter also offers Anthropic's models, but you pay for them with credits at per-token prices. What's free on OpenRouter are other vendors' models with the :free suffix and a daily cap.

Does my code leave my PC?

With Ollama running locally (http://localhost:11434), model requests don't leave your machine; Claude Code can still send nonessential traffic (updates, telemetry), which you turn off with CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1. With OpenRouter your code travels to OpenRouter and to the model provider; OpenRouter says it doesn't store prompts unless you turn that on, and doesn't route to providers that log or train on data unless you allow it in its privacy tab.

What's the cheapest legal way to use real Claude?

Claude Code works with a Pro, Max or Ultra subscription (the free claude.ai plan isn't among the supported accounts) or with a Claude Console key, paying per use. With an API key you pay only for what you use: pick a small model (/model haiku) and check the session's spend with /usage. Compare with the plan prices at claude.com/pricing.

Is ollama launch claude better than setting it up by hand?

ollama launch claude sets the variables for you and lets you pick the model; it's the fastest way to try it. By hand you get full control and can save it in settings.json, but remember to remove the variables to go back to Claude.

All recipes