Use Claude Code with DeepSeek, Ollama or other models

Point Claude Code at a vendor's Anthropic-compatible endpoint with ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN, and name the vendor's models in ANTHROPIC_MODEL and the ANTHROPIC_DEFAULT_*_MODEL variables. It works, but Anthropic does not support it, one session talks to one vendor, and features such as prompt caching can be silently ignored.

Updated

Questa pagina è scritta in inglese. I termini che spiega sono quelli che si cercano in inglese, e un termine tecnico tradotto è un termine diverso.

How it works

Claude Code sends every request in Anthropic's Messages format to whatever ANTHROPIC_BASE_URL names. A vendor that serves that format can stand in for Anthropic, and Claude Code cannot tell the difference until a request uses something the vendor has not implemented. Anthropic's gateway documentation is explicit that this is unsupported: it "doesn't support routing Claude Code to non-Claude models through any gateway."

Endpoints that work with it

VendorANTHROPIC_BASE_URLNotes
DeepSeekhttps://api.deepseek.com/anthropicOfficial guide. claude-opus* names map to V4 Pro, everything else to V4 Flash
Ollama (local)http://localhost:11434Official integration from Ollama 0.14; ollama launch claude sets it up
Kimi (Moonshot)https://api.moonshot.ai/anthropicOfficial guide; kimi-k2.7-code needs thinking switched on
Z.ai GLMhttps://api.z.ai/api/anthropicOfficial guide
Alibaba Coding Planhttps://coding-intl.dashscope.aliyuncs.com/apps/anthropicOfficial guide; pay-as-you-go endpoints are per region
OpenRouterhttps://openrouter.ai/apiOnly guaranteed with Anthropic as the upstream provider; set ANTHROPIC_API_KEY to an empty string
LM Studio (local)http://localhost:1234Official integration page

Example: DeepSeek

export ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic
export ANTHROPIC_AUTH_TOKEN=<your DeepSeek API key>
export ANTHROPIC_MODEL=deepseek-flash[1m]
export ANTHROPIC_DEFAULT_OPUS_MODEL=deepseek-flash[1m]
export ANTHROPIC_DEFAULT_SONNET_MODEL=deepseek-flash[1m]
export ANTHROPIC_DEFAULT_HAIKU_MODEL=deepseek-flash
export CLAUDE_CODE_SUBAGENT_MODEL=deepseek-flash
claude

These are the values from DeepSeek's own Claude Code page. Set every tier, not just ANTHROPIC_MODEL: a tier left unset still sends a Claude model name, which the endpoint does not know, and the background tasks and subagents that use it fail.

Example: a local model with Ollama

ollama launch claude

That configures Claude Code for you. By hand, it is three variables and a model that supports tool calling:

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

Ollama recommends a context window of 64K tokens or more for larger repositories; a coding agent re-sends the conversation on every turn and a small window fills fast.

What breaks

  • Prompt caching markers. DeepSeek and Ollama ignore Anthropic's cache_control, and Kimi honours it only at the top level. Nothing errors; whether you still get cache pricing depends on the vendor's own caching.
  • Switching vendors. Claude Code reads one ANTHROPIC_BASE_URL, so moving from DeepSeek to Claude means restarting with different settings, not a /model command.
  • Web search. Claude Code's WebSearch tool runs on Anthropic's servers. DeepSeek says its endpoint supports it; Ollama says hosted web search is not fully supported.
  • Unknown request fields. Claude Code treats an unrecognised model name as a current Claude model and sends thinking and effort fields that some endpoints reject with a 400. CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 strips beta fields, at the cost of MCP tool search.
  • Account features. Remote Control and voice dictation are unavailable while a gateway credential is set.
  • Stale credentials. A leftover ANTHROPIC_API_KEY, a saved login or old env values in settings.json can override the setup; /status shows which one is in use.

When a model-agnostic agent fits better

If you want one vendor at a time and already work in Claude Code, the settings above are enough. If you want Claude, DeepSeek and a local model inside the same task, OHarness does that natively: each provider is built in, the conversation is stored in a vendor-neutral format, and /model switches across vendors without a restart. OHarness vs Claude Code compares the two directly.

npm install -g oharness
oharness -m deepseek/deepseek-v4-flash

Then, mid-session, /model anthropic/claude-sonnet-5 for the step that needs it, and back again.

Sources

Common questions

Can Claude Code use DeepSeek models?

Yes, unofficially. DeepSeek serves an Anthropic-compatible endpoint at https://api.deepseek.com/anthropic: set ANTHROPIC_BASE_URL to it, ANTHROPIC_AUTH_TOKEN to a DeepSeek API key, and the model variables to DeepSeek's model names. Anthropic does not support non-Claude models in Claude Code.

Can Claude Code run a local model with Ollama?

Yes. Ollama 0.14 and later serve the Anthropic Messages format, and `ollama launch claude` configures Claude Code to use it. Pick a model that supports tool calling and give it a context window of 64K tokens or more.

Can Claude Code switch between Claude and DeepSeek in one session?

No. Claude Code reads a single ANTHROPIC_BASE_URL, so one Claude Code session talks to one vendor. OHarness switches between Anthropic, DeepSeek, OpenAI, Gemini and local models mid-session with /model.

Why does Claude Code return 400 errors with another vendor's endpoint?

Claude Code assumes an unrecognised model is a current Claude model and sends fields such as adaptive thinking and effort that some endpoints reject. Setting every ANTHROPIC_DEFAULT_*_MODEL variable to the vendor's names, and CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1, removes most of them.

Related