Claude Tool Use(工具調用 / Function Calling)入門:定義工具與返回 tool_result

Claude API 工具調用(Function Calling)入門:工具定義三要素、tool_use / tool_result 往返循環、Python 完整示例、is_error、tool runner、伺服器工具,以及常見報錯原因。

NNathaniel bigo··原創首發·AI 輔助撰寫
10 分鐘讀完
資料核對於 2026-10-07 · 依據官方文檔與公開資料整理 其他 帳號

結論先說

  1. 模型自己從不執行任何東西:它只會返回一個結構化的「我要調用某某工具、參數是這些」的請求(tool_use 塊),由你的代碼去執行,再把結果用 tool_result 塊發回去。
  2. 工具定義三要素:name(名稱)、description(說明)、input_schema(JSON Schema 格式的參數)。官方說描述寫得詳細是影響效果的最重要因素,每個工具建議至少寫 3~4 句。
  3. 標準循環:只要響應的 stop_reason 是 "tool_use",就執行工具、把結果發回、繼續請求;變成 "end_turn" 等其他值時結束。
  4. tool_result 必須緊跟在對應的 tool_use 之後,並且在用戶消息裏排在最前面;每個 tool_use 都要有一個對應的 tool_result。違反這兩條就會出現上面那個報錯。
  5. 不想自己寫循環,可以用 SDK 的 tool runner(beta);網頁搜尋、代碼執行等伺服器工具由 Anthropic 執行,你不用寫 tool_result。
本文根據 Claude API 官方文檔的 Tool use 系列頁面整理,核對日期 2026-10-07。示例代碼基於官方 get_weather 示例改寫。

適用於誰

  • 想讓 Claude 查數據庫、調用內部接口、發電郵,也就是「讓模型調用我的函數」的開發者;
  • 搜「claude tool use」「claude function calling example」的人;
  • 遇到報錯「tool_use ids were found without tool_result blocks immediately after」的人。

基礎調用方式詳見本站《Claude API Python 入門:安裝 SDK、第一個請求、流式輸出與多輪對話》。

一、三類工具

類型誰定義 schema誰執行例子
自定義工具你你的代碼查訂單、調內部 API
Anthropic 定義 schema 的客戶端工具Anthropic你的代碼memory、bash、text_editor、computer use、browser use
伺服器工具AnthropicAnthropicweb_search、web_fetch、code_execution、tool_search

第二類的好處是:模型針對這些固定的工具簽名專門訓練過,調用更可靠。

二、定義一個工具

{
  "name": "get_weather",
  "description": "查詢指定城市當前的天氣。當用戶詢問某地現在的天氣、氣溫時使用。只返回當前天氣,不提供預報。",
  "input_schema": {
    "type": "object",
    "properties": {
      "location": {"type": "string", "description": "城市名,例如:上海"},
      "unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "description": "溫度單位"}
    },
    "required": ["location"]
  }
}
  • name 只能用字母、數字、下劃線和短橫線,最長 128 個字符;
  • 官方的寫法建議:說清楚工具做什麼、什麼時候用(以及什麼時候不用)、每個參數的含義、有什麼限制;相關操作合併成少量工具(用一個 action 參數區分),而不是一個動作一個工具;跨多個服務時給工具名加前綴(如 github_list_prs);工具返回只給 Claude 需要的高價值資訊;
  • 想保證參數一定符合 schema,可以在工具定義裏加 strict: true(嚴格工具調用)。

三、完整的調用循環(Python)

import json
import anthropic

client = anthropic.Anthropic()

tools = [{
    "name": "get_weather",
    "description": "查詢指定城市當前的天氣。當用戶詢問某地現在的天氣、氣溫時使用。",
    "input_schema": {
        "type": "object",
        "properties": {"location": {"type": "string", "description": "城市名"}},
        "required": ["location"],
    },
}]

