Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K

    第一步

    Claude 介紹快速入門

    模型與定價

    模型概覽選擇模型Claude 4.5 的新功能遷移到 Claude 4.5模型棄用定價

    使用 Claude 建構

    功能概覽使用 Messages API上下文視窗提示詞最佳實踐

    功能

    提示詞快取上下文編輯延伸思考串流訊息批次處理引用多語言支援Token 計數嵌入向量視覺PDF 支援Files API搜尋結果Google Sheets 附加元件

    工具

    概述如何實現工具使用代幣高效工具使用細粒度工具串流Bash 工具代碼執行工具電腦使用工具文字編輯工具網頁擷取工具網路搜尋工具記憶工具

    代理技能

    概述在 API 中開始使用 Agent Skills技能編寫最佳實踐使用 Agent Skills 與 API

    Agent SDK

    概述Agent SDK 參考 - TypeScriptPython SDK

    指南

    串流輸入處理權限會話管理託管 Agent SDK修改系統提示SDK 中的 MCP自訂工具SDK 中的子代理SDK 中的斜線命令SDK 中的代理技能追蹤成本和使用量待辦事項清單SDK 中的外掛程式

    API 中的 MCP

    MCP 連接器遠端 MCP 伺服器

    Claude 在第三方平台上

    Amazon BedrockVertex AI

    提示工程

    概述提示詞生成器使用提示模板提示詞改進器保持清晰和直接使用範例(多樣提示)讓 Claude 思考(思維鏈)使用 XML 標籤給 Claude 分配角色(系統提示詞)預填 Claude 的回應串接複雜提示長文本技巧延伸思考技巧

    測試與評估

    定義成功標準開發測試案例使用評估工具降低延遲

    加強防護措施

    減少幻覺提高輸出一致性防範越獄handle-streaming-refusals減少提示詞洩漏保持 Claude 的角色特性

    管理和監控

    Admin API 概述使用量和成本 APIClaude Code 分析 API
    Console
    工具

    使用 Claude 進行工具使用

    Claude 能夠與工具和函數互動,讓您能夠擴展 Claude 的能力來執行更廣泛的任務。

    在我們的新課程中學習掌握 Claude 工具使用所需的一切!請 繼續使用此表單分享您的想法和建議。

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

    Shell
    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-sonnet-4-5",
        "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?"
          }
        ]
      }'
    Python
    import anthropic
    
    client = anthropic.Anthropic()
    
    response = client.messages.create(
        model="claude-sonnet-4-5",
        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's the weather like in San Francisco?"}],
    )
    print(response)
    TypeScript
    import { Anthropic } from '@anthropic-ai/sdk';
    
    const anthropic = new Anthropic({
      apiKey: process.env.ANTHROPIC_API_KEY
    });
    
    async function main() {
      const response = await anthropic.messages.create({
        model: "claude-sonnet-4-5",
        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: "Tell me the weather in San Francisco." 
        }]
      });
    
      console.log(response);
    }
    
    main().catch(console.error);
    Java
    import java.util.List;
    import java.util.Map;
    
    import com.anthropic.client.AnthropicClient;
    import com.anthropic.client.okhttp.AnthropicOkHttpClient;
    import com.anthropic.core.JsonValue;
    import com.anthropic.models.messages.Message;
    import com.anthropic.models.messages.MessageCreateParams;
    import com.anthropic.models.messages.Model;
    import com.anthropic.models.messages.Tool;
    import com.anthropic.models.messages.Tool.InputSchema;
    
    public class GetWeatherExample {
    
        public static void main(String[] args) {
            AnthropicClient client = AnthropicOkHttpClient.fromEnv();
    
            InputSchema schema = InputSchema.builder()
                    .properties(JsonValue.from(Map.of(
                            "location",
                            Map.of(
     "type", "string",
     "description", "The city and state, e.g. San Francisco, CA"))))
                    .putAdditionalProperty("required", JsonValue.from(List.of("location")))
                    .build();
    
            MessageCreateParams params = MessageCreateParams.builder()
                    .model(Model.CLAUDE_OPUS_4_0)
                    .maxTokens(1024)
                    .addTool(Tool.builder()
                            .name("get_weather")
                            .description("Get the current weather in a given location")
                            .inputSchema(schema)
                            .build())
                    .addUserMessage("What's the weather like in San Francisco?")
                    .build();
    
            Message message = client.messages().create(params);
            System.out.println(message);
        }
    }

    工具使用的運作方式

    Claude 支援兩種類型的工具:

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

      • 您創建和實作的使用者定義自訂工具
      • Anthropic 定義的工具,如電腦使用和文字編輯器,需要客戶端實作
    2. 伺服器工具:在 Anthropic 伺服器上執行的工具,如網路搜尋和網路擷取工具。這些工具必須在 API 請求中指定,但不需要您進行實作。

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

    客戶端工具

    透過以下步驟將客戶端工具與 Claude 整合:

    1. 1

      為 Claude 提供工具和使用者提示

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

      Claude 決定使用工具

      • Claude 評估是否有任何工具可以幫助處理使用者的查詢。
      • 如果有,Claude 會構建格式正確的工具使用請求。
      • 對於客戶端工具,API 回應的 stop_reason 為 tool_use,表示 Claude 的意圖。
    3. 3

      執行工具並返回結果

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

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

      • Claude 分析工具結果以制定對原始使用者提示的最終回應。

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

    伺服器工具

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

    1. 1

      為 Claude 提供工具和使用者提示

      • 伺服器工具,如網路搜尋和網路擷取,有自己的參數。
      • 包含可能需要這些工具的使用者提示,例如「搜尋關於 AI 的最新新聞」或「分析此 URL 的內容」。
    2. 2

      Claude 執行伺服器工具

      • Claude 評估伺服器工具是否可以幫助處理使用者的查詢。
      • 如果可以,Claude 執行工具,結果會自動整合到 Claude 的回應中。
    3. 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 4.1auto, none
    any, tool
    346 tokens
    313 tokens
    Claude Opus 4auto, none
    any, tool
    346 tokens
    313 tokens
    Claude Sonnet 4.5auto, none
    any, tool
    346 tokens
    313 tokens
    Claude Sonnet 4auto, none
    any, tool
    346 tokens
    313 tokens
    Claude Sonnet 3.7 (deprecated)auto, none
    any, tool
    346 tokens
    313 tokens
    Claude Haiku 4.5auto, none
    any, tool
    346 tokens
    313 tokens
    Claude Haiku 3.5auto, none
    any, tool
    264 tokens
    340 tokens
    Claude Opus 3 (deprecated)auto, 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 指標的一部分。


    下一步

    探索我們在食譜中準備實作的工具使用程式碼範例儲存庫:

    計算器工具

    學習如何將簡單的計算器工具與 Claude 整合以進行精確的數值計算。

    客戶服務代理

    建立一個響應式客戶服務機器人,利用客戶端工具來 增強支援。

    JSON 提取器

    了解 Claude 和工具使用如何從非結構化文字中提取結構化資料。

      © 2025 ANTHROPIC PBC

      Products

      • Claude
      • Claude Code
      • Max plan
      • Team plan
      • Enterprise plan
      • Download app
      • Pricing
      • Log in

      Features

      • Claude and Slack
      • Claude in Excel

      Models

      • Opus
      • Sonnet
      • Haiku

      Solutions

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

      Claude Developer Platform

      • Overview
      • Developer docs
      • Pricing
      • Amazon Bedrock
      • Google Cloud’s Vertex AI
      • Console login

      Learn

      • Blog
      • Catalog
      • 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

      Help and security

      • Availability
      • Status
      • Support center

      Terms and policies

      • Privacy policy
      • Responsible disclosure policy
      • Terms of service: Commercial
      • Terms of service: Consumer
      • Usage policy

      Products

      • Claude
      • Claude Code
      • Max plan
      • Team plan
      • Enterprise plan
      • Download app
      • Pricing
      • Log in

      Features

      • Claude and Slack
      • Claude in Excel

      Models

      • Opus
      • Sonnet
      • Haiku

      Solutions

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

      Claude Developer Platform

      • Overview
      • Developer docs
      • Pricing
      • Amazon Bedrock
      • Google Cloud’s Vertex AI
      • Console login

      Learn

      • Blog
      • Catalog
      • 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

      Help and security

      • Availability
      • Status
      • Support center

      Terms and policies

      • Privacy policy
      • Responsible disclosure policy
      • Terms of service: Commercial
      • Terms of service: Consumer
      • Usage policy
      © 2025 ANTHROPIC PBC