Claude Platform Docs
  • Messages
  • Managed Agents
  • 管理

Search...
⌘K
第一步
Claude 簡介快速入門
使用 Claude 進行建構
功能概覽使用 Messages API停止原因與備援拒絕與備援備援額度
模型能力
擴展思考自適應思考Effort任務預算(測試版)快速模式(研究預覽)結構化輸出引用串流 Messages批次處理搜尋結果串流拒絕多語言支援嵌入
工具
概覽工具使用的運作方式教學:建構使用工具的代理定義工具處理工具呼叫平行工具使用Tool Runner (SDK)嚴格工具使用工具使用與提示快取伺服器工具疑難排解網頁搜尋工具網頁擷取工具程式碼執行工具顧問工具記憶工具Bash 工具電腦使用工具文字編輯器工具
工具基礎架構
工具參考管理工具上下文工具組合工具搜尋程式化工具呼叫細粒度工具串流
上下文管理
上下文視窗壓縮上下文編輯提示快取對話中系統訊息建構協調模式快取診斷(測試版)Token 計數
處理檔案
Files APIPDF 支援圖片與視覺
技能
概覽快速入門最佳實務企業技能API 中的技能
MCP
遠端 MCP 伺服器MCP 連接器
雲端平台上的 Claude
Amazon BedrockAmazon Bedrock(舊版)AWS 上的 Claude PlatformMicrosoft FoundryVertex AI

Log in
定義工具
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Claude Platform Docs

Solutions

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

Partners

  • Claude on AWS
  • Claude on Google Cloud

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
Messages/工具

定義工具

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

選擇模型

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

對於簡單的工具,請使用 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 對工具的行為和用法有許多疑問。



如需更深入的工具設計指引(整合、命名和回應塑造),請參閱 Writing tools for agents。

提供工具使用範例

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

基本用法

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

import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-4-8",
    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 結構描述的用戶端工具,但不適用於網頁搜尋或程式碼執行等伺服器工具
  • Token 成本 - 範例會增加提示 token:簡單範例約 20-50 個 token,複雜的巢狀物件約 100-200 個 token

控制 Claude 的輸出

強制使用工具

在某些情況下,您可能希望 Claude 使用特定工具來回答使用者的問題,即使 Claude 原本會直接回答而不呼叫工具。您可以透過在請求的 tool_choice 欄位中指定工具來達成此目的。醒目標示的行是與標準工具使用請求的唯一差異:

import anthropic

client = anthropic.Anthropic()

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",
                }
            },
            "required": ["location"],
        },
    }
]

response = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    tools=tools,
    tool_choice={"type": "tool", "name": "get_weather"},
    messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}],
)

print(response)

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

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

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

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

顯示四個 tool_choice 選項的圖表:auto、any、tool 和 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 通常會在呼叫工具之前說明它正在做什麼,或自然地回應使用者。

例如,給定提示「舊金山現在的天氣如何,那裡現在幾點?」,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 的輸出
  • 強制使用工具
  • 使用工具時的模型回應
  • 後續步驟