Blog / Claude Code

Claude Code with DeepSeek: setup and common errors

Point Claude Code at DeepSeek's Anthropic-compatible API with a few env vars or settings.json, and fix the 400s and wrong context size people hit.

Claude Code with DeepSeek: setup and common errors

To use Claude Code with DeepSeek, set ANTHROPIC_BASE_URL to https://api.deepseek.com/anthropic, put your DeepSeek key in ANTHROPIC_AUTH_TOKEN, and set the model variables to deepseek-flash. You can export them in your shell or put them in the env block of ~/.claude/settings.json. Most errors after that are 400s from features DeepSeek's endpoint handles differently, or a context size Claude Code guesses wrong.

Last checked: 20 November 2026, against DeepSeek's API docs and Claude Code's docs.

One thing to know first. Anthropic's gateway docs say it "doesn't support routing Claude Code to non-Claude models through any gateway". It works, many people do it, and DeepSeek documents it. But when something breaks, the fix comes from DeepSeek, from you, or from the community threads linked below.

The quick setup

DeepSeek's Claude Code page gives these variables for Linux and macOS:

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
export CLAUDE_CODE_EFFORT_LEVEL=max
export CLAUDE_CODE_AUTO_COMPACT_WINDOW=786432

Then run claude in the same shell.

What each line does:

Variable What it does
ANTHROPIC_BASE_URL Sends requests to DeepSeek instead of Anthropic
ANTHROPIC_AUTH_TOKEN Your DeepSeek key, sent as Authorization: Bearer
ANTHROPIC_MODEL The model for the main conversation
ANTHROPIC_DEFAULT_OPUS_MODEL, _SONNET_, _HAIKU_ What the opus, sonnet and haiku aliases resolve to. Haiku also runs background tasks
CLAUDE_CODE_SUBAGENT_MODEL The model for subagents
CLAUDE_CODE_EFFORT_LEVEL How hard the model thinks
CLAUDE_CODE_AUTO_COMPACT_WINDOW How full the context gets before Claude Code compacts it

The descriptions come from Claude Code's environment variable reference.

deepseek-flash is DeepSeek's API name for its Flash model. On the Models & Pricing page, that name serves DeepSeek-V4.1-Flash-1017, with a 1M context window. The older name deepseek-v4-flash (people still search for "Claude Code DeepSeek V4 Flash") is still accepted and served by the same model. The [1m] suffix tells Claude Code the model has a 1M window.

The model names matter. DeepSeek's Anthropic API page says names starting with claude-opus are mapped to deepseek-v4-pro and billed at the Pro price. Other claude* names are mapped to deepseek-flash. And: "Unsupported model names that do not start with claude return a 400 error." So a typo in a model name shows up as a 400, and forgetting to set the Opus default can bill you at the Pro rate.

Using settings.json instead

To keep the setup without exporting variables each time, put them in the env block of your user settings file, ~/.claude/settings.json:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.deepseek.com/anthropic",
    "ANTHROPIC_AUTH_TOKEN": "<your DeepSeek API Key>",
    "ANTHROPIC_MODEL": "deepseek-flash[1m]",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek-flash[1m]",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek-flash[1m]",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek-flash",
    "CLAUDE_CODE_SUBAGENT_MODEL": "deepseek-flash",
    "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "786432"
  }
}

Three rules from Claude Code's settings reference and gateway setup page:

  • A value in the env block overwrites the same variable exported in your shell.
  • Don't put the key in a project's .claude/settings.json. That file is usually committed. Use the user file, or .claude/settings.local.json.
  • Run /status inside Claude Code to see which base URL and credential it is using.

Two side effects to expect. Your Claude subscription's usage limits don't apply to requests made with a gateway credential, per the gateway docs. And as of Claude Code v2.1.196, Remote Control is turned off when ANTHROPIC_BASE_URL points anywhere other than api.anthropic.com (env var reference).

What it costs

DeepSeek bills per token. For deepseek-flash at peak, per 1M tokens: $0.006 for cached input, $0.30 for uncached input and $1.20 for output. Off-peak is half. Peak is 01:00 to 04:00 and 06:00 to 10:00 UTC on weekdays. All of this is from DeepSeek's pricing page. Check it before you plan a budget, because it changes.

Common errors and fixes

Each of these comes from DeepSeek's docs, Claude Code's docs, or a GitHub issue where people reported the error. Where a fix is one person's report, it says so.

"role must be user or assistant (got system)"

The error looks like 400 ... messages[1].role: unknown variant 'system', expected 'user' or 'assistant' or messages[1].role must be either 'user' or 'assistant', but got 'system'. It started with Claude Code 2.1.156, which began sending a system role inside the messages list. It is tracked in claude-code#63569 and #63469.

Reporters fixed it by going back to 2.1.153. Later comments disagree on which version stopped it: one said 2.1.157 was fine, another still saw it on 2.1.159. DeepSeek's compatibility table now lists system as "Fully Supported". Update Claude Code first. If it still happens, the threads' workaround is to pin an older version.

400 "thinking options type cannot be disabled when reasoning_effort is set"

This one hits subagents, web search and web fetch, while the main chat works. It is claude-code#65863, seen on v2.1.167. The community's diagnosis: Claude Code turned thinking off for subagent tasks but still sent the effort setting, which DeepSeek's endpoint rejects.

