Claude Code with OpenRouter or Ollama

How to Use Claude Code With OpenRouter or Ollama (2026 Setup Guide)

How we wrote this: every command and variable below comes from the official Claude Code, OpenRouter and Ollama documentation, checked on 25 November 2026. We did not benchmark models inside Claude Code, so we do not rank them. More on our method at how we test.

Short answer

Claude Code sends its requests to whatever address is in ANTHROPIC_BASE_URL. For OpenRouter, set it to https://openrouter.ai/api, put your OpenRouter key in ANTHROPIC_AUTH_TOKEN and set ANTHROPIC_API_KEY to an empty string. For Ollama, run ollama launch claude, or set the base URL to http://localhost:11434 with the token ollama. The honest catch: Anthropic says it does not support Claude Code on non-Claude models, and OpenRouter only guarantees it with Anthropic’s own provider. Free and local models can work, but expect weaker tool use than with Claude.

Key facts

ItemOpenRouterOllama (local)
ANTHROPIC_BASE_URLhttps://openrouter.ai/apihttp://localhost:11434
ANTHROPIC_AUTH_TOKENyour OpenRouter API keyollama (not checked locally)
ANTHROPIC_API_KEY"" (must be empty)""
One-command setupNoollama launch claude
Officially guaranteed modelsAnthropic first-party provider onlyModels that support tools; 64k+ context advised
CostPay per token; :free models capped at 50 requests a day (1,000 a day after buying 10 credits)Free on your own hardware; Ollama cloud models from $0 pay-as-you-go
Anthropic support for non-Claude modelsNoNo

How Claude Code picks its endpoint and model

Claude Code reads these environment variables (from the Claude Code docs):

VariableWhat it does
ANTHROPIC_BASE_URLSends requests to a proxy or gateway instead of Anthropic
ANTHROPIC_AUTH_TOKENSent as Authorization: Bearer <value>
ANTHROPIC_API_KEYSent as X-Api-Key; leave it empty when using a gateway token
ANTHROPIC_MODELThe model to use; overrides the model setting
ANTHROPIC_DEFAULT_SONNET_MODEL, _OPUS_MODEL, _HAIKU_MODELWhat each model alias points to; the Haiku one also covers background tasks
CLAUDE_CODE_SUBAGENT_MODELThe default model for subagents

ANTHROPIC_SMALL_FAST_MODEL is deprecated; use ANTHROPIC_DEFAULT_HAIKU_MODEL instead.

The gateway must speak Anthropic’s Messages API (/v1/messages) and stream events as they come. A gateway that buffers the full reply makes Claude Code stall. OpenRouter and Ollama both offer that endpoint.

How to use Claude Code with OpenRouter

Step 1. Create an API key in your OpenRouter account (and add credits if you will use paid models).

Step 2. In a terminal, set three variables, as shown in OpenRouter’s Claude Code guide:

export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY=""

Step 3. If you were logged in to Claude Code with an Anthropic account, run /logout inside Claude Code first, so the cached login does not take over.

Step 4. Start claude as usual. To choose models, set the alias variables, for example ANTHROPIC_DEFAULT_SONNET_MODEL. OpenRouter’s guide uses Anthropic model IDs such as ~anthropic/claude-sonnet-latest.

Why ANTHROPIC_API_KEY must be empty: if it holds a value, Claude Code sends it as x-api-key, and the request may be checked against Anthropic instead of OpenRouter.

On Windows PowerShell, the same three lines look like this:

$env:ANTHROPIC_BASE_URL="https://openrouter.ai/api"
$env:ANTHROPIC_AUTH_TOKEN="<your OpenRouter key>"
$env:ANTHROPIC_API_KEY=""

Claude Code OpenRouter settings.json

To keep the setup without typing it every time, put the variables in the env block of your user settings file, ~/.claude/settings.json (on Windows, %USERPROFILE%\.claude\settings.json):

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

Values in the settings file win over shell exports. Never put a key in a project’s shared .claude/settings.json, because that file is usually committed to git. Use .claude/settings.local.json or your user file instead.

