工具使用讓 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?