Loading...
    • 構建
    • 管理
    • 模型和定價
    • 客戶端 SDK
    • API 參考
    Search...
    ⌘K
    第一步
    Claude 簡介快速開始
    使用 Claude 構建
    功能概覽使用 Messages APIClaude API 技能處理停止原因
    模型功能
    擴展思考自適應思考努力任務預算 (測試版)快速模式 (測試版:研究預覽)結構化輸出引用流式傳輸消息批量處理搜索結果流式傳輸拒絕多語言支持嵌入
    工具
    概覽工具使用如何運作網絡搜索工具網絡獲取工具代碼執行工具顧問工具記憶工具Bash 工具計算機使用工具文本編輯器工具
    工具基礎設施
    工具參考工具搜索程序化工具調用細粒度工具流式傳輸
    上下文管理
    上下文窗口壓縮上下文編輯提示詞緩存令牌計數
    處理文件
    Files APIPDF 支持圖像和視覺
    技能
    概覽快速開始最佳實踐企業技能API 中的技能
    MCP
    遠程 MCP 服務器MCP 連接器
    提示詞工程
    概覽提示詞最佳實踐Console 提示詞工具
    測試和評估
    定義成功並構建評估在 Console 中使用評估工具降低延遲
    加強護欄
    減少幻覺提高輸出一致性緩解越獄減少提示詞洩露
    資源
    詞彙表
    發佈說明
    Claude Platform
    Console
    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?