工具使用让 Claude 调用您定义的函数或 Anthropic 提供的函数。Claude 根据用户的请求和工具的描述决定何时调用工具,然后返回一个结构化的调用,您的应用程序执行(客户端工具)或 Anthropic 执行(服务器工具)。
以下是使用服务器工具的最简单示例,其中 Anthropic 处理执行:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
tools=[{"type": "web_search_20260209", "name": "web_search"}],
messages=[{"role": "user", "content": "What's the latest on the Mars rover?"}],
)
print(response.content)工具主要区别在于代码执行的位置。客户端工具(包括用户定义的工具和 Anthropic 架构工具,如 bash 和 text_editor)在您的应用程序中运行:Claude 响应 stop_reason: "tool_use" 和一个或多个 tool_use 块,您的代码执行操作,然后您发送回 tool_result。服务器工具(web_search、code_execution、web_fetch、tool_search)在 Anthropic 的基础设施上运行:您直接看到结果,无需处理执行。
有关完整的概念模型(包括代理循环和何时选择每种方法),请参阅工具使用如何工作。
有关连接到 MCP 服务器,请参阅 MCP 连接器。有关构建您自己的 MCP 客户端,请参阅 modelcontextprotocol.io。
使用严格工具使用保证架构一致性
将 strict: true 添加到您的工具定义中,以确保 Claude 的工具调用始终与您的架构完全匹配。请参阅严格工具使用。
工具访问是您可以提供给代理的最高杠杆原始操作之一。在 LAB-Bench FigQA(科学图表解释)和 SWE-bench(真实世界软件工程)等基准测试中,即使添加基本工具也会产生超大的能力收益,通常超过人类专家基线。
有关完整的实践演练,请参阅教程。有关各个概念的参考示例,请参阅定义工具和处理工具调用。
Tool use requests are priced based on:
tools parameter)Client-side tools are priced the same as any other Claude API request, while server-side tools may incur additional charges based on their specific usage.
The additional tokens from tool use come from:
tools parameter in API requests (tool names, descriptions, and schemas)tool_use content blocks in API requests and responsestool_result content blocks in API requestsWhen you use 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.7 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4.6 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4.5 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4.1 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 4.6 | 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 (deprecated) | auto, noneany, tool | 346 tokens 313 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 |
These token counts are added to your normal input and output tokens to calculate the total cost of a request.
有关当前的每个模型价格,请参阅模型概览表。
当您发送工具使用提示时,就像任何其他 API 请求一样,响应将输出输入和输出令牌计数,作为报告的 usage 指标的一部分。
Was this page helpful?