Claude Platform Docs
  • 消息
  • 托管智能体
  • 管理

Search...
⌘K
第一步
Claude 简介快速入门
使用 Claude 构建
功能概览使用 Messages API停止原因与回退拒绝与回退回退额度
模型能力
扩展思考自适应思考努力程度任务预算(测试版)快速模式(研究预览)结构化输出引用流式传输消息批量处理搜索结果流式传输拒绝多语言支持嵌入
工具
概览工具使用的工作原理教程:构建使用工具的智能体定义工具处理工具调用并行工具使用工具运行器(SDK)严格工具使用服务器工具网络搜索工具网页抓取工具代码执行工具顾问工具工具搜索工具记忆工具Bash 工具文本编辑器工具计算机使用工具故障排除
工具基础设施
工具参考管理工具上下文工具组合工具使用与提示缓存编程式工具调用细粒度工具流式传输
上下文管理
上下文窗口压缩上下文编辑提示缓存对话中系统消息构建编排模式缓存诊断(测试版)令牌计数
处理文件
Files APIPDF 支持
技能
概览快速入门最佳实践企业技能API 中的技能
MCP
远程 MCP 服务器MCP 连接器
云平台上的 Claude
Amazon BedrockAmazon Bedrock(旧版)AWS 上的 Claude PlatformGoogle CloudMicrosoft Foundry

Log in
处理工具调用
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Claude Platform Docs

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Claude on AWS
  • Claude on Google Cloud

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
消息/工具

处理工具调用

解析 tool_use 块,格式化 tool_result 响应,并使用 is_error 处理错误。

本页介绍工具调用的生命周期:从 Claude 的响应中读取 tool_use 块、在您的回复中格式化 tool_result 块,以及发出错误信号。如需了解自动处理这些操作的 SDK 抽象层,请参阅 Tool Runner。



使用 Tool Runner 更简单:本页描述的手动工具处理流程由 Tool Runner 自动管理。当您需要对工具执行进行自定义控制时,请参考本页内容。

Claude 的响应会根据其使用的是客户端工具还是服务器工具而有所不同。

处理客户端工具的结果

响应的 stop_reason 将为 tool_use,并包含一个或多个 tool_use 内容块,其中包括:

  • id:此特定工具使用块的唯一标识符。稍后将用于匹配工具结果。
  • name:正在使用的工具的名称。
  • input:一个对象,包含传递给工具的输入,符合该工具的 input_schema。

当您收到客户端工具的工具使用响应时,您应该:

  1. 从 tool_use 块中提取 name、id 和 input。
  2. 在您的代码库中运行与该工具名称对应的实际工具,并传入工具的 input。
  3. 通过发送一条 role 为 user 的新消息来继续对话,该消息包含一个 tool_result 类型的 content 块以及以下信息:
    • tool_use_id:此结果所对应的工具使用请求的 id。
    • content(可选):工具的结果,可以是字符串(例如 "content": "15 degrees")、嵌套内容块列表(例如 "content": [{"type": "text", "text": "15 degrees"}]),或文档块列表(例如 "content": [{"type": "document", "source": {"type": "text", "media_type": "text/plain", "data": "15 degrees"}}])。这些内容块可以使用 text、image、document 或 search_result 类型。
    • is_error(可选):如果工具执行导致错误,则设置为 true。


重要的格式要求:

  • 在消息历史记录中,工具结果块必须紧跟在其对应的工具使用块之后。您不能在助手的工具使用消息和用户的工具结果消息之间插入任何消息。
  • 在包含工具结果的用户消息中,tool_result 块必须位于 content 数组的最前面。任何文本都必须放在所有工具结果之后。
  • 如果助手回合还调用了尚无结果块的服务器工具,则用户消息必须仅包含 tool_result 块。在结果之后添加文本会提前结束该回合;对于 Claude 直接调用的服务器工具,请求随后会失败并返回 400 错误,错误中会指明未解析的服务器工具。请参阅停止原因和回退。

例如,以下内容将导致 400 错误:

{
  "role": "user",
  "content": [
    { "type": "text", "text": "Here are the results:" }, // ❌ Text before tool_result
    { "type": "tool_result", "tool_use_id": "toolu_01" /* ... */ }
  ]
}

当助手回合仅调用客户端工具时,以下内容是正确的:

{
  "role": "user",
  "content": [
    { "type": "tool_result", "tool_use_id": "toolu_01" /* ... */ },
    { "type": "text", "text": "What should I do next?" } // ✅ Text after tool_result
  ]
}

如果您收到类似 "tool_use ids were found without tool_result blocks immediately after" 的错误,请检查您的工具结果格式是否正确。



工具结果通常包含来自您无法控制的来源的内容:网页、入站电子邮件、用户上传的文件、第三方 API。请将这些内容视为不可信:能够影响这些内容的攻击者可能会嵌入试图重定向 Claude 的指令(即间接提示注入,indirect prompt injection)。请将不可信内容保留在 tool_result 块中,而不是 system 提示或普通的用户 text 块中,并参阅缓解越狱和提示注入以进一步加固。

收到工具结果后,Claude 将使用该信息继续生成对原始用户提示的响应。

处理服务器工具的结果

Claude 在内部执行工具,并将结果直接整合到其响应中,无需额外的用户交互。



一个响应可以同时包含客户端 tool_use 块和尚无结果块的 server_tool_use 块。该服务器工具调用尚未完成,其结果块将在后续响应中到达。请回复一条仅包含客户端工具的 tool_result 块的用户消息,并保持相同的 tools 数组;对于 Claude 直接调用的服务器工具,API 会在该请求上运行它,下一个响应将以其结果块开始。请参阅停止原因和回退。



与其他 API 的区别

与那些将工具使用分离出来或使用 tool 或 function 等特殊角色的 API 不同,Claude API 将工具直接集成到 user 和 assistant 消息结构中。

消息包含 text、image、tool_use 和 tool_result 块的数组。user 消息包含客户端内容和 tool_result,而 assistant 消息包含 AI 生成的内容和 tool_use。

使用 is_error 处理错误

在与 Claude 一起使用工具时,可能会发生几种不同类型的错误:

后续步骤

并行工具使用

处理 Claude 在单个回合中调用多个工具的响应。


Tool Runner (SDK)

让 SDK 为您管理 tool_use 循环、结果格式化和重试。

定义工具

编写 schema 和描述,引导 Claude 选择正确的工具。

Was this page helpful?

  • 处理客户端工具的结果
  • 处理服务器工具的结果
  • 使用 is_error 处理错误
  • 后续步骤