def get_weather(location: str) -> str:
    # 這裏換成你真實的天氣接口
    return json.dumps({"location": location, "temperature": "22°C", "condition": "多雲"}, ensure_ascii=False)

messages = [{"role": "user", "content": "杭州現在天氣怎麼樣?"}]

while True:
    response = client.messages.create(
        model="claude-opus-5-5",
        max_tokens=1024,
        tools=tools,
        messages=messages,
    )
    # 1. 把助手這一輪的完整回覆(含 tool_use 塊)加入歷史
    messages.append({"role": "assistant", "content": response.content})

    if response.stop_reason != "tool_use":
        break  # end_turn / max_tokens 等:結束循環

    # 2. 執行每一個 tool_use,收集 tool_result
    results = []
    for block in response.content:
        if block.type == "tool_use":
            try:
                output = get_weather(**block.input)
                results.append({"type": "tool_result", "tool_use_id": block.id, "content": output})
            except Exception as e:
                results.append({"type": "tool_result", "tool_use_id": block.id,
                                "content": f"查詢失敗:{e},請換個說法或稍後再試", "is_error": True})

    # 3. 把所有結果放在同一條 user 消息裏發回去
    messages.append({"role": "user", "content": results})

print("".join(b.text for b in response.content if b.type == "text"))

循環的關鍵點(官方要求):

  • tool_use 塊裏有三樣東西:id(用來對應結果)、name、input(符合你 schema 的參數);
  • tool_result 裏填 tool_use_id、content(字符串,或文本 / 圖片 / 文檔內容塊列表),出錯時加 "is_error": true;
  • Claude 一次可能並行調用多個工具:要把多個 tool_result 放在同一條 user 消息裏發回,而不是一條結果一輪;
  • 同一條 user 消息裏如果還想附加文字,文字必須放在所有 tool_result 之後。

四、出錯了怎麼告訴 Claude

工具執行失敗時,把錯誤訊息放進 content 並設 is_error: true,Claude 會據此給用戶解釋或換個方式重試。官方建議錯誤訊息寫得有指導性:不要只寫「失敗」,而是寫清楚出了什麼問題、下一步該怎麼做(例如「超出頻率限制,60 秒後重試」)。

參數缺失之類的無效調用,也可以用帶 is_error 的結果告訴它缺了什麼,官方說明 Claude 通常會修正後再試 2~3 次。

五、讓 SDK 幫你跑循環:tool runner

Python SDK 提供了 tool runner(beta),用裝飾器從函數簽名和文檔字符串自動生成 schema,並自動處理調用循環:

import json
from anthropic import Anthropic, beta_tool

client = Anthropic()

@beta_tool
def get_weather(location: str, unit: str = "celsius") -> str:
    """查詢指定城市當前的天氣。

    Args:
        location: 城市名,例如 上海
        unit: 溫度單位,celsius 或 fahrenheit
    """
    return json.dumps({"temperature": "20°C", "condition": "晴"}, ensure_ascii=False)

runner = client.beta.messages.tool_runner(
    model="claude-opus-5-5",
    max_tokens=1024,
    tools=[get_weather],
    messages=[{"role": "user", "content": "巴黎現在天氣如何?"}],
)
for message in runner:
    print(message)

需要人工審批、自定義日誌或按條件執行時,官方建議還是用第三節的手寫循環。

六、伺服器工具:不用寫 tool_result

以網頁搜尋為例,只要在 tools 裏聲明,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": "火星車最近有什麼新進展?"}],
)

響應裏會出現 server_tool_use 塊記錄它做了什麼。伺服器工具有內部循環次數上限,達到上限時 stop_reason 會是 "pause_turn",把對話(含這次的響應)原樣再發一次即可讓它繼續。伺服器工具除了 token 費用,還可能按使用次數另外計費(如網頁搜尋按搜尋次數),見官方定價。

