Ollama Claude Code Setup: Local Models and OpenRouter
Ollama Claude Code setup step by step: ollama launch claude, the env vars for local and cloud models, OpenRouter config, and what stops working off Claude.
Josh Pocock7 min read
AI Coding
Claude Code + Ollama
Stride AI Academy
To use Ollama with Claude Code, install both, then run ollama launch claude. Ollama exposes an Anthropic-compatible API, so Claude Code can send its requests to a local model on your machine or to Ollama's cloud models instead of to Anthropic. You can also set it up by hand with three environment variables, and OpenRouter works the same way with its own URL and key.
This guide covers the quick start, the manual setup for local and cloud models, context length, the OpenRouter setup, and what you give up when Claude Code isn't talking to Claude. Everything here was checked against Ollama's Claude Code docs, Ollama's context length docs, OpenRouter's Claude Code guide and Anthropic's LLM gateway docs and gateway connection guide on November 22, 2026.
Read this first: other models aren't Claude
Anthropic's gateway docs say it "doesn't support routing Claude Code to non-Claude models through any gateway." Ollama and OpenRouter both document the setup, but if something breaks with a local model, Anthropic's support won't cover it.
Claude Code is built and tested with Claude. A local model may call tools differently, miss steps or edit files badly. Ollama's own docs say to pick a model that supports tools. OpenRouter's guide warns that Claude Code is tuned for Anthropic models and may not work correctly with others. Test on a throwaway project before you trust it with client work.
Quick start: ollama launch claude
With Claude Code installed and Ollama running, one command starts Claude Code wired to Ollama:
ollama launch claudeIt shows a model picker. For scripts, Docker or CI, skip the picker with --yes and name a model:
ollama launch claude --model gemma4:cloud --yesOllama's docs also show passing Claude Code's own flags, such as -p for a one-off prompt, after a double-hyphen separator at the end of that command.
--yes needs --model, and it pulls the model if you don't have it yet.
Manual setup with a local model
If you'd rather run claude yourself, set three variables. These come from Ollama's docs:
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_API_KEY=""
export ANTHROPIC_BASE_URL=http://localhost:11434Then start Claude Code with a model you've pulled:
claude --model qwen3.5On Windows, set the same values in PowerShell, using the $env: form from Anthropic's gateway guide:
$env:ANTHROPIC_AUTH_TOKEN = "ollama"
$env:ANTHROPIC_API_KEY = ""
$env:ANTHROPIC_BASE_URL = "http://localhost:11434"
claude --model qwen3.5What each one does:
| Variable | Value | Why |
|---|---|---|
ANTHROPIC_BASE_URL |
http://localhost:11434 |
Sends requests to Ollama instead of Anthropic |
ANTHROPIC_AUTH_TOKEN |
ollama |
A bearer token, so Claude Code doesn't fall back to your Claude login |
ANTHROPIC_API_KEY |
"" (empty) |
Stops a real Anthropic key from taking over |
Shell exports last for one terminal session. To keep them, put them in the env block of ~/.claude/settings.json. Ollama's docs warn that Claude settings can override shell variables, so if you've set a base URL there before, update it there. Our settings.json guide shows where each file lives, and Claude Code environment variables covers the rest of the variables.
Set the context length to 64k or more
This is the step people skip. Ollama's default context length depends on your GPU memory:
| GPU memory (VRAM) | Default context |
|---|---|
| Under 24 GiB | 4k tokens |
| 24 to 48 GiB | 32k tokens |
| 48 GiB or more | 256k tokens |
Ollama recommends at least 64,000 tokens for coding tools. A 4k window fills up almost immediately in Claude Code. Raise it when you start the server:
OLLAMA_CONTEXT_LENGTH=64000 ollama serveIn the Ollama app, use the context length slider in settings instead. Check it took effect with:
ollama psThe CONTEXT column shows the allocated length, and PROCESSOR shows whether the model fits on your GPU or spills onto the CPU. A bigger context uses more memory, so a model that ran fine at 4k may slow down at 64k.
Connect to Ollama's cloud models
You don't need a local install to use Ollama's cloud models. Set your Ollama API key in OLLAMA_API_KEY and point Claude Code at ollama.com:
ANTHROPIC_BASE_URL=https://ollama.com \
ANTHROPIC_AUTH_TOKEN="$OLLAMA_API_KEY" \
ANTHROPIC_API_KEY="" \
claude --model glm-5.3-flashCloud models let you run larger models without downloading them. Ollama's docs name gemma4:cloud, glm-5.3-flash and kimi-k2.7-code:cloud as examples, and you can browse more in its cloud model search. Pick one that supports tools.
Ollama's notes say basic chat and file edits work on cloud connections with compatible models, but hosted web search and advanced tool controls aren't fully supported.
Check that it's connected
Inside Claude Code, run:
/statusAnthropic's connection guide says to look for two lines. Anthropic base URL should show the Ollama or OpenRouter address. An Auth token line naming ANTHROPIC_AUTH_TOKEN confirms the gateway credential is active. If you see a Login method line naming a claude.ai account instead, your token didn't reach the session.
How to use Claude Code with OpenRouter
OpenRouter is a different case. Its guide is mainly about running Claude models through OpenRouter's billing, and it says Claude Code through OpenRouter is only guaranteed to work with Anthropic's first-party provider. It suggests setting Anthropic as your top-priority provider.
Set these in your shell profile, defining the key first:
export OPENROUTER_API_KEY="<your-openrouter-api-key>"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY=""Or put them in a project's .claude/settings.local.json:
{
"env": {
"ANTHROPIC_BASE_URL": "https://openrouter.ai/api",
"ANTHROPIC_AUTH_TOKEN": "<your-openrouter-api-key>",
"ANTHROPIC_API_KEY": ""
}
}Three details from OpenRouter's guide:
- Clear your old login. If you signed in with a Claude account before, run
/logoutin Claude Code, then quit and relaunch. - Don't use a project
.envfile. The native installer doesn't read it. - Pin models per tier with
ANTHROPIC_DEFAULT_OPUS_MODEL,ANTHROPIC_DEFAULT_SONNET_MODEL,ANTHROPIC_DEFAULT_HAIKU_MODELandANTHROPIC_DEFAULT_FABLE_MODEL. OpenRouter says Fable won't show in/modelby default, so the Fable variable is the only way to select it there.
To see the gateway's models in the /model picker, Anthropic documents an opt-in variable:
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1OpenRouter notes its picker shows a curated set of top models, not its full catalog, and some entries route to non-Anthropic providers. Our guide to changing the model in Claude Code covers how the picker and aliases work.
Never put the key in a project's .claude/settings.json. Anthropic's docs warn that file is committed and shared with everyone who clones the repository.
What stops working
From Anthropic's gateway docs, while these variables are set:
- Your Claude plan doesn't apply. Requests carry the gateway credential instead of your claude.ai login, so plan limits don't cover them. You pay whoever issued the key, or nothing for a local model.
- Remote Control is off. It's unavailable while a gateway credential is active, and also while
ANTHROPIC_BASE_URLpoints at a non-Anthropic host. - Voice dictation is off while a gateway credential is active.
- Newer features can break. Claude Code adds capabilities in each release, and a backend that doesn't support them breaks the matching features.
To go back to Claude, unset the variables (or remove them from your settings file), then run /login.
Ollama, OpenRouter or Claude Code Router?
| Ollama | OpenRouter | Claude Code Router | |
|---|---|---|---|
| What it is | Runs models locally or in Ollama's cloud | Hosted API for many providers | Open-source local gateway |
| Main use with Claude Code | Local and open models | Claude models on OpenRouter billing | Switching between providers with routing rules |
| Setup | ollama launch claude or three variables |
Base URL, token, empty API key | App or npm, configured in a UI |
| Cost | Free locally, or Ollama cloud | OpenRouter's per-token rates | Free software, plus provider rates |
If you want routing rules, fallbacks and per-request cost logs across several providers, see our Claude Code Router guide. For what Claude Code costs on Anthropic's own plans, see how much Claude Code costs.
Learn to build with Claude Code
The Stride AI Coding course teaches Claude Code and Codex for building client tools, written for non-programmers, with weekly live calls to review your builds. Many of the free guides cover Claude Code setups, and the Claude Code community is where members compare theirs.
FAQ
Can Claude Code use Ollama?
Yes. Ollama has an Anthropic-compatible API, and ollama launch claude starts Claude Code connected to it. You can also set ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN and an empty ANTHROPIC_API_KEY yourself. Anthropic doesn't support non-Claude models, so test results carefully.
Which local model should I use with Claude Code?
Ollama's docs say to choose a model that supports tools and to set a context window of 64k tokens or more. Their local example is qwen3.5. Check ollama ps to confirm the model fits on your GPU.
Is Claude Code with Ollama free?
Running a local model through Ollama costs nothing beyond your hardware and electricity. Your Claude subscription doesn't apply to these requests, and Ollama's cloud models need an Ollama API key.
How do I use OpenRouter with Claude Code?
Set ANTHROPIC_BASE_URL to https://openrouter.ai/api, put your OpenRouter key in ANTHROPIC_AUTH_TOKEN and set ANTHROPIC_API_KEY to an empty string. Run /logout if you were signed in to Claude, then check /status.
Why does Claude Code still use my Claude account?
Usually because a saved login or a real ANTHROPIC_API_KEY is taking over. Set ANTHROPIC_API_KEY to an empty string, run /logout, relaunch, and check that /status shows your gateway's base URL.
What features don't work with Ollama in Claude Code?
Remote Control and voice dictation are off while a gateway credential is active. Ollama says hosted web search and advanced tool controls aren't fully supported on cloud connections. Results also depend on how well the model handles tools.