Skip to main content

Using the Anthropic API

To meet the demand for using the Anthropic API ecosystem, our API has added support for the Anthropic API format, with the base_url being https://api.deepseek.com/anthropic.

With simple configuration, you can integrate the capabilities of DeepSeek into the Anthropic API ecosystem.

For request parameters, response fields, and complete HTTP examples, see the Messages API reference.


Use DeepSeek in Claude Code​

Please refer to Integrate with Claude Code.

Invoke DeepSeek Model via Anthropic API​

  1. Install Anthropic SDK
pip install anthropic
  1. Config Environment Variables
export ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic
export ANTHROPIC_API_KEY=${YOUR_API_KEY}
  1. Invoke the API
import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
model="deepseek-flash",
max_tokens=1000,
system="You are a helpful assistant.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Hi, how are you?"
}
]
}
]
)
print(message.content)

Note: Unsupported model names that start with claude are still accepted: claude-opus* names are mapped to deepseek-v4-pro, and other claude* names are mapped to deepseek-flash. Unsupported model names that do not start with claude return a 400 error.


Anthropic Model Mapping​

When you use the Anthropic API, we map the Claude model names you pass in:

  • Models starting with claude-opus are mapped to deepseek-v4-pro
  • Models starting with claude-haiku or claude-sonnet are mapped to deepseek-flash

The claude-opus mapping points to deepseek-v4-pro, which is billed at the V4 Pro price.

With this mapping, when using the developer mode of the new Claude Desktop APP, you can bypass the APP's model name restrictions by simply changing the base_url and api_key to connect to DeepSeek models.


Anthropic API Compatibility Details​

This section lists the compatibility details of the DeepSeek API with the Anthropic API. For the full Anthropic API format definition, please refer to the official Anthropic API reference.

HTTP Header​

Authenticate with either x-api-key: <API_KEY> or Authorization: Bearer <API_KEY>.

FieldSupport Status
anthropic-betaIgnored for /messages, except that an image with source.type of file requires files-api-2025-04-14; required for Files API endpoints — see Files API
anthropic-versionIgnored
x-api-keyFully Supported
AuthorizationSupports Bearer <API_KEY> as an alternative to x-api-key

Simple Fields​

FieldSupport Status
modelUse DeepSeek Model Instead
max_tokensSupported. Optional in HTTP requests; the default and effective limit depend on the model, thinking mode, and effort. The Anthropic SDK requires this argument, so SDK examples set it explicitly. See the compatibility exception below
containerIgnored
mcp_serversIgnored
metadatauser_id is supported, others are ignored
Please refer to Rate Limit & Isolation for more information about user_id parameter.
service_tierIgnored
stop_sequencesFully Supported
streamFully Supported
systemFully Supported
temperatureValid range: [0.0 ~ 2.0]. Takes effect in non-thinking mode; fixed at 1.0 in thinking mode
thinkingSupports type: "enabled" and type: "disabled"; adaptive is treated as enabled. budget_tokens is ignored
output_configOnly effort is supported
top_kIgnored
top_pDepends on the model:
deepseek-flash: takes effect in thinking mode (lower bound 0.95); fixed at 1.0 in non-thinking mode.
deepseek-v4-pro: ignored in thinking mode (fixed at 0.95 when tool calling is enabled, otherwise 1.0); takes effect in non-thinking mode. Setting tool_choice to {"type": "none"} disables tool calling even when tools is provided

For certain main-agent requests from Claude Code or OpenCode in thinking mode, compatibility handling can increase the effective max_tokens above the requested value. In these cases, this parameter does not provide a strict output-token limit.

Tool Fields​

tools​

For client-defined tools, provide name, input_schema, and an optional description, and omit type. The type field selects a server tool; passing type: "custom" for a client tool returns a 422 error.

FieldSupport Status
nameFully Supported
input_schemaFully Supported
descriptionFully Supported
cache_controlIgnored

tool_choice​

ValueSupport Status
noneFully Supported
autoSupported (disable_parallel_tool_use is ignored)
anyTreated as auto; does not force a tool call (disable_parallel_tool_use is ignored)
toolSelects a named client tool in non-thinking mode; returns a 400 error in thinking mode (disable_parallel_tool_use is ignored)

Returning tool results​

