DeepSeek API
入门
零代码迁移:用 DeepSeek 替代 Claude API
2026-08
10 分钟阅读
#Anthropic API
#Claude迁移
#API兼容
#零代码
#降低成本
一行迁移
DeepSeek 提供与 Anthropic Messages API 完全兼容的端点,只需修改两行:
# 原代码(使用 Claude)
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-xxx')
# 迁移到 DeepSeek——只改两行!
import anthropic
client = anthropic.Anthropic(
api_key='sk-your-deepseek-key',
base_url='https://api.deepseek.com/anthropic'
)
# 以下代码完全不变!
response = client.messages.create(
model='claude-sonnet-4-20250514',
max_tokens=1024,
system='You are a helpful assistant.',
messages=[{'role': 'user', 'content': 'Hello!'}]
)
print(response.content[0].text)
兼容性对照表
| Anthropic 参数 | DeepSeek 支持 | 说明 |
|---|
| messages.create() | ✅ 支持 | 标准消息创建 |
| system prompt | ✅ 支持 | 系统提示词 |
| max_tokens | ✅ 支持 | 最大生成 token 数 |
| stop_sequences | ✅ 支持 | 停止序列 |
| temperature / top_p / top_k | ✅ 支持 | 采样参数 |
| stream | ✅ 支持 | 流式输出(SSE) |
| tool_use(Function Calling) | ✅ 支持 | 工具调用 |
Node.js 迁移示例
// 迁移到 DeepSeek——只改 baseURL 和 apiKey
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: 'https://api.deepseek.com/anthropic'
});
const msg = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [{ role: 'user', content: '解释量子计算' }]
});
console.log(msg.content[0].text);
典型成本对比
| Claude 4.5 Sonnet | DeepSeek V4 Flash |
|---|
| 输入($/M tokens) | $3.00 | $0.14(节省 95%) |
| 输出($/M tokens) | $15.00 | $0.28(节省 98%) |
| 百万 tokens 总成本 | $18.00 | $0.42(节省 97.7%) |
注意事项
- 模型名保留:使用 Anthropic SDK 时传入 claude 模型名,DeepSeek 自动映射
- max_tokens 必填:与 Anthropic API 一致,此参数为必填项
- 响应格式一致:返回结构与 Anthropic API 完全相同
- 多模态差异:DeepSeek 不支持图像输入,确保只发送文本消息