主题
Agent SDK 核心概念
Message 消息结构
python
message = {
"role": "user" | "assistant",
"content": "消息内容"
}Tool 调用流程
python
# 1. 定义工具
tools = [
{
"name": "get_weather",
"description": "获取天气信息",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
]
# 2. Claude 决定调用工具
# 3. 执行工具并返回结果
# 4. Claude 继续处理流式响应
python
with client.messages.stream(...) as stream:
for event in stream:
if event.type == "content_block_delta":
print(event.delta.text, end="")下一节: 实战项目