教程

Anthropic Python SDK 完全指南:从安装到生产级集成的最佳实践

Anthropic Python SDK 完整使用指南:安装配置、同步与异步客户端、流式输出、工具调用(Tool Use)、视觉 API、Prompt Caching、错误处理与重试、Token 用量追踪、httpx 客户端自定义,以及在 Django/FastAPI/Flask 框架中的集成最佳实践。

2026/3/164分钟 阅读ClaudeEagle

anthropic 是 Anthropic 官方的 Python SDK,封装了 Claude API 的所有功能。 本文从安装到生产最佳实践,全面讲解 SDK 的使用方法。

安装

bash
pip install anthropic

# 可选:异步支持(已内置,无需额外安装)
# 可选:流式输出(已内置)

基础用法

python
import anthropic

client = anthropic.Anthropic(api_key="sk-ant-你的key")
# 或设置环境变量 ANTHROPIC_API_KEY,无需传参

message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "解释量子纠缠,用通俗语言"}
    ]
)

print(message.content[0].text)
print(f"输入 Token: {message.usage.input_tokens}")
print(f"输出 Token: {message.usage.output_tokens}")

系统提示词

python
message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    system="你是一位专业的 Python 代码审查员。用中文回复,重点关注性能和安全问题。",
    messages=[
        {"role": "user", "content": "帮我审查这段代码:
```python
[代码]
```"}
    ]
)

多轮对话

python
conversation = []

def chat(user_message: str) -> str:
    conversation.append({"role": "user", "content": user_message})
    
    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=conversation
    )
    
    assistant_message = response.content[0].text
    conversation.append({"role": "assistant", "content": assistant_message})
    return assistant_message

# 多轮对话
print(chat("我叫张三"))
print(chat("我叫什么名字?"))  # Claude 会记住「张三」

流式输出(Streaming)

python
# 同步流式
with client.messages.stream(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "写一首关于春天的诗"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

# 获取最终完整消息(含 usage)
final_message = stream.get_final_message()
print(f"
总 Token: {final_message.usage.input_tokens + final_message.usage.output_tokens}")

异步客户端

python
import asyncio
import anthropic

async def main():
    client = anthropic.AsyncAnthropic()
    
    # 异步调用
    message = await client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "你好"}]
    )
    print(message.content[0].text)
    
    # 异步流式
    async with client.messages.stream(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "写一段代码"}]
    ) as stream:
        async for text in stream.text_stream:
            print(text, end="", flush=True)

asyncio.run(main())

工具调用(Tool Use)

python
tools = [
    {
        "name": "get_weather",
        "description": "获取指定城市的当前天气",
        "input_schema": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "城市名称"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
            },
            "required": ["city"]
        }
    }
]

response = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "北京今天天气怎么样?"}]
)

# 检查是否调用了工具
if response.stop_reason == "tool_use":
    tool_use = next(b for b in response.content if b.type == "tool_use")
    print(f"调用工具: {tool_use.name}")
    print(f"参数: {tool_use.input}")
    
    # 执行工具(你自己的函数)
    result = get_weather(**tool_use.input)
    
    # 把结果返回给 Claude
    final = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        tools=tools,
        messages=[
            {"role": "user", "content": "北京今天天气怎么样?"},
            {"role": "assistant", "content": response.content},
            {"role": "user", "content": [
                {"type": "tool_result", "tool_use_id": tool_use.id, "content": str(result)}
            ]}
        ]
    )
    print(final.content[0].text)

Prompt Caching

python
# 长系统提示词或文档缓存,节省 90% 读取成本
response = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": "你是一位代码助手...",  # 短提示词,不缓存
        },
        {
            "type": "text",
            "text": 长文档内容,  # 超长内容,启用缓存
            "cache_control": {"type": "ephemeral"}
        }
    ],
    messages=[{"role": "user", "content": "问题1"}],
    extra_headers={"anthropic-beta": "prompt-caching-2024-07-31"}
)

# 查看缓存命中情况
print(response.usage.cache_creation_input_tokens)  # 首次:缓存写入
print(response.usage.cache_read_input_tokens)       # 后续:从缓存读取

错误处理

python
import anthropic

