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
    Documentation

    定義工具

    指定工具架構、撰寫有效的描述,並控制 Claude 何時呼叫您的工具。

    選擇模型

    對於複雜的工具和模糊的查詢,請使用最新的 Claude Opus (4.7) 模型;它能更好地處理多個工具,並在需要時尋求澄清。

    對於直接的工具,請使用 Claude Haiku 模型,但請注意它們可能會推斷缺失的參數。

    如果使用 Claude 搭配工具使用和擴展思考,請參考擴展思考指南以獲取更多資訊。

    指定客戶端工具

    客戶端工具(Anthropic 架構和使用者定義的工具)在 API 請求的 tools 頂層參數中指定。每個工具定義包括:

    參數描述
    name工具的名稱。必須符合正規表達式 ^[a-zA-Z0-9_-]{1,64}$。
    description工具功能、何時應使用以及其行為方式的詳細純文字描述。
    input_schema定義工具預期參數的 JSON Schema 物件。
    input_examples(選用)範例輸入物件的陣列,幫助 Claude 理解如何使用工具。請參閱提供工具使用範例。

    如需工具定義上所有可用的選用屬性的完整集合,包括 cache_control、strict、defer_loading 和 allowed_callers,請參閱工具參考。

    工具使用系統提示

    當您使用 tools 參數呼叫 Claude API 時,API 會從工具定義、工具配置和任何使用者指定的系統提示構建特殊系統提示。構建的提示旨在指示模型使用指定的工具並為工具提供必要的上下文以正確運作:

    In this environment you have access to a set of tools you can use to answer the user's question.
    {{ FORMATTING INSTRUCTIONS }}
    String and scalar parameters should be specified as is, while lists and objects should use JSON format. Note that spaces for string values are not stripped. The output is not expected to be valid XML and is parsed with regular expressions.
    Here are the functions available in JSONSchema format:
    {{ TOOL DEFINITIONS IN JSON SCHEMA }}
    {{ USER SYSTEM PROMPT }}
    {{ TOOL CONFIGURATION }}

    工具定義的最佳實踐

    為了在使用工具時從 Claude 獲得最佳效能,請遵循以下指南:

    • 提供極其詳細的描述。 這是工具效能中最重要的因素。您的描述應該解釋工具的每個細節,包括:
      • 工具的功能
      • 何時應該使用它(以及何時不應該使用)
      • 每個參數的含義以及它如何影響工具的行為
      • 任何重要的注意事項或限制,例如工具不會返回的資訊(如果工具名稱不清楚)。您能為 Claude 提供關於工具的越多上下文,它在決定何時以及如何使用它們時就會越好。每個工具描述至少應有 3-4 句話,如果工具複雜則更多。
    • 優先考慮描述,但對於複雜工具可考慮使用 input_examples。 清晰的描述最重要,但對於具有複雜輸入、嵌套物件或格式敏感參數的工具,您可以使用 input_examples 欄位提供架構驗證的範例。詳見提供工具使用範例。
    • 將相關操作合併為更少的工具。 與其為每個動作建立單獨的工具(create_pr、review_pr、merge_pr),不如將它們分組到具有 action 參數的單一工具中。更少但功能更強大的工具可減少選擇歧義,使 Claude 更容易導航您的工具表面。
    • 在工具名稱中使用有意義的命名空間。 當您的工具跨越多個服務或資源時,在名稱前加上服務前綴(例如 github_list_prs、slack_send_message)。隨著您的工具庫增長,這使工具選擇變得明確,在使用工具搜尋時尤其重要。
    • 設計工具回應以僅返回高信號資訊。 返回語義、穩定的識別碼(例如 slug 或 UUID),而不是不透明的內部參考,並僅包含 Claude 需要推理其下一步的欄位。臃腫的回應會浪費上下文,使 Claude 更難提取重要內容。

    良好的描述清楚地解釋了工具的功能、何時使用、它返回的資料以及 ticker 參數的含義。不佳的描述過於簡潔,讓 Claude 對工具的行為和使用方式有許多疑問。

    如需更深入的工具設計指導(合併、命名和回應塑形),請參閱為代理編寫工具。

    提供工具使用範例

    您可以提供有效工具輸入的具體範例,幫助 Claude 更有效地理解如何使用您的工具。這對於具有嵌套物件、選用參數或格式敏感輸入的複雜工具特別有用。

    基本用法

    在您的工具定義中新增選用的 input_examples 欄位,其中包含範例輸入物件的陣列。每個範例必須根據工具的 input_schema 有效:

    import anthropic
    
    client = anthropic.Anthropic()
    
    response = client.messages.create(
        model="claude-opus-4-7",
        max_tokens=1024,
        tools=[
            {
                "name": "get_weather",
                "description": "Get the current weather in a given location",
                "input_schema": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g. San Francisco, CA",
                        },
                        "unit": {
                            "type": "string",
                            "enum": ["celsius", "fahrenheit"],
                            "description": "The unit of temperature",
                        },
                    },
                    "required": ["location"],
                },
                "input_examples": [
                    {"location": "San Francisco, CA", "unit": "fahrenheit"},
                    {"location": "Tokyo, Japan", "unit": "celsius"},
                    {
                        "location": "New York, NY"  # 'unit' is optional
                    },
                ],
            }
        ],
        messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}],
    )
    
    print(response)

    範例包含在提示中,與您的工具架構一起,向 Claude 展示格式良好的工具呼叫的具體模式。這幫助 Claude 理解何時包含選用參數、使用什麼格式以及如何構建複雜輸入。

    要求和限制

    • 架構驗證 - 每個範例必須根據工具的 input_schema 有效。無效的範例返回 400 錯誤
    • 不支援伺服器端工具 - 輸入範例適用於使用者定義和 Anthropic 架構的客戶端工具,但不適用於伺服器工具,如網路搜尋或程式碼執行
    • 令牌成本 - 範例增加提示令牌:簡單範例約 20-50 個令牌,複雜嵌套物件約 100-200 個令牌

    控制 Claude 的輸出

    強制工具使用

    在某些情況下,您可能希望 Claude 使用特定工具來回答使用者的問題,即使 Claude 在沒有呼叫工具的情況下會直接回答。您可以通過在 tool_choice 欄位中指定工具來執行此操作,如下所示:

    tool_choice = {"type": "tool", "name": "get_weather"}

    使用 tool_choice 參數時,有四個可能的選項:

    • auto 允許 Claude 決定是否呼叫任何提供的工具。這是提供 tools 時的預設值。
    • any 告訴 Claude 它必須使用提供的工具之一,但不強制特定工具。
    • tool 強制 Claude 始終使用特定工具。
    • none 防止 Claude 使用任何工具。這是未提供 tools 時的預設值。

    使用提示快取時,對 tool_choice 參數的更改將使快取的訊息區塊失效。工具定義和系統提示保持快取,但訊息內容必須重新處理。

    此圖表說明了每個選項的工作方式:

    Diagram showing the four tool_choice options: auto, any, tool, and none

    請注意,當您將 tool_choice 設為 any 或 tool 時,API 會預填助手訊息以強制使用工具。這意味著即使明確要求,模型也不會在 tool_use 內容區塊之前發出自然語言回應或解釋。

    使用擴展思考搭配工具使用時,不支援 tool_choice: {"type": "any"} 和 tool_choice: {"type": "tool", "name": "..."} 並將導致錯誤。只有 tool_choice: {"type": "auto"} (預設)和 tool_choice: {"type": "none"} 與擴展思考相容。

    Claude Mythos Preview 不支援強制工具使用。使用 tool_choice: {"type": "any"} 或 tool_choice: {"type": "tool", "name": "..."} 的請求在此模型上返回 400 錯誤。使用 tool_choice: {"type": "auto"} (預設)或 tool_choice: {"type": "none"} 並依賴提示來影響工具選擇。

    測試表明這不應該降低效能。如果您希望模型在仍然請求模型使用特定工具的同時提供自然語言上下文或解釋,您可以為 tool_choice 使用 {"type": "auto"} (預設)並在 user 訊息中新增明確的指示。例如:What's the weather like in London? Use the get_weather tool in your response.

    使用嚴格工具保證工具呼叫

    將 tool_choice: {"type": "any"} 與嚴格工具使用結合,以保證您的工具之一將被呼叫,並且工具輸入嚴格遵循您的架構。在您的工具定義上設定 strict: true 以啟用架構驗證。

    使用工具的模型回應

    使用工具時,Claude 通常會在呼叫工具之前評論它正在做什麼或自然地回應使用者。

    例如,給定提示「San Francisco 現在的天氣如何,那裡現在幾點?」,Claude 可能會回應:

    JSON
    {
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": "I'll help you check the current weather and time in San Francisco."
        },
        {
          "type": "tool_use",
          "id": "toolu_01A09q90qw90lq917835lq9",
          "name": "get_weather",
          "input": { "location": "San Francisco, CA" }
        }
      ]
    }

    這種自然的回應風格幫助使用者理解 Claude 正在做什麼,並創建更具對話性的互動。您可以通過您的系統提示和在提示中提供 <examples> 來指導這些回應的風格和內容。

    重要的是要注意,Claude 在解釋其操作時可能使用各種措辭和方法。您的程式碼應該像對待任何其他助手生成的文字一樣對待這些回應,並且不應依賴特定的格式化慣例。

    後續步驟

    處理工具呼叫

    解析 tool_use 區塊並格式化 tool_result 回應。

    Tool Runner (SDK)

    讓 SDK 自動處理代理迴圈。

    工具參考

    Anthropic 提供的工具和選用屬性的目錄。

    Was this page helpful?

    • 控制 Claude 的輸出