Can you use free OpenRouter models with Claude Code?

You can point the model variables at a model ID that ends in :free, but nobody guarantees it will work. OpenRouter’s guide says Claude Code “is only guaranteed to work with the Anthropic first-party provider”, and Anthropic says it does not support routing Claude Code to non-Claude models through any gateway.

The free limits are also tight for an agent. OpenRouter allows 20 requests a minute on free models, and 50 requests a day until you have bought at least 10 credits (then 1,000 a day). One Claude Code task can make dozens of requests, because every file read, edit and command is a separate turn. Our free LLM API guide compares other free tiers. Our OpenRouter free models list shows which :free models exist today, including two built for coding agents.

How to use Claude Code with Ollama (local models)

Ollama documents a one-command setup in its Claude Code integration guide:

ollama launch claude

It asks which model to use and starts Claude Code connected to Ollama. To set it up by hand instead:

export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_API_KEY=""
export ANTHROPIC_BASE_URL=http://localhost:11434

Two settings decide whether it works well:

  • Pick a model that supports tools. Claude Code works by calling tools (read file, edit, run command). Ollama’s guide tells you to choose a model that supports tools; a chat-only model will not get far.
  • Raise the context length to 64k or more. Ollama’s own default is only 4k tokens on GPUs under 24 GB, which is far too small for a codebase. Start the server with OLLAMA_CONTEXT_LENGTH=64000 ollama serve.

If your computer is too small for a good coding model, Ollama’s cloud models are the middle road: set ANTHROPIC_BASE_URL=https://ollama.com and use your Ollama API key as the token, or pick a :cloud model in ollama launch claude. Not sure whether to use Ollama or LM Studio? See Ollama vs LM Studio.

What works and what breaks

  • Works: the Claude Code interface, file edits, commands and slash commands, as long as the model returns well-formed tool calls.
  • Breaks more often with non-Claude models: long multi-step tasks, strict tool formats, extended thinking, and features Anthropic adds first for its own models.
  • Stalls: a gateway or proxy that buffers responses instead of streaming them.
  • Costs creep: with paid OpenRouter models you pay per token, and agents use many tokens. Set a credit limit on your OpenRouter key.

If you want the best results for the money, a Claude subscription or Anthropic API key is still the supported path. Our Claude Free vs Pro guide shows what each plan’s limits cover. OpenRouter and Ollama make sense for experiments, private local code, or cheap background tasks.

Last checked: 25 November 2026

FAQ

Does Claude Code support OpenRouter?

Yes, OpenRouter publishes an official setup: base URL https://openrouter.ai/api, your OpenRouter key as ANTHROPIC_AUTH_TOKEN, and an empty ANTHROPIC_API_KEY. OpenRouter only guarantees it with Anthropic’s own models as the provider.

Can I use Claude Code for free?

Not in a supported way. You can connect it to a local Ollama model, which costs nothing beyond your hardware, or to free OpenRouter models with 50 to 1,000 requests a day. Anthropic does not support non-Claude models in Claude Code, so results vary.

What is the best local model for Claude Code with Ollama?

Neither Anthropic nor Ollama publishes a ranking. Ollama’s guide says to choose a model that supports tools and to give it a 64k or larger context window. On a normal laptop, the cloud models in ollama launch claude are often more practical than a small local model.

Why do I need to set ANTHROPIC_API_KEY to an empty string?

If it has a value, Claude Code sends it in the x-api-key header, which can make the request authenticate against Anthropic instead of your gateway. Setting it to "" avoids that. Also run /logout if you were signed in with an Anthropic account.

Where do I put Claude Code environment variables on Windows?

Either set them in PowerShell with $env:NAME="value" before running claude, or add them to the env block in %USERPROFILE%\.claude\settings.json, which keeps them for every session.

Why does Claude Code freeze with my proxy?

Claude Code needs streamed responses. If the gateway or proxy waits for the full reply before sending it, Claude Code stalls. Use a gateway that streams the Anthropic Messages API events as they arrive.

Similar Posts