七、控制 Claude 用不用工具

  • 預設 tool_choice 是 {"type": "auto"}:Claude 自己判斷。想讓它更積極地用工具,可以在系統提示裏寫「回答前先用工具調查」;想更剋制,寫「自行判斷是否需要調用工具」;
  • {"type": "none"}:本次不用工具;
  • {"type": "any"} 或 {"type": "tool", "name": "..."}:強制調用工具。注意官方說明在 Opus 5.5、Sonnet 5.5、Fable 5.1 等新模型上強制調用會返回 400 錯誤,開啟手動擴展思考時也不支持;這些情況下改用 auto 加嚴格工具調用,或用結構化輸出。

用戶沒給全必填參數時,官方提到 Opus 更傾向於反問,Sonnet 有時會自己推測一個值——對關鍵參數,最好在描述裏寫明「缺少時先問用戶」。

常見問題

Q:報錯「tool_use ids were found without tool_result blocks immediately after」?

官方給出的原因有兩個:有的 tool_use id 沒有對應的 tool_result(並行調用了多個工具,你只回了一個);或者 tool_result 不是 user 消息裏的第一個內容塊(前面放了文字)。解決:給助手回覆裏的每一個 tool_use 都返回一個 tool_result,並把它們都放在文字之前;兩條消息之間也不能插入其他消息。

Q:工具調用會多花錢嗎?

會。tools 參數裏的名稱、描述、schema,以及 tool_use、tool_result 塊都算輸入 token;使用工具時 API 還會自動加一段工具調用專用的系統提示(官方頁面列有各模型的 token 數)。

Q:Claude 選錯工具或亂編參數?

官方排查建議:把工具描述寫得更具體(何時用、何時不用);參數格式複雜的可以加 input_examples 示例;需要保證格式時用 strict: true。

Q:工具返回的內容安全嗎?

工具結果往往來自網頁、電郵、用戶上載等你控制不了的來源,可能藏有「提示詞注入」。官方建議把這類不可信內容放在 tool_result 裏,而不是放進 system 提示或普通用戶文本。

Q:想做能自己讀文件、跑命令的智能體?

可以直接用 Claude Agent SDK,它內置了這些工具和循環,詳見本站《Claude Agent SDK 入門:是什麼、怎麼裝、第一個智能體(Python / TypeScript)》。要接入現成的外部工具,也可以看 MCP,詳見本站《MCP 是什麼:Model Context Protocol 入門,以及在 Claude 裏怎麼用》。

參考資料

  • Tool use with Claude(官方):https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview
  • How tool use works(官方):https://platform.claude.com/docs/en/agents-and-tools/tool-use/how-tool-use-works
  • Define tools(官方):https://platform.claude.com/docs/en/agents-and-tools/tool-use/define-tools
  • Handle tool calls(官方):https://platform.claude.com/docs/en/agents-and-tools/tool-use/handle-tool-calls
  • Tool runner (SDK)(官方):https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-runner
  • Troubleshooting tool use(官方):https://platform.claude.com/docs/en/agents-and-tools/tool-use/troubleshooting-tool-use

