Loading...
  • 构建
  • 管理
  • 模型与定价
  • 客户端 SDK
  • API 参考
Search...
⌘K
Log in
概览
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

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

Partners

  • Amazon Bedrock
  • Google Cloud's Vertex AI

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
构建/工具

使用 Claude 进行工具调用

将 Claude 连接到外部工具和 API。了解工具在哪里执行以及代理循环如何工作。

工具使用让 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:

  1. The total number of input tokens sent to the model (including in the tools parameter)
  2. The number of output tokens generated
  3. For server-side tools, additional usage-based pricing (e.g., web search charges per search performed)

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:

  • The tools parameter in API requests (tool names, descriptions, and schemas)
  • tool_use content blocks in API requests and responses
  • tool_result content blocks in API requests

When 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.

ModelTool choiceTool use system prompt token count
Claude Opus 4.7auto, none
any, tool
346 tokens
313 tokens
Claude Opus 4.6auto, none
any, tool
346 tokens
313 tokens
Claude Opus 4.5auto, none
any, tool
346 tokens
313 tokens
Claude Opus 4.1auto, none
any, tool
346 tokens
313 tokens
Claude Opus 4auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 4.6auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 4.5auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 4auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 3.7 (deprecated)auto, none
any, tool
346 tokens
313 tokens
Claude Haiku 4.5auto, none
any, tool
346 tokens
313 tokens
Claude Haiku 3.5auto, none
any, tool
264 tokens
340 tokens
Claude Opus 3 (deprecated)auto, none
any, tool
530 tokens
281 tokens
Claude Sonnet 3auto, none
any, tool
159 tokens
235 tokens
Claude Haiku 3auto, none
any, 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 指标的一部分。


后续步骤

选择您的路径

理解概念

工具在哪里运行、循环如何工作以及何时使用工具。

逐步构建

教程:从单个工具调用到生产。

浏览所有工具

Anthropic 提供的工具和属性目录。

Was this page helpful?