Skip to content

Anthropic API 速查

核心 API

python
import anthropic
client = anthropic.Anthropic(api_key="sk-ant-xxxxx")

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,  # 必须指定
    system="系统提示",  # 独立参数,不在 messages 中
    messages=[{"role": "user", "content": "问题"}]
)

Tool Use

python
tools = [{"name": "fn", "input_schema": {"type": "object", "properties": {...}}}]
# 判断 stop_reason == "tool_use" → 执行工具 → 传入 tool_result 继续对话

流式

python
with client.messages.stream(model="...", max_tokens=1024, messages=[...]) as stream:
    for text in stream.text_stream: print(text, end="")

Prompt Caching

python
system=[{"type": "text", "text": "长提示...", "cache_control": {"type": "ephemeral"}}]

节省 90% 成本。

与 OpenAI 差异

维度OpenAIAnthropic
系统提示messages 中 system 角色独立 system 参数
响应choices[0].message.contentcontent[0].text
工具tool_calls 字段contenttool_use block
max_tokens可选必须指定
流式stream=Trueclient.messages.stream()

基于 MIT 许可发布