Nathaniel 的更多內容

  1. 01

    Claude 提示詞緩存(Prompt Caching)入門:緩存時間、價格倍率與命中率

    Claude API 的提示詞緩存能把重複的長前綴(系統提示、工具定義、長文檔、對話歷史)緩存起來,讀緩存只收少量費用。本文講自動緩存與斷點寫法、5 分鐘和 1 小時兩種時長、價格倍率、最短長度、怎麼看命中率以及命中率低的原因。

    Claude0
  2. 02

    Claude API Python 入門:安裝 SDK、第一個請求、流式輸出與多輪對話

    用官方 anthropic Python SDK 調 Claude:安裝與 API Key 配置、第一個 messages.create 請求、system 提示詞、多輪對話(API 無狀態)、流式輸出、錯誤處理與重試、讀取 token 用量。附可運行的命令行聊天示例。

    Claude0
  3. 03

    Claude API Key 怎麼獲取:Claude Console 創建密鑰、充值與用量查看

    Claude API Key 在 Claude Console(platform.claude.com)裏創建。本文講註冊、創建密鑰、三種密鑰類型、預付費額度怎麼買、用量頁怎麼看、密鑰安全,以及 Claude Pro 訂閱為什麼不包含 API。

    Claude0
  4. 04

    Claude Agent SDK 入門:是什麼、怎麼裝、第一個智能體(Python / TypeScript)

    Claude Agent SDK(原 Claude Code SDK)是把 Claude Code 的工具和智能體循環做成的 Python / TypeScript 庫。本文講它和其他方式的區別、安裝認證,並跑通一個自己讀代碼修 bug 的智能體。

    Claude0
  5. 05

    MCP 是什麼:Model Context Protocol 入門,以及在 Claude 裏怎麼用

    MCP(模型上下文協議)是 Anthropic 發起的開放標準,讓 AI 應用統一連接文件、數據庫和各種工具。本文講清它的結構、兩種傳輸方式,以及在 claude.ai、桌面版、Claude Code、API 裏怎麼用。

    Claude0
  6. 06

    Claude Code 權限模式詳解:auto、手動、plan、bypass 與權限規則配置

    Claude Code 六種權限模式各自放行什麼、怎麼用 Shift+Tab 切換、auto 模式為什麼提示不可用、allow / ask / deny 規則怎麼寫,以及 bypass 模式的風險。

    Claude0

同產品的其他教學

  1. 01

    Claude 數據分析教學:上載 CSV / Excel、做圖表與導出結果

    在 claude.ai 裏用「代碼執行與文件創建」做數據分析:先打開開關,上載 CSV / Excel,讓 Claude 清洗數據、做透視和圖表、建模,再導出 Excel / Word / PDF 報告。附 8 條可複製的中文提問和安全注意事項。

    Claude0
  2. 02

    Claude 帳號被封(This organization has been disabled)怎麼辦:官方申訴流程

    Claude 提示帳號被停用、「This organization has been disabled」是什麼原因?本文按官方幫助中心整理:封號的官方理由、怎麼提交申訴、被封后還能導出數據或刪除帳號、組織被暫停怎麼申請複查、訂閱和退款怎麼處理,以及 Claude Code 裏同名報錯的另一種原因。

    Claude0
  3. 03

    Claude Free、Pro、Max 有什麼區別:套餐功能對比(含 Team / Enterprise)

    按官方定價頁和幫助中心整理 Claude 各套餐的區別:Free 能做什麼、Pro 多了哪些功能、Max 與 Ultra(原 Max 20x)差在哪、Team 標準 / 高級席位與 Enterprise 的管理能力,以及 API 額度規則、怎麼選。不含價格。

    Claude0
  4. 04

    Claude Code 命令大全:斜槓命令速查與自定義命令

    按用途整理 Claude Code 常用斜槓命令(上下文、模型、會話、審查、並行、配置),並講清自定義命令怎麼寫:.claude/commands 與 skills 的關係、$ARGUMENTS 傳參。

    Claude0
  5. 05

    Claude Code Hooks 怎麼用:配置示例(完成通知、自動格式化、攔截危險命令)

    Claude Code Hooks 是在固定時機自動執行的命令。本文講清配置寫在哪、事件和 matcher 怎麼選、退出碼 0 / 2 的含義,附通知(含 Windows)、自動格式化、保護文件等可複製示例。

    Claude0
  6. 06

    Claude Code Subagents(子代理)怎麼用:創建、調用與最佳實踐

    Claude Code 子代理是在獨立上下文裏幹活、只交回摘要的專用助手。本文講內置子代理、怎麼創建(.claude/agents)、frontmatter 字段、三種調用方式、指定模型和常見用法。

    Claude0

0 條評論

登入 後參與評論

還沒有評論,來搶沙發~