For multi-turn conversations, send the conversation history in messages on every request. After an assistant message containing client tool_use blocks, the immediately following message must have the user role and include a tool_result for every tool call, with matching tool_use_id values. Place all tool_result blocks before any text in that user message.

When continuing tool calls in thinking mode, preserve and pass back the assistant's complete content array, including thinking and tool-use blocks. If thinking content is omitted, the service may recover it using the tool-call IDs; if recovery is required but fails, the request returns a 400 error. Do not rely on automatic recovery.

Message Fields​

FieldVariantSub-FieldSupport Status
content string Fully Supported
array, type="text" text Fully Supported
cache_control Ignored
citations Ignored
array, type="image" source deepseek-flash supports images in user messages and tool_result content; images in assistant messages return a 400 error. deepseek-v4-pro does not process images and replaces them with the text [Unsupported Image].
source.type can be base64 (media types: image/jpeg, image/png, image/gif, image/webp), url, or file (include the header anthropic-beta: files-api-2025-04-14 when using file images).
array, type = "document" Not Supported
array, type = "search_result" Not Supported
array, type = "thinking" Supported
array, type="redacted_thinking" Not Supported
array, type = "tool_use" id Fully Supported
input Fully Supported
name Fully Supported
cache_control Ignored
array, type = "tool_result" tool_use_id Fully Supported
content Fully Supported
cache_control Ignored
is_error Ignored
array, type = "server_tool_use" Supported
array, type = "web_search_tool_result" Supported
array, type = "code_execution_tool_result" Not Supported
array, type = "mcp_tool_use" Not Supported
array, type = "mcp_tool_result" Not Supported
array, type = "container_upload" Not Supported

Server tool usage​

A content block with type: "server_tool_use" describes a tool call executed by the service. It contains the call id, tool name, JSON input, and caller. For web search, name is web_search, input contains the search query, and caller is {"type":"direct"}. The matching web_search_tool_result block uses the same ID in tool_use_id. The service runs the search and continues generating the answer; the client does not need to execute the tool or supply a tool_result.

usage.server_tool_use reports request-level counters for these searches:

FieldMeaning
web_search_requestsNumber of attempted server-side web searches, including attempts rejected for exceeding max_uses, invalid queries, and failed searches. It can therefore exceed max_uses.
tool_searchNumber of web searches that succeeded and returned at least one result. A failed search or an empty result does not increment this counter. It counts calls, not individual search results.

The object is omitted when no web search was attempted; declaring a search tool in tools does not guarantee that it appears. For a non-streaming response, read usage.server_tool_use. For streaming, read the cumulative counters in the final message_delta.usage.server_tool_use; the initial message_start usage does not contain them.

For example, send this request body to POST /anthropic/v1/messages with your DeepSeek API key:

{
"model": "deepseek-v4-pro",
"max_tokens": 256,
"thinking": {"type": "disabled"},
"tools": [
{"type": "web_search_20250305", "name": "web_search", "max_uses": 1}
],
"messages": [
{
"role": "user",
"content": "Use the web search tool once to search for Python official website. Then reply with only the official site URL."
}
]
}

A successful response to this request contained server_tool_use, web_search_tool_result, and text content blocks, with the following usage. Token counts and search counts can vary between requests.

{
"usage": {
"input_tokens": 2912,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 256,
"output_tokens": 52,
"service_tier": "standard",
"server_tool_use": {
"web_search_requests": 1,
"tool_search": 1
}
}
}

Streaming and token usage​

Set stream to true to receive server-sent events (SSE). A normal response starts with message_start, emits content_block_start, content_block_delta, and content_block_stop for its content blocks, then ends with message_delta and message_stop. ping events may appear between these events. The stream does not send data: [DONE].

Normal message events have an SSE event name and a JSON type field. Keep-alive comments may also appear. If waiting for generation to start times out, the service may send an unnamed data frame containing {"error":{"message":"..."}}, without an SSE event name or a top-level JSON type, and then close the connection. A connection that closes before message_stop indicates an incomplete response.

Use the final message_delta.usage as the request's token usage; do not add it to the initial usage in message_start. input_tokens excludes cache hits, cache_read_input_tokens reports cache hits, and cache_creation_input_tokens is currently 0. The total input token count is input_tokens + cache_read_input_tokens + cache_creation_input_tokens.

See the Messages API reference for response examples and error handling.