工具使用的工作原理
Claude 支持两种类型的工具:Anthropic 定义的工具使用版本化类型(例如,
web_search_20250305、text_editor_20250124)来确保跨模型版本的兼容性。客户端工具
通过以下步骤将客户端工具与 Claude 集成:1
为 Claude 提供工具和用户提示
- 在您的 API 请求中定义具有名称、描述和输入模式的客户端工具。
- 包含可能需要这些工具的用户提示,例如,“旧金山的天气如何?”
2
Claude 决定使用工具
- Claude 评估是否有任何工具可以帮助处理用户的查询。
- 如果是,Claude 构造一个格式正确的工具使用请求。
- 对于客户端工具,API 响应的
stop_reason为tool_use,表示 Claude 的意图。
3
执行工具并返回结果
- 从 Claude 的请求中提取工具名称和输入
- 在您的系统上执行工具代码
- 在包含
tool_result内容块的新user消息中返回结果
4
Claude 使用工具结果制定响应
- Claude 分析工具结果以制定对原始用户提示的最终响应。
服务器工具
服务器工具遵循不同的工作流程:2
Claude 执行服务器工具
- Claude 评估服务器工具是否可以帮助处理用户的查询。
- 如果是,Claude 执行工具,结果会自动合并到 Claude 的响应中。
3
Claude 使用服务器工具结果制定响应
- Claude 分析服务器工具结果以制定对原始用户提示的最终响应。
- 服务器工具执行不需要额外的用户交互。
工具使用示例
以下是一些代码示例,演示各种工具使用模式和技术。为了简洁起见,工具是简单的工具,工具描述比理想情况下要短,以确保最佳性能。单个工具示例
单个工具示例
JSON
get_weather 函数,并在新的 user 消息中返回结果:JSON
并行工具使用
并行工具使用
多个工具示例
多个工具示例
您可以在单个请求中为 Claude 提供多个工具供其选择。以下是一个同时包含 在这种情况下,Claude 可能会:
get_weather 和 get_time 工具的示例,以及一个询问两者的用户查询。- 顺序使用工具(一次一个)— 先调用
get_weather,然后在收到天气结果后调用get_time - 使用并行工具调用 — 当操作独立时,在单个响应中输出多个
tool_use块
user 消息中返回所有工具结果,每个结果都在自己的 tool_result 块中。缺少信息
缺少信息
如果用户的提示不包含足够的信息来填充工具的所有必需参数,Claude Opus 更有可能识别出缺少参数并询问。Claude Sonnet 可能会询问,特别是在提示它在输出工具请求之前先思考时。但它也可能尽力推断一个合理的值。例如,使用上面的 这种行为不能保证,特别是对于更模糊的提示和不太智能的模型。如果 Claude Opus 没有足够的上下文来填写必需的参数,它更有可能用澄清问题而不是进行工具调用来响应。
get_weather 工具,如果您问 Claude “天气如何?“而不指定位置,Claude,特别是 Claude Sonnet,可能会对工具输入进行猜测:JSON
顺序工具
顺序工具
某些任务可能需要按顺序调用多个工具,使用一个工具的输出作为另一个工具的输入。在这种情况下,Claude 将一次调用一个工具。如果提示一次调用所有工具,如果下游工具依赖于上游工具的工具结果,Claude 很可能会为下游工具猜测参数。以下是使用 在这种情况下,Claude 首先会调用
此示例演示了 Claude 如何将多个工具调用链接在一起,以回答需要从不同来源收集数据的问题。关键步骤是:
get_location 工具获取用户位置,然后将该位置传递给 get_weather 工具的示例:get_location 工具来获取用户的位置。在您在 tool_result 中返回位置后,Claude 然后会使用该位置调用 get_weather 来获得最终答案。完整的对话可能如下所示:| 角色 | 内容 |
|---|---|
| 用户 | 我所在地方的天气如何? |
| 助手 | 我先找到您的当前位置,然后检查那里的天气。[使用 get_location 工具] |
| 用户 | [get_location 的工具结果,匹配 id 和结果为 San Francisco, CA] |
| 助手 | [使用 get_weather 工具,输入如下]{ “location”: “San Francisco, CA”, “unit”: “fahrenheit” } |
| 用户 | [get_weather 的工具结果,匹配 id 和结果为 “59°F (15°C), mostly cloudy”] |
| 助手 | 根据您在加利福尼亚州旧金山的当前位置,现在的天气是 59°F (15°C),大部分多云。这是一个相当凉爽和阴天的日子。如果您要外出,可能需要带一件轻便的夹克。 |
- Claude 首先意识到它需要用户的位置来回答天气问题,因此它调用
get_location工具。 - 用户(即客户端代码)执行实际的
get_location函数并在tool_result块中返回结果”San Francisco, CA”。 - 现在知道了位置,Claude 继续调用
get_weather工具,传入”San Francisco, CA”作为location参数(以及猜测的unit参数,因为unit不是必需参数)。 - 用户再次使用提供的参数执行实际的
get_weather函数,并在另一个tool_result块中返回天气数据。 - 最后,Claude 将天气数据合并到对原始问题的自然语言响应中。
思维链工具使用
思维链工具使用
默认情况下,Claude Opus 被提示在回答工具使用查询之前先思考,以最好地确定是否需要工具、使用哪个工具以及适当的参数。Claude Sonnet 和 Claude Haiku 被提示尽可能多地尝试使用工具,更有可能调用不必要的工具或推断缺失的参数。要提示 Sonnet 或 Haiku 在进行工具调用之前更好地评估用户查询,可以使用以下提示:思维链提示
使用相关工具(如果可用)回答用户的请求。在调用工具之前,进行一些分析。首先,考虑提供的工具中哪个是回答用户请求的相关工具。其次,遍历相关工具的每个必需参数,并确定用户是否直接提供或给出了足够的信息来推断值。在决定是否可以推断参数时,仔细考虑所有上下文,看它是否支持特定值。如果所有必需参数都存在或可以合理推断,则继续进行工具调用。但是,如果必需参数的值之一缺失,请不要调用函数(即使用缺失参数的填充值也不行),而是要求用户提供缺失的参数。如果未提供可选参数,请不要要求更多信息。 JSON 模式
JSON 模式
您可以使用工具让 Claude 产生遵循模式的 JSON 输出,即使您没有任何意图通过工具或函数运行该输出。以这种方式使用工具时:
- 您通常希望提供单个工具
- 您应该设置
tool_choice(参见强制工具使用)来指示模型明确使用该工具 - 请记住,模型将把
input传递给工具,因此工具的名称和描述应该从模型的角度来看。
record_summary 工具按特定格式描述图像。定价
Tool use requests are priced based on:- The total number of input tokens sent to the model (including in the
toolsparameter) - The number of output tokens generated
- For server-side tools, additional usage-based pricing (e.g., web search charges per search performed)
- The
toolsparameter in API requests (tool names, descriptions, and schemas) tool_usecontent blocks in API requests and responsestool_resultcontent blocks in API requests
tools, we also automatically include a special system prompt for the model which enables tool use. The number of tool use tokens required for each model are listed below (excluding the additional tokens listed above). Note that the table assumes at least 1 tool is provided. If no tools are provided, then a tool choice of none uses 0 additional system prompt tokens.
| Model | Tool choice | Tool use system prompt token count |
|---|---|---|
| Claude Opus 4.1 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 4.5 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 4 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 3.7 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 3.5 (Oct) (deprecated) | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 3.5 (June) (deprecated) | auto, noneany, tool | 294 tokens 261 tokens |
| Claude Haiku 4.5 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Haiku 3.5 | auto, noneany, tool | 264 tokens 340 tokens |
| Claude Opus 3 (deprecated) | auto, noneany, tool | 530 tokens 281 tokens |
| Claude Sonnet 3 | auto, noneany, tool | 159 tokens 235 tokens |
| Claude Haiku 3 | auto, noneany, tool | 264 tokens 340 tokens |
usage 指标的一部分。