def safe_call(messages, retries=3):
    for attempt in range(retries):
        try:
            return client.messages.create(
                model="claude-sonnet-4-5",
                max_tokens=1024,
                messages=messages
            )
        except anthropic.RateLimitError:
            if attempt < retries - 1:
                import time
                time.sleep(2 ** attempt)  # 指数退避
            else:
                raise
        except anthropic.AuthenticationError:
            raise  # API Key 错误,不重试
        except anthropic.BadRequestError as e:
            print(f"请求参数错误: {e}")
            raise
        except anthropic.APIConnectionError:
            if attempt < retries - 1:
                import time
                time.sleep(1)
            else:
                raise

在 FastAPI 中集成

python
from fastapi import FastAPI, Depends
import anthropic

app = FastAPI()

# 依赖注入:每个请求共享同一个 client 实例
def get_claude_client() -> anthropic.AsyncAnthropic:
    return anthropic.AsyncAnthropic()

@app.post("/chat")
async def chat(
    message: str,
    client: anthropic.AsyncAnthropic = Depends(get_claude_client)
):
    response = await client.messages.create(
        model="claude-haiku-3-5",  # 接口用 Haiku,速度快
        max_tokens=512,
        messages=[{"role": "user", "content": message}]
    )
    return {"reply": response.content[0].text}

# 流式响应
from fastapi.responses import StreamingResponse

@app.post("/chat/stream")
async def chat_stream(message: str):
    async def generate():
        async with anthropic.AsyncAnthropic().messages.stream(
            model="claude-sonnet-4-5",
            max_tokens=1024,
            messages=[{"role": "user", "content": message}]
        ) as stream:
            async for text in stream.text_stream:
                yield f"data: {text}

"
    
    return StreamingResponse(generate(), media_type="text/event-stream")

来源:Anthropic Python SDK 官方文档

相关文章推荐

教程Claude API 接入完全教程:从零开始调用 Claude,Python/Node.js 实战(2026)Claude API 从零接入完整教程:Console 注册与 API Key 获取、核心接口概览、Python/Node.js SDK 安装与首次调用、多轮对话、流式输出、System Prompt 配置、REST 直调、模型选择、Token 成本控制与批量 API 省费技巧。2026/3/13教程Anthropic Claude API 完整开发指南 2026:Prompt 缓存、自适应推理、工具调用全解析Claude API 2026 年完整开发指南:模型选型和最新定价(Haiku/Sonnet/Opus 对比)、Prompt 缓存实现(最高节省 90% 成本,含代码示例)、自适应推理替代旧 budget_tokens(含流式实现)、工具调用完整循环、流式响应(含 Next.js App Router 示例)、视觉能力(Opus 4.7 支持 3.75MP),以及生产最佳实践(重试、成本监控)。2026/4/24教程LangGraph 2026 完全解析:从零到生产的确定性 AI 工作流引擎实战指南LangGraph 2026 版完整教程:状态机 + LLM 的核心心智模型、State/Node/Edge/Loop/Checkpoint 五大概念详解、生产级 Code Review Agent 完整代码、Human-in-the-Loop 实现、LangGraph Platform 部署,以及与 LangChain 的本质差异。2026/4/19教程AutoGen 实战入门:用微软多 Agent 对话框架构建协作推理系统(含完整代码)AutoGen 完整入门教程:AssistantAgent + UserProxyAgent 基础模式、多 Agent 群聊辩论(代码审查场景)、研究报告三 Agent 协作系统、人在回路配置、终止条件设置,以及 AutoGen vs CrewAI 的实际选择建议,含所有可运行 Python 代码。2026/4/18教程CrewAI 快速上手教程:30 分钟搭建第一个多 Agent 协作系统(含完整代码)CrewAI 完整入门教程:Agent/Task/Crew 三大构建块详解,30 分钟实现技术资讯自动生成的多 Agent 系统,含并行执行配置、内存设置、成本分级控制,以及 Agent 忽略工具、输出不稳定等常见问题解决。2026/4/17教程Sub2API 源码编译部署教程:Go + Vue 3 自定义构建 AI API 中转网关开发者指南Sub2API 源码编译完整指南:Go/Node.js/pnpm 环境搭建、前后端分别编译、-tags embed 嵌入前端、config.yaml 详细配置、开发热重载、Ent/Wire 代码生成、systemd 生产部署,面向开发者和需要自定义的用户。2026/4/14