Claude 能夠與工具和函數互動,讓您擴展 Claude 的能力以執行更多樣化的任務。

通過我們新的全面性工具使用課程學習掌握 Claude 工具使用所需的一切知識!請繼續使用此表單分享您的想法和建議。

以下是如何使用 Messages API 向 Claude 提供工具的示例:

curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-opus-4-20250514",
    "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"
            }
          },
          "required": ["location"]
        }
      }
    ],
    "messages": [
      {
        "role": "user",
        "content": "What is the weather like in San Francisco?"
      }
    ]
  }'

工具使用的工作原理

Claude 支持兩種類型的工具:

  1. 客戶端工具:在您的系統上執行的工具,包括:

  2. 服務器工具:在 Anthropic 的服務器上執行的工具,如網絡搜索工具。這些工具必須在 API 請求中指定,但不需要您自己實現。

Anthropic 定義的工具使用版本化類型(例如 web_search_20250305text_editor_20250124)以確保跨模型版本的兼容性。

客戶端工具

通過以下步驟將客戶端工具與 Claude 集成:

1

向 Claude 提供工具和用戶提示

  • 在 API 請求中定義客戶端工具,包括名稱、描述和輸入模式。
  • 包含可能需要這些工具的用戶提示,例如「舊金山的天氣如何?」
2

Claude 決定使用工具

  • Claude 評估是否有任何工具可以幫助解答用戶的查詢。
  • 如果是,Claude 會構建格式正確的工具使用請求。
  • 對於客戶端工具,API 響應的 stop_reasontool_use,表示 Claude 的意圖。
3

執行工具並返回結果

  • 從 Claude 的請求中提取工具名稱和輸入
  • 在您的系統上執行工具代碼
  • 在包含 tool_result 內容塊的新 user 消息中返回結果
4

Claude 使用工具結果來制定回應

  • Claude 分析工具結果,為原始用戶提示制定最終回應。

注意:步驟 3 和 4 是可選的。對於某些工作流程,Claude 的工具使用請求(步驟 2)可能就是您所需的全部內容,無需將結果發送回 Claude。

服務器工具

服務器工具遵循不同的工作流程:

1

向 Claude 提供工具和用戶提示

  • 服務器工具,如網絡搜索,有自己的參數。
  • 包含可能需要這些工具的用戶提示,例如「搜索有關 AI 的最新新聞。」
2

Claude 執行服務器工具

  • Claude 評估服務器工具是否可以幫助解答用戶的查詢。
  • 如果是,Claude 執行該工具,結果會自動納入 Claude 的回應中。
3

Claude 使用服務器工具結果來制定回應

  • Claude 分析服務器工具結果,為原始用戶提示制定最終回應。
  • 服務器工具執行不需要額外的用戶互動。

工具使用示例

以下是展示各種工具使用模式和技術的幾個代碼示例。為了簡潔起見,這些工具都是簡單工具,工具描述比理想情況下確保最佳性能的描述要短。


定價

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 4auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 4auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 3.7auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 3.5 (Oct)auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 3.5 (June)auto, none
any, tool
294 tokens
261 tokens
Claude Haiku 3.5auto, none
any, tool
264 tokens
340 tokens
Claude Opus 3auto, 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 指標的一部分。


下一步

在我們的食譜中探索現成可實現的工具使用代碼示例: