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
    功能

    Token 計數

    Token 計數讓您能夠在將訊息發送給 Claude 之前確定訊息中的 token 數量,幫助您對提示和使用做出明智的決策。透過 token 計數,您可以

    • 主動管理速率限制和成本
    • 做出智慧的模型路由決策
    • 優化提示以達到特定長度

    如何計算訊息 token

    token 計數端點接受與建立訊息相同的結構化輸入列表,包括支援系統提示、工具、圖像和 PDF。回應包含輸入 token 的總數。

    token 計數應被視為估計值。在某些情況下,建立訊息時使用的實際輸入 token 數量可能會有少量差異。

    Token 計數可能包括 Anthropic 為系統優化自動添加的 token。您不會為系統添加的 token 付費。計費僅反映您的內容。

    支援的模型

    所有活躍模型都支援 token 計數。

    計算基本訊息中的 token

    Python
    import anthropic
    
    client = anthropic.Anthropic()
    
    response = client.messages.count_tokens(
        model="claude-sonnet-4-5",
        system="You are a scientist",
        messages=[{
            "role": "user",
            "content": "Hello, Claude"
        }],
    )
    
    print(response.json())
    TypeScript
    import Anthropic from '@anthropic-ai/sdk';
    
    const client = new Anthropic();
    
    const response = await client.messages.countTokens({
      model: 'claude-sonnet-4-5',
      system: 'You are a scientist',
      messages: [{
        role: 'user',
        content: 'Hello, Claude'
      }]
    });
    
    console.log(response);
    Shell
    curl https://api.anthropic.com/v1/messages/count_tokens \
        --header "x-api-key: $ANTHROPIC_API_KEY" \
        --header "content-type: application/json" \
        --header "anthropic-version: 2023-06-01" \
        --data '{
          "model": "claude-sonnet-4-5",
          "system": "You are a scientist",
          "messages": [{
            "role": "user",
            "content": "Hello, Claude"
          }]
        }'
    Java
    import com.anthropic.client.AnthropicClient;
    import com.anthropic.client.okhttp.AnthropicOkHttpClient;
    import com.anthropic.models.messages.MessageCountTokensParams;
    import com.anthropic.models.messages.MessageTokensCount;
    import com.anthropic.models.messages.Model;
    
    public class CountTokensExample {
    
        public static void main(String[] args) {
            AnthropicClient client = AnthropicOkHttpClient.fromEnv();
    
            MessageCountTokensParams params = MessageCountTokensParams.builder()
                    .model(Model.CLAUDE_SONNET_4_20250514)
                    .system("You are a scientist")
                    .addUserMessage("Hello, Claude")
                    .build();
    
            MessageTokensCount count = client.messages().countTokens(params);
            System.out.println(count);
        }
    }
    JSON
    { "input_tokens": 14 }

    計算包含工具的訊息中的 token

    伺服器工具 token 計數僅適用於第一次採樣呼叫。

    import anthropic
    
    client = anthropic.Anthropic()
    
    response = client.messages.count_tokens(
        model="claude-sonnet-4-5",
        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.json())
    JSON
    { "input_tokens": 403 }

    計算包含圖像的訊息中的 token

    #!/bin/sh
    
    IMAGE_URL="https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
    IMAGE_MEDIA_TYPE="image/jpeg"
    IMAGE_BASE64=$(curl "$IMAGE_URL" | base64)
    
    curl https://api.anthropic.com/v1/messages/count_tokens \
         --header "x-api-key: $ANTHROPIC_API_KEY" \
         --header "anthropic-version: 2023-06-01" \
         --header "content-type: application/json" \
         --data \
    '{
        "model": "claude-sonnet-4-5",
        "messages": [
            {"role": "user", "content": [
                {"type": "image", "source": {
                    "type": "base64",
                    "media_type": "'$IMAGE_MEDIA_TYPE'",
                    "data": "'$IMAGE_BASE64'"
                }},
                {"type": "text", "text": "Describe this image"}
            ]}
        ]
    }'
    JSON
    { "input_tokens": 1551 }

    計算包含延伸思考的訊息中的 token

    請參閱這裡了解更多關於延伸思考如何計算上下文視窗的詳細資訊

    • 來自先前助理回合的思考區塊會被忽略,不會計入您的輸入 token
    • 當前助理回合的思考會計入您的輸入 token
    curl https://api.anthropic.com/v1/messages/count_tokens \
        --header "x-api-key: $ANTHROPIC_API_KEY" \
        --header "content-type: application/json" \
        --header "anthropic-version: 2023-06-01" \
        --data '{
          "model": "claude-sonnet-4-5",
          "thinking": {
            "type": "enabled",
            "budget_tokens": 16000
          },
          "messages": [
            {
              "role": "user",
              "content": "Are there an infinite number of prime numbers such that n mod 4 == 3?"
            },
            {
              "role": "assistant",
              "content": [
                {
                  "type": "thinking",
                  "thinking": "This is a nice number theory question. Lets think about it step by step...",
                  "signature": "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV..."
                },
                {
                  "type": "text",
                  "text": "Yes, there are infinitely many prime numbers p such that p mod 4 = 3..."
                }
              ]
            },
            {
              "role": "user",
              "content": "Can you write a formal proof?"
            }
          ]
        }'
    JSON
    { "input_tokens": 88 }

    計算包含 PDF 的訊息中的 token

    Token 計數支援 PDF,具有與 Messages API 相同的限制。

    curl https://api.anthropic.com/v1/messages/count_tokens \
        --header "x-api-key: $ANTHROPIC_API_KEY" \
        --header "content-type: application/json" \
        --header "anthropic-version: 2023-06-01" \
        --data '{
          "model": "claude-sonnet-4-5",
          "messages": [{
            "role": "user",
            "content": [
              {
                "type": "document",
                "source": {
                  "type": "base64",
                  "media_type": "application/pdf",
                  "data": "'$(base64 -i document.pdf)'"
                }
              },
              {
                "type": "text",
                "text": "Please summarize this document."
              }
            ]
          }]
        }'
    JSON
    { "input_tokens": 2188 }

    定價和速率限制

    Token 計數免費使用,但受到基於您的使用層級的每分鐘請求數速率限制。如果您需要更高的限制,請透過 Claude Console 聯繫銷售團隊。

    使用層級每分鐘請求數 (RPM)
    1100
    22,000
    34,000
    48,000

    Token 計數和訊息建立有獨立的速率限制 -- 其中一個的使用不會計入另一個的限制。


    常見問題

    • 如何計算訊息 token
    • 計算基本訊息中的 token
    • 計算包含工具的訊息中的 token
    • 計算包含圖像的訊息中的 token
    • 計算包含延伸思考的訊息中的 token
    • 計算包含 PDF 的訊息中的 token
    © 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