使用 Claude 進行工具使用
將 Claude 連接到外部工具與 API。了解工具在何處執行、Claude 何時呼叫它們,以及哪種工具適合您的任務。
「Tool use」(工具使用),也稱為「function calling」(函式呼叫),讓 Claude 能夠呼叫您定義的函式或 Anthropic 提供的函式。Claude 會根據使用者的請求和工具的描述,決定何時呼叫工具。接著,它會回傳一個結構化的呼叫,由您的應用程式執行(「client tools」,用戶端工具),或由 Anthropic 執行(「server tools」,伺服器工具)。
以下是一個使用伺服器工具的最簡範例。此範例使用網頁搜尋工具,由 Anthropic 為您執行:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5-5",
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)Claude 會在 Anthropic 的基礎架構上執行搜尋,並在同一個回應中回傳附有引用來源的結果。若要讓 Claude 呼叫您定義的函式,請傳入帶有 input_schema 的工具,然後在 Claude 回傳 tool_use 區塊時執行該呼叫。工具使用的運作方式完整展示了這個往返流程。進一步了解如何定義工具和處理工具呼叫。
工具使用的運作方式
工具之間的主要差異在於程式碼的執行位置。用戶端工具(包括使用者定義的工具,以及採用 Anthropic 定義之「schema」(結構描述)的工具,例如 bash 和 text_editor)在您的應用程式中執行。Claude 會以 stop_reason: "tool_use" 以及一個或多個 tool_use 區塊回應。您的程式碼會執行該操作,並傳回 tool_result。伺服器工具(例如 web_search、web_fetch、code_execution 和 tool_search)在 Anthropic 的基礎架構上執行:您可以直接看到結果,無需自行處理執行,除非 Claude 在同一組平行工具呼叫中,同時呼叫了該工具與您的某個用戶端工具(請參閱停止原因與後備處理)。
以下是用戶端工具的完整往返流程。第一個請求定義了 get_weather 工具,Claude 透過呼叫該工具來回答問題:回應中包含一個 tool_use 區塊,您的程式碼執行查詢,然後第二個請求以 tool_result 區塊將結果傳回,讓 Claude 能夠回覆答案。
client = anthropic.Anthropic()
tools = [
{
"name": "get_weather",
"description": "Get the current weather for a given location.",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, e.g. San Francisco, CA",
}
},
"required": ["location"],
},
}
]
messages = [{"role": "user", "content": "What's the weather in San Francisco?"}]
# Claude 會回覆一個 tool_use 區塊,其中指明工具名稱及其引數。
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
tools=tools,
# 每個回合最多只要求一次工具呼叫。
tool_choice={"type": "auto", "disable_parallel_tool_use": True},
messages=messages,
)
tool_use = next(block for block in response.content if block.type == "tool_use")
print(f"Claude called {tool_use.name} with {json.dumps(tool_use.input)}")
# 執行工具,然後以 tool_result 區塊將結果傳回。
weather = "15 degrees Celsius, partly cloudy" # your weather lookup goes here
messages += [
{"role": "assistant", "content": response.content},
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": tool_use.id, "content": weather}
],
},
]
followup = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
tools=tools,
tool_choice={"type": "auto", "disable_parallel_tool_use": True},
messages=messages,
)
# Claude 會使用該結果來回答原本的問題。
final_text = next(block for block in followup.content if block.type == "text")
print(final_text.text)Claude called get_weather with {"location": "San Francisco, CA"}
The current weather in San Francisco is 15 degrees Celsius with partly cloudy skies.處理工具呼叫詳細說明了每個步驟,包括結果格式化和錯誤回報;平行工具使用則說明了一次呼叫多個工具的回應。若不想自行撰寫這個往返流程,請使用 Tool Runner:SDK 會自動執行您的工具並將結果傳回。
如需完整的概念模型,包括「agentic loop」(代理迴圈)以及何時選擇各種方法,請參閱工具使用的運作方式。
若要連接到「Model Context Protocol」(即 MCP)伺服器,請參閱 MCP 連接器。若要建置您自己的 MCP 用戶端,請參閱 Model Context Protocol 指南中的建置 MCP 用戶端。
Claude 何時使用工具
在預設的 tool_choice 為 {"type": "auto"} 時,Claude 會在每一輪判斷要呼叫工具還是直接回應。當請求對應到該工具所描述的能力,且答案尚未存在於上下文中時,它會呼叫工具。對於穩定的知識、創意任務與對話性的輪次,它會直接回應。
這個界線可以透過您的系統提示加以引導。如果 Claude 沒有在您預期的時候呼叫工具,一個輕量的指示,例如 "Use the tools to investigate before responding.",可以增加工具使用。更強烈的形式,例如 "Always call a tool first before responding.",則會進一步推動。相反地,"Use your judgment about whether to call a tool or respond directly." 會讓觸發行為保持保守。
若要強制要求工具呼叫而非依賴提示,請設定 tool_choice。
每個伺服器工具的頁面都更詳細地描述了其自身的觸發界線。
如果使用者的提示未包含足夠的資訊來填入工具的所有必要參數,Claude Opus 更有可能辨識出缺少某個參數並詢問該參數。Claude Sonnet 可能會詢問,尤其是在被提示於輸出工具請求前先思考時。但它也可能推斷出一個合理的值。
例如,給定一個需要 location 參數的 get_weather 工具,如果您詢問 Claude「What's the weather?」而未指定地點,Claude(尤其是 Claude Sonnet)可能會猜測您未提供的值:
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "get_weather",
"input": { "location": "New York, NY", "unit": "fahrenheit" }
}此行為並不保證,尤其是對於較模糊的提示以及能力較弱的模型。
選擇工具
如需 type 字串、版本與 beta 標頭,請參閱工具參考。
您自己的工具
對於您定義的工具,由您撰寫 schema,並由您的應用程式執行每次呼叫。
Anthropic schema 用戶端工具
Anthropic 發布 schema 並以此訓練 Claude。您的應用程式仍需執行每次呼叫並回傳 tool_result。
在您控制的檔案中跨對話儲存與擷取資訊。
在維持狀態的持久工作階段中執行 shell 指令。
檢視與修改文字檔案,以除錯、修正並改進程式碼。
在桌面環境中擷取螢幕截圖並控制滑鼠與鍵盤。
在您自己的瀏覽器環境中導覽、閱讀並與網頁互動。
伺服器工具
伺服器工具在 Anthropic 的基礎設施上執行,您的應用程式中無需處理程式碼。請參閱伺服器工具以了解它們共通的運作機制。
在網路上搜尋超出知識截止日期的資訊,並附上引用來源。
擷取指定網頁與 PDF 文件的完整內容。
在沙箱容器中執行 Python 與 bash 程式碼,以分析資料並產生檔案。
讓較快速的執行者模型在生成過程中諮詢智慧更高的顧問模型。
透過按需探索與載入工具,處理數以千計的工具。
從 Messages API 連接到遠端 MCP 伺服器,無需獨立的 MCP 用戶端。
定價
工具使用(tool use)請求的計價依據如下:
- 傳送至模型的輸入 token 總數(包括
tools參數中的內容) - 產生的輸出 token 數量
- 對於伺服器端工具,另有依使用量計算的額外費用(例如,網頁搜尋依每次執行的搜尋計費)
用戶端工具的計價方式與任何其他 Claude API 請求相同,但伺服器端工具可能會依其特定使用情況產生額外費用。
工具使用所產生的額外 token 來自:
- API 請求中的
tools參數(工具名稱、描述與結構描述) - API 請求與回應中的
tool_use內容區塊 - API 請求中的
tool_result內容區塊
當您使用 tools 時,API 也會自動為模型加入一段特殊的系統提示(system prompt),以啟用工具使用功能。下表列出了每個模型所需的工具使用 token 數量(不包括前述的額外 token)。請注意,此表假設至少提供了 1 個工具。若未提供任何 tools,則工具選擇為 none 時會使用 0 個額外的系統提示 token。
| Model | Tool use system prompt tokens | |
|---|---|---|
| Name | Token count | |
Claude Opus 5.5For long-running agentic coding and knowledge work | auto, none | 286 tokens |
Claude Sonnet 5The best combination of speed and intelligence | auto, none | 354 tokens |
any, tool | 474 tokens | |
Claude Haiku 4.5The fastest model with near-frontier intelligence | auto, none | 496 tokens |
any, tool | 588 tokens | |
auto, none | 286 tokens | |
any, tool | 406 tokens | |
auto, none | 290 tokens | |
any, tool | 410 tokens | |
auto, none | 675 tokens | |
any, tool | 804 tokens | |
auto, none | 497 tokens | |
any, tool | 589 tokens | |
auto, none | 496 tokens | |
any, tool | 588 tokens | |
Claude Opus 4.1 | auto, none | 313 tokens |
any, tool | 315 tokens | |
Claude Opus 4 | auto, none | 313 tokens |
any, tool | 315 tokens | |
auto, none | 497 tokens | |
any, tool | 589 tokens | |
auto, none | 496 tokens | |
any, tool | 588 tokens | |
Claude Sonnet 4 | auto, none | 313 tokens |
any, tool | 315 tokens | |
Claude Haiku 3.5 | auto, none | 264 tokens |
any, tool | 355 tokens | |
這些 token 數量會加到您一般的輸入與輸出 token 中,以計算請求的總費用。
請參閱模型概覽表格以了解目前各模型的價格。
當您傳送工具使用提示時,與任何其他 API 請求一樣,回應會在回報的 usage 指標中同時包含輸入與輸出 token 數量。
部分伺服器工具會在 token 之外加收依用量計費的費用:請參閱 Web search 工具與程式碼執行工具以了解其費率。
後續步驟
了解工具使用迴圈、工具在何處執行,以及何時應使用工具而非散文式回應。
從單一工具呼叫到可用於正式環境的代理迴圈的引導式逐步教學。
Anthropic 提供之工具的目錄,以及選用工具定義屬性的參考。
Was this page helpful?