Claude Code with Other Models: Ollama, OpenRouter and More
Claude Code with other models works through ANTHROPIC_BASE_URL, either directly or via an LLM gateway. Here is the setup, tier routing, fallbacks and what you give up.

Yes, you can use Claude Code with other models. Claude Code sends every request to the address in ANTHROPIC_BASE_URL, so any server that speaks the Anthropic Messages API can answer it, and an LLM gateway can translate for providers that do not. Anthropic documents the variables but does not support non-Claude models, so test your setup on real tasks before you depend on it.
How does Claude Code connect to a different model?
Claude Code reads its endpoint and credential from environment variables. Anthropic's gateway documentation names the ones that matter here.
ANTHROPIC_BASE_URLis the address of the gateway or model server. Claude Code posts to/v1/messagesunder it.ANTHROPIC_AUTH_TOKENsends your credential as anAuthorization: Bearerheader.ANTHROPIC_API_KEYsends it as anx-api-keyheader. Use the one your server expects.
When one of the credential variables is set, Claude Code uses it instead of a saved claude.ai login. Set both the address and the credential. Anthropic's docs warn that with only the address set, a saved claude.ai login stays the active credential. Run /status in a session to confirm: the Anthropic base URL line shows where requests go, and the credential line names the variable in use.
# bash or zsh (values from Ollama's own docs)
export ANTHROPIC_BASE_URL=http://localhost:11434
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_API_KEY=""
claude --model qwen3.5
# PowerShell
$env:ANTHROPIC_BASE_URL = "http://localhost:11434"
$env:ANTHROPIC_AUTH_TOKEN = "ollama"
$env:ANTHROPIC_API_KEY = ""
claude --model qwen3.5
Model choice has its own variables. ANTHROPIC_MODEL sets the model for one session. ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL and ANTHROPIC_DEFAULT_HAIKU_MODEL decide what the opus, sonnet and haiku aliases resolve to, and CLAUDE_CODE_SUBAGENT_MODEL sets the default for subagents. You can put any of them in the env block of ~/.claude/settings.json instead of exporting them each time. The official gateway connection guide has the full list.
Do you need a gateway to use Claude Code with a local model?
Often not. Ollama, LM Studio and the llama.cpp server each expose an Anthropic-compatible endpoint, so Claude Code can talk to them directly.
| Server | Address to use | What the docs say |
|---|---|---|
| Ollama | http://localhost:11434 | A subset of the Messages API on /v1/messages. Its compatibility page lists no token counting, no tool_choice and no prompt caching. |
| LM Studio | http://localhost:1234 | Exposes POST /v1/messages. Its Claude Code example sets ANTHROPIC_AUTH_TOKEN=lmstudio. |
| llama.cpp | The host and port you give llama-server | The server README lists Anthropic Messages API compatible chat completions. OpenAI-style function calling needs the --jinja flag. |
OpenRouter also publishes an Anthropic-compatible address, https://openrouter.ai/api. Put your OpenRouter key in ANTHROPIC_AUTH_TOKEN and set ANTHROPIC_API_KEY to an empty string. Its Claude Code guide adds that Claude Code is optimized for Anthropic models and may not work correctly with other providers. For Ollama specifically, Ollama's integration page also documents ollama launch claude, which sets everything up for you.
Start with a direct connection. It is the simplest test of whether a model can drive Claude Code at all. A gateway earns its place when one of these applies:
- The provider only offers OpenAI Chat Completions or Gemini formats. The gateway translates requests, streaming and tool calls.
- You want different models for different jobs, which is tier routing, covered next.
- You want a stand-in when a model errors or hits a rate limit.
- You want provider keys in one place instead of in every tool's config.
- You want a log of every request with its cost.
A gateway is also one more moving part. Anthropic's compatibility guide says Claude Code adds capabilities with each release, and a gateway that does not forward them breaks the matching features, so keep yours updated.
What is tier routing in a Claude Code gateway?
Claude Code does not use one model for everything. The opus alias is for complex reasoning, sonnet for daily coding and haiku for simple tasks. Anthropic's docs also say that pinning the haiku model moves background work such as session titles onto it. A gateway that does tier routing sees which tier Claude Code asked for and sends the request to a model you chose.
In My Claude Code the settings are MODEL (used when nothing more specific matches), MODEL_OPUS, MODEL_SONNET and MODEL_HAIKU, plus MODEL_FABLE and MODEL_MYTHOS for the other two tiers. Each value is a slug in the form <provider-id>/<exact-provider-model-id>. The values below only show the format. Pick real ones from the Models page, which lists what each configured provider publishes.
MODEL=deepseek/deepseek-chat
MODEL_OPUS=anthropic/claude-sonnet-4-6
MODEL_SONNET=mistral/devstral-small-latest
MODEL_HAIKU=ollama/<model-tag>
| Tier | Typical job | A sensible choice |
|---|---|---|
| Opus | Planning, hard debugging, large refactors | The strongest model you can afford, which can still be a Claude model on an API key |
| Sonnet | Everyday edits and questions | A mid-priced cloud model |
| Haiku | Background work and simple lookups | A small, fast model, local if your hardware allows |
This is the same idea as the routing in our post on cutting LLM costs by 99%, which sends each request to the cheapest model that passes your checks.
How do fallback chains work?
Each tier gets an ordered list of stand-ins. My Claude Code walks the list when a model errors, rate-limits or refuses. Two related controls keep a bad route from stalling you. A model that answers 429 is benched for a while instead of being retried repeatedly, and a first-token deadline turns a silent model into a failure that moves to the next entry.
A typical chain for the sonnet tier puts a local model first because it costs nothing per token, a cheap cloud model second and a stronger cloud model last. Keep chains short. Claude Code's own docs also say every model has its own prompt cache, so after a failover the stand-in reads the whole conversation without cache hits. Fallbacks protect the session, and the first turn after one can be slower and pricier.
What do you give up with a non-Claude model?
Anthropic's gateway documentation says Anthropic does not support routing Claude Code to non-Claude models through any gateway. It works, and you are on your own when it does not. Expect these differences.
| Area | What changes | What to do |
|---|---|---|
| Tool calling | Claude Code is built around Anthropic models, and models differ in how reliably they emit tool calls. Ollama lists tool_choice as unsupported. | Choose models that advertise tool use. My Claude Code probes what a model can do instead of trusting the catalogue. |
| Context length | Claude Code sends a long system prompt and tool definitions every turn. Ollama recommends a context of 64k or higher. For model IDs it does not recognise, Claude Code assumes a 200K window. | Set CLAUDE_CODE_AUTO_COMPACT_WINDOW to your model's real limit. Claude Code clamps it to at least 100,000 tokens, so models with smaller windows are a poor fit. |
| Prompt caching | Ollama lists prompt caching as unsupported, so a local model reprocesses the whole conversation each turn. Cloud providers vary. | Keep sessions short and run /clear between tasks. |
| Thinking and effort | Support is partial. Ollama accepts budget_tokens and does not enforce it. | Lower the effort level on small models. |
| Guarantees | No vendor tests your exact combination. | Run your own checks. |
Test before you trust. Pick three tasks from your real work, for example a rename across several files, a failing test to fix and a module to explain. Run each one on every candidate model through the same setup, and keep the model that finishes them with the fewest corrections. Leave a Claude route in the opus tier for the cases that need it. If the reason you are looking at other models is cost, read Claude Code free for the options and their limits.
How do you set it up with My Claude Code?
My Claude Code (MCC) is a local gateway that Danube Labs built and maintains. It translates between four API formats, reaches 57 providers including Ollama, LM Studio and llama.cpp, and runs on your machine. These steps take a few minutes.
- Install Claude Code. MCC does not install it for you. On macOS, Linux and WSL run
curl -fsSL https://claude.ai/install.sh | bash. In Windows PowerShell runirm https://claude.ai/install.ps1 | iex. - Install MCC. Run
npx @firedmosquito831/my-claude-codeto try it, ornpm install -g @firedmosquito831/my-claude-codeto keep it.uv tool install my-claude-codeworks if you manage Python 3.14 yourself, and desktop apps exist for Windows, macOS and Linux. Close and reopen your terminal, then checkmcc-server --version. - Start the server. Run
mcc-serverand openhttp://127.0.0.1:8082/admin. - Add a provider. On the Providers page click Configure on a provider, paste the key, then Validate and Apply. For Ollama, LM Studio or llama.cpp, enter the base URL (
OLLAMA_BASE_URL,LM_STUDIO_BASE_URLorLLAMACPP_BASE_URL) instead of a key. - Set the tiers. On Model Config choose
MODEL, thenMODEL_OPUS,MODEL_SONNETandMODEL_HAIKU, each with a fallback chain. - Launch. Run
mcc-claude. Use/statusto see the base URL, and open the Analytics page to watch each request with its cost.
Your provider keys stay in one local .env file, and MCC has no telemetry. It is dual-licensed, AGPL-3.0-or-later or commercial, and independent of Anthropic. It began as a fork of free-claude-code by Alishahryar1 and became a standalone project on 5 September 2026. Source and docs live on GitHub, and the project page has more detail.
What breaks most often, and how do you fix it?
These rows come from Anthropic's own gateway troubleshooting table.
| Symptom | Cause | Fix |
|---|---|---|
401 errors | The credential is in a header the server does not read | Move it between ANTHROPIC_AUTH_TOKEN and ANTHROPIC_API_KEY |
400 naming context_management or Extra inputs are not permitted | The upstream rejects fields Claude Code sends to Anthropic-format endpoints | Set CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 |
400 stating a context or token limit | The server enforces a smaller window than Claude Code assumes | Run /compact and set CLAUDE_CODE_AUTO_COMPACT_WINDOW |
Model missing from /model | Gateway discovery keeps only IDs that contain claude or anthropic | Pass the name with --model, or add one entry with ANTHROPIC_CUSTOM_MODEL_OPTION |
| Login screen even though curl works | No credential variable reached Claude Code before first-run setup | Set ANTHROPIC_AUTH_TOKEN in your shell or in the env block of ~/.claude/settings.json, never in a committed project settings file |
Where Danube Labs fits
Danube Labs is a founder-led software, AI and automation studio in Bucharest. We built My Claude Code to manage providers, keys and routing for AI coding agents in one place, and we use the same ideas (routing, evaluation sets, cost tracking) when we build AI features for clients. If you want to choose models for a product with measured quality instead of guesses, see our AI systems service or book a free intro call.
Frequently asked questions
- Can Claude Code use models other than Claude?
- Technically yes. Claude Code sends requests to the address in ANTHROPIC_BASE_URL, and a model can answer if the server speaks the Anthropic Messages format or sits behind a gateway that translates. Anthropic's docs say it does not support routing Claude Code to non-Claude models through a gateway, so expect gaps in tool calling, caching and thinking, and test before relying on it.
- Does Claude Code work with Ollama?
- Yes. Ollama exposes an Anthropic-compatible /v1/messages endpoint. Its docs set ANTHROPIC_BASE_URL to http://localhost:11434, ANTHROPIC_AUTH_TOKEN to ollama and ANTHROPIC_API_KEY to empty, then run claude with a model tag. The command ollama launch claude does the same setup. Ollama recommends a context length of 64k or higher for larger repositories.
- Do I need a proxy to use Claude Code with OpenRouter?
- No. OpenRouter offers an Anthropic-compatible address at https://openrouter.ai/api. Set ANTHROPIC_BASE_URL to it, put your OpenRouter key in ANTHROPIC_AUTH_TOKEN and leave ANTHROPIC_API_KEY empty. OpenRouter itself warns that Claude Code may not work correctly with non-Anthropic models. A local gateway becomes useful when you want tier routing, fallback chains or several providers behind one address.
- What is tier routing in a Claude Code proxy?
- Claude Code asks for opus, sonnet or haiku class models depending on the job. A gateway with tier routing maps each tier to a model you choose, for example a strong model for planning and a small or local one for background work. Each tier can also carry a fallback chain that takes over when the first model errors or hits a rate limit.
- Will a small local model work well with Claude Code?
- Often poorly. Claude Code sends a long system prompt and tool definitions on every turn, and Ollama recommends a context of 64k or higher. Claude Code also clamps its auto-compact window to at least 100,000 tokens, so with a smaller window you recover with /compact. Pick a tool-capable model with a long context and test it on your own tasks.
- Is My Claude Code free to use?
- The software is free under AGPL-3.0-or-later, and a commercial licence exists if the AGPL terms do not suit you. The models behind it may be free, paid, subscription-based or local, and that is your choice and your account. It runs on your machine with no telemetry, and it is independent of Anthropic.