Pointing subagents at another DeepSeek model did not help. What worked for several people was a small proxy that strips the effort field when thinking is off (one report). The issue was closed by the inactivity bot, not by a fix we could confirm. Claude Code's changelog for 2.1.198 says subagents now inherit the session's thinking settings. We have not tested whether that ends this error.

400 on the second turn through OpenRouter

claude-code#68995 reports 400 Provider returned error on every turn after the first, with DeepSeek V4 Flash routed through OpenRouter. The reporter traced it to thinking blocks. DeepSeek's Anthropic API page says that when tool calls continue in thinking mode, the client must pass back the complete content, "including thinking and tool-use blocks". If it doesn't, the request may fail with a 400. The reporter's workaround was a translation proxy. Using DeepSeek's own Anthropic endpoint, as above, avoids the extra hop.

400 on forced tool calls

claude-code#58590 shows 400 deepseek-reasoner does not support this tool_choice on every web search. DeepSeek's table explains the general rule: in thinking mode, tool_choice set to any with only client tools, or to a named tool, "returns a 400 error". DeepSeek's Claude Code page now says its API supports Claude Code's web search natively. If a tool still fails this way, the forced tool choice is the likely cause.

400 "Extra inputs are not permitted" or "Unexpected value(s)"

These come from pre-release request fields an endpoint doesn't know. Claude Code's docs give the fix: set CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1. It strips beta headers and the fields that go with them.

If a gateway rejects the thinking parameter itself, CLAUDE_CODE_DISABLE_THINKING=1 leaves it out. The docs call it "a compatibility option for proxies and gateways that reject the parameter."

Images and PDFs

The answer depends on the model. DeepSeek's compatibility table says deepseek-flash reads images in user messages and tool results, and images in assistant messages return a 400. deepseek-v4-pro does not read images and replaces them with the text [Unsupported Image], which matches claude-code#62875. For documents, only file and text sources are supported in user messages, and other sources return a 400. If a PDF fails, paste the text or convert the pages you need.

The context size shown is wrong

Claude Code doesn't know DeepSeek's model IDs, so it guesses the window. The changelog for 2.1.323 changed that guess from 200K to 1M. Older versions capped the status line and auto-compaction at 200K, which is claude-code#68522.

The fixes, from the model configuration docs:

  • Add the [1m] suffix to the model name, as DeepSeek's setup does.
  • Or set CLAUDE_CODE_MAX_CONTEXT_TOKENS to the real window. A Claude Code maintainer confirmed this for model IDs that don't start with claude-.
  • Set CLAUDE_CODE_AUTO_COMPACT_WINDOW so compaction starts before the window is full.

The wrong key is used, or apiKeyHelper is ignored

Claude Code picks credentials in a fixed order. Its authentication docs list: cloud provider variables, then ANTHROPIC_AUTH_TOKEN, then ANTHROPIC_API_KEY, then apiKeyHelper, then CLAUDE_CODE_OAUTH_TOKEN, then your /login subscription. So an ANTHROPIC_API_KEY left in your shell beats an apiKeyHelper script. claude-code#98838 is an open report of stored credentials overriding apiKeyHelper. Check /status, unset what you don't want, and remember the env block in settings.json wins over your shell.

The no-setup option: dl claude

If you'd rather not manage a DeepSeek key, dl's dl claude command starts the Claude Code you already have on dl's free tier. Every request is served by DeepSeek V4.1 Flash. dl sets the variables for that one run with a key that lasts 15 minutes, and it doesn't read or change anything under ~/.claude. Sign in with GitHub and you get free DeepSeek V4.1 Flash tokens every week. How many depends on your GitHub account.

You don't set any variables yourself. One difference from the setup above: on the free tier, DeepSeek reads text only, so images are left out. One caution: if your ~/.claude/settings.json sets these variables in its env block, or sets apiKeyHelper, Claude Code gives them priority, so remove them first.

Plain dl starts Autopilot, our open-source coding agent, on the same free tier.

Install dl with curl -fsSL https://dl.new | sh, then run dl claude.

FAQs

What is DeepSeek's Anthropic base URL?

https://api.deepseek.com/anthropic. Set it as ANTHROPIC_BASE_URL. It is on DeepSeek's Anthropic API page.

Which model name should I use for DeepSeek V4.1 Flash in Claude Code?

deepseek-flash, with [1m] on the main models so Claude Code knows the window is 1M tokens. The legacy name deepseek-v4-flash is still accepted and served by the same model.

Should I use ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY?

DeepSeek accepts either header, so both work. DeepSeek's Claude Code page uses ANTHROPIC_AUTH_TOKEN. Note that ANTHROPIC_API_KEY replaces your Claude subscription even when you are logged in.

Why does Claude Code show a 200K context for DeepSeek?

Older Claude Code versions assumed 200K for model IDs they didn't know. Add [1m] to the model name or set CLAUDE_CODE_MAX_CONTEXT_TOKENS.

Does Claude Code with DeepSeek use my Claude subscription limits?

No. Requests sent with a gateway credential don't count against your subscription's usage limits. DeepSeek bills them per token instead.

Is there a free way to run Claude Code on DeepSeek?

Yes. dl claude runs your Claude Code on dl's free tier, with free weekly tokens after a GitHub sign-in.