How to Use Claude Code with Ollama, OpenRouter and DeepSeek
By Shubhranshu, creator of claudeagents · Last reviewed
You can run Claude Code with other models by pointing it at an Anthropic-compatible endpoint with two environment variables: ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN. Ollama (local or cloud models), OpenRouter and DeepSeek all document this. The catch: Anthropic doesn't support running Claude Code on non-Claude models, so features and quality can differ from using Claude itself.
People do this for three reasons: to run models locally for privacy, to try Claude Code without a Claude subscription, or to use a cheaper model for simple work. This guide shows the exact setup for each option, taken from each provider's own documentation, and tells you honestly what works and what doesn't.
Commands and variables were checked against Ollama's, OpenRouter's, DeepSeek's and Anthropic's docs on 28 November 2026.
How Claude Code connects to other models
Claude Code talks to an API in Anthropic's Messages format. Any service that speaks the same format can stand in for Anthropic's API. You point Claude Code at it with environment variables:
| Variable | What it does |
|---|---|
| ANTHROPIC_BASE_URL | The address Claude Code sends requests to |
| ANTHROPIC_AUTH_TOKEN | Your key for that service, sent as a Bearer token |
| ANTHROPIC_API_KEY | Set it to an empty string when the provider tells you to, so it doesn't conflict |
| --model or ANTHROPIC_DEFAULT_*_MODEL | Which model to use, in the provider's naming |
Run /status inside Claude Code to confirm it's using the address and key you set. While these variables are active, your claude.ai subscription isn't used for those requests.
The honest catch (read this first)
- Not supported by Anthropic. Anthropic's docs say it doesn't support routing Claude Code to non-Claude models through any gateway. If something breaks, Anthropic can't help.
- Claude Code is tuned for Claude. Other models may follow its tool instructions less reliably, so multi-step edits can fail more often.
- Some features won't work. For example, Ollama says hosted web search and some advanced tool controls aren't fully supported, and DeepSeek ignores prompt caching and some content types.
- Privacy is per provider. OpenRouter and DeepSeek receive your prompts and code. Only fully local Ollama models keep everything on your machine.
Option 1: Claude Code with Ollama (local or cloud models)
Ollama is the easiest way to run Claude Code on a local model. Its docs give three ways to set it up. The simplest is one command that launches Claude Code already connected:
Manual setup with a local Ollama server
If you prefer to set the variables yourself (Ollama runs on port 11434 by default):
export ANTHROPIC_AUTH_TOKEN=ollama, export ANTHROPIC_API_KEY="", export ANTHROPIC_BASE_URL=http://localhost:11434, then run claude --model qwen3.5 (or any model you've pulled).
Pick the right local model
- Choose a model that supports tools. Claude Code depends on tool calls to read and edit files.
- Set a context window of at least 64k tokens. Larger repositories need more.
- Ollama's cloud models let you use bigger models without downloading them; connect straight to
https://ollama.comwith yourOLLAMA_API_KEYas the token.
ollama launch claude
# Non-interactive (scripts or CI): choose the model and skip prompts
ollama launch claude --model gemma4:cloud --yes -- -p "how does this repository work?"Option 2: Claude Code with OpenRouter
OpenRouter gives you one key for many models. Its own guide for Claude Code uses these variables (put them in your shell profile, not a project .env file):
- Don't add `/v1` to the base URL. Claude Code adds
/v1/messagesitself, and a doubled path returns 404. - OpenRouter says Claude Code is only guaranteed to work with the Anthropic first-party provider. So OpenRouter is great for paying for Claude through one account, but other models may misbehave.
- If you were logged in to Claude before, run
/logoutand relaunch, or you may see model-not-found errors.
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="" # must be explicitly empty
# Optional: list OpenRouter models in /model
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1Option 3: Claude Code with DeepSeek
DeepSeek offers an Anthropic-compatible API. Point Claude Code at it and use your DeepSeek key:
- DeepSeek maps Claude model names to its own models: names starting with
claude-opusgo todeepseek-v4-pro, andclaude-sonnetorclaude-haikugo todeepseek-flash. - It ignores some features Claude Code uses, including prompt caching (
cache_control), and doesn't support some content types, so long sessions can cost more tokens and some tools may fail. - DeepSeek has a dedicated "Integrate with Claude Code" guide in its docs; follow it for the latest details.
export ANTHROPIC_BASE_URL="https://api.deepseek.com/anthropic"
export ANTHROPIC_API_KEY="<your-deepseek-api-key>"
claudeWhich option should you choose?
| Option | Best for | Cost | Privacy | Quality in Claude Code |
|---|---|---|---|---|
| Ollama (local) | Privacy, offline work, experiments | Free (your hardware) | Everything stays on your machine | Depends on the model; small models struggle with tools |
| Ollama (cloud) | Bigger open models without a GPU | Ollama plan | Sent to Ollama | Better than small local models |
| OpenRouter | One account for Claude and other models | Pay per token | Sent to OpenRouter and the provider | Best when routed to Anthropic's Claude models |
| DeepSeek | Cheaper API usage | Pay per token | Sent to DeepSeek | Works, but some features are ignored |
| Claude (Pro/Max or API) | Full features and best results | From $20/month or per token | Sent to Anthropic | What Claude Code is built for |
Want to compare costs? Use our Claude Code cost calculator to see what the same work costs on Claude's own models.
Switch back to Claude
- Remove or comment out the
ANTHROPIC_BASE_URL,ANTHROPIC_AUTH_TOKENandANTHROPIC_API_KEYlines from your shell profile or settings file. - Open a new terminal so the old values are gone.
- Start Claude Code and run
/loginto sign in with your Claude account, then check/status.
Troubleshooting other-model setups
- 401 errors: the key is in the wrong variable. Try
ANTHROPIC_AUTH_TOKENinstead ofANTHROPIC_API_KEY, or the other way round, depending on the provider. - 404 errors: the base URL is wrong, often an extra
/v1at the end. - "Extra inputs are not permitted" (400): the provider rejects newer request fields. Setting
CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1turns most of them off. - Edits fail or tools are ignored: the model probably has weak tool support. Try a larger model or one marked for tool use.
- Settings override your shell: values in a Claude Code settings file win over shell exports. Run
/statusto see what's active.
More error fixes are in our guide to Claude Code errors.
Make any model more useful with ready-made agents
Whatever model you run, clear instructions make a big difference. Small and open models especially benefit from focused, well-written agent files instead of long improvised prompts. claudeagents gives Claude Code 85 ready-made agents for coding, SEO and marketing, each a plain Markdown file you can read and adapt. For the best results, though, pair them with Claude models, which is what Claude Code is designed for.
Frequently asked questions
Can I use Claude Code with Ollama?
Yes. Ollama documents it: run ollama launch claude, or set ANTHROPIC_BASE_URL to http://localhost:11434 with ANTHROPIC_AUTH_TOKEN=ollama and an empty ANTHROPIC_API_KEY, then start claude with a model that supports tools. Anthropic doesn't officially support non-Claude models, so results vary.
Can I use Claude Code without a subscription?
Yes. You can pay per token with an Anthropic API key, or point Claude Code at another provider such as OpenRouter, DeepSeek or a local Ollama model. Only the Claude options are officially supported. The free Claude plan doesn't include Claude Code.
Can I use Claude Code with a local LLM?
Yes, through Ollama or another server with an Anthropic-compatible API. Use a model that supports tool calls and give it at least a 64k-token context. Small local models often struggle with Claude Code's multi-step edits.
How do I use Claude Code with OpenRouter?
Set ANTHROPIC_BASE_URL to https://openrouter.ai/api (without /v1), put your OpenRouter key in ANTHROPIC_AUTH_TOKEN and set ANTHROPIC_API_KEY to an empty string. OpenRouter says it's only guaranteed to work with Anthropic's own models as the provider.
How do I use Claude Code with DeepSeek?
Set ANTHROPIC_BASE_URL to https://api.deepseek.com/anthropic and use your DeepSeek key. DeepSeek maps Claude model names to its own models and ignores some features such as prompt caching. Check DeepSeek's Claude Code guide for the latest steps.
Is it safe to use Claude Code with other models?
It's as safe as the provider you choose: your prompts and code go to that provider unless you run a model locally. Use keys with spending limits, keep them out of project files, and review edits carefully, since weaker models make more mistakes.
Does Anthropic support Claude Code with other models?
No. Anthropic's documentation says it doesn't support routing Claude Code to non-Claude models through any gateway. It works because those providers copy Anthropic's API format, but features and reliability are their responsibility.
Sources
Skip the setup: get 85 ready-made agents
Coding, SEO and marketing kits for Claude Code, with 52 skills and 53 slash commands. $29 one-time, lifetime updates.
See what's included →