OpenAI / Anthropic / DeepSeek API 调用对比
接 LLM 的第一道门槛不是技术,是选型。三家主流 API 用法看似都差不多,但字段差异、Tool 调用模型、流式协议各有坑。这篇按"前端开发者会真正撞到的差异"组织,方便选型时直接抄。
::: tip 一句话总结
- OpenAI:生态最广,文档质量稍弱于 Anthropic,价格中位。
- Anthropic:文档最干净,Tool Use 设计最严谨,价格中位偏上。
- DeepSeek:价格 1/10,中文体验非常好,对国内开发者最友好;reasoning 模型 R1 很有竞争力。 :::
一、调用入口对比
| 维度 | OpenAI | Anthropic | DeepSeek |
|---|---|---|---|
| 端点 | https://api.openai.com/v1/chat/completions | https://api.anthropic.com/v1/messages | https://api.deepseek.com/v1/chat/completions |
| 兼容协议 | OpenAI 官方 | 自家 | OpenAI 兼容 |
| Auth header | Authorization: Bearer | x-api-key: + anthropic-version | Authorization: Bearer |
| 国内直连 | ❌ | ❌ | ✅ |
DeepSeek 兼容 OpenAI 协议这件事很重要——你写一份代码可以同时跑 OpenAI 和 DeepSeek,只换 baseURL。Anthropic 不行,必须用它的 SDK 或自己适配。
二、最小请求体
OpenAI / DeepSeek(同款):
{
"model": "gpt-4o-mini",
"messages": [
{ "role": "system", "content": "你是简洁的助手。" },
{ "role": "user", "content": "你好" }
],
"stream": true
}
Anthropic:
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"system": "你是简洁的助手。",
"messages": [
{ "role": "user", "content": "你好" }
],
"stream": true
}
注意三处差异:
max_tokens在 Anthropic 是必填,OpenAI 是可选。漏写会直接 400。system在 Anthropic 是顶层字段,OpenAI 把它塞进 messages 数组。- Anthropic 不接受连续两条
user消息——必须 user/assistant 交替。
三、流式协议差异
OpenAI / DeepSeek
SSE,每个事件 data: {...delta}\n\n,结束以 data: [DONE] 收尾:
data: {"choices":[{"delta":{"content":"你"}}]}
data: {"choices":[{"delta":{"content":"好"}}]}
data: [DONE]
Anthropic
也是 SSE,但有 多种事件类型:
event: message_start
data: {"type":"message_start","message":{...}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"你"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"好"}}
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}
event: message_stop
data: {"type":"message_stop"}
写解析器时 OpenAI 一行 event 都不需要管,Anthropic 必须按 event 字段分流——尤其是要准确处理 Tool Use 时。
四、Tool Use(Function Calling)字段对比
详见 Function Calling vs Tool Use 实战对比,这里只列字段差异:
| 字段 | OpenAI | Anthropic |
|---|---|---|
| 工具声明 | tools: [{ type: "function", function: {...} }] | tools: [{ name, description, input_schema }] |
| 调用结果 | message.tool_calls[],含 id, function.name, function.arguments (JSON 字符串) | content[] 中的 tool_use block,含 id, name, input (已解析对象) |
| 回写结果 | role: tool, tool_call_id, content | role: user,content 中的 tool_result block |
| 多工具并行 | ✅ 同一轮多个 tool_calls | ✅ 同一 message 多个 tool_use block |
| 强制调用 | tool_choice: { type: "function", function: { name } } | tool_choice: { type: "tool", name } |
Anthropic 的设计更"消息为中心":tool result 是用户的下一条消息里的一个 block;OpenAI 是 messages 数组里多一种 role。前者更适合 Agent 流式 UI,后者更适合简单 function call。
五、价格(2026 Q2 参考价)
| 模型 | 输入 / 1M tok | 输出 / 1M tok | 速度 |
|---|---|---|---|
| GPT-4o | $2.5 | $10 | 中 |
| GPT-4o mini | $0.15 | $0.60 | 快 |
| Claude Opus 4.6 | $15 | $75 | 中 |
| Claude Sonnet 4.6 | $3 | $15 | 快 |
| Claude Haiku 4.5 | $1 | $5 | 极快 |
| DeepSeek-V3 | ¥1 (~$0.14) | ¥2 (~$0.28) | 中 |
| DeepSeek-R1 | ¥4 | ¥16 | 慢(推理) |
::: warning 价格随时变,请以官方为准。这里给一个数量级的对比印象。 :::
DeepSeek 的价格优势在 大批量任务(RAG 嵌入查询、日志分析、批量改写)下非常明显——同样预算可以跑 10 倍量。
六、写一个适配三家的最小客户端
type Provider = 'openai' | 'anthropic' | 'deepseek';
interface Config {
provider: Provider;
apiKey: string;
model: string;
}
const ENDPOINTS: Record<Provider, string> = {
openai: 'https://api.openai.com/v1/chat/completions',
deepseek: 'https://api.deepseek.com/v1/chat/completions',
anthropic: 'https://api.anthropic.com/v1/messages',
};
async function* chat(cfg: Config, messages: { role: string; content: string }[]) {
const isAnthropic = cfg.provider === 'anthropic';
const headers: Record<string, string> = {
'Content-Type': 'application/json',
...(isAnthropic
? { 'x-api-key': cfg.apiKey, 'anthropic-version': '2023-06-01' }
: { Authorization: `Bearer ${cfg.apiKey}` }),
};
const body = isAnthropic
? {
model: cfg.model,
max_tokens: 4096,
system: messages.find((m) => m.role === 'system')?.content,
messages: messages.filter((m) => m.role !== 'system'),
stream: true,
}
: { model: cfg.model, messages, stream: true };
const res = await fetch(ENDPOINTS[cfg.provider], {
method: 'POST',
headers,
body: JSON.stringify(body),
});
const reader = res.body!.pipeThrough(new TextDecoderStream()).getReader();
let buffer = '';
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += value;
const events = buffer.split('\n\n');
buffer = events.pop() ?? '';
for (const ev of events) {
const dataLine = ev.split('\n').find((l) => l.startsWith('data: '));
if (!dataLine) continue;
const data = dataLine.slice(6);
if (data === '[DONE]') return;
try {
const json = JSON.parse(data);
if (isAnthropic) {
if (json.type === 'content_block_delta') {
yield json.delta.text;
}
} else {
const delta = json.choices?.[0]?.delta?.content;
if (delta) yield delta;
}
} catch {
/* ignore */
}
}
}
}
这个客户端覆盖 90% 日常需求。Tool Use 的部分需要在 anthropic 分支额外处理 content_block_start 中 type === 'tool_use' 的情况。
七、选型建议
- 国内项目、成本敏感、要 ICP 备案:DeepSeek。
- 要最稳的 Tool Use / Agent 行为:Anthropic Sonnet 4.6 或 Opus 4.6。
- 要海量轻量任务(IDE 补全后端、批量改写):GPT-4o mini 或 DeepSeek-V3。
- 要深度推理(数学、复杂规划):Claude Opus 4.6 或 DeepSeek-R1。
- 要做 RAG 嵌入:自己跑 Ollama + nomic-embed-text,比走 API 便宜。
::: info 相关阅读