Skip to content

Agent SDK 高级功能

多 Agent 卶作

python
from anthropic import Anthropic

client = Anthropic()

# Agent A - 分析器
analyzer = client.messages.create(
    model="claude-sonnet-4-20250514",
    system=[{"type": "text", "text": "你是代码分析专家..."}],
    messages=[{"role": "user", "content": "分析这段代码"}]
)

# Agent B - 优化器
optimizer = client.messages.create(
    model="claude-sonnet-4-20250514",
    system=[{"type": "text", "text": "你是代码优化专家..."}],
    messages=[{"role": "user", "content": analyzer.content[0].text}]
)
print("优化建议:", optimizer.content[0].text)

流程: 用户 → 分析器 → 优化器 → 用户

工具调用

python
from anthropic import Anthropic

client = Anthropic()

# 定义工具
tools = [
    {
        "name": "get_weather",
        "description": "获取天气信息",
        "input_schema": {
        "type": "object",
        "properties": {
            "city": {"type": "string"}
        }
    }
]

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    tools=tools,
    messages=[{"role": "user", "content": "北京天气怎么样?"}]
)
# Claude 会自动调用 get_weather 工具

下一阶段: 部署指南

最近更新