• 訊息
  • 託管代理
  • 管理

Search...
⌘K
第一步
Claude 簡介快速入門
使用 Claude 進行建構
功能概覽使用 Messages API停止原因與備援拒絕與備援備援額度
模型能力
擴展思考自適應思考努力程度任務預算(測試版)快速模式(研究預覽)結構化輸出引用串流訊息批次處理搜尋結果串流拒絕多語言支援嵌入
工具
概覽工具使用的運作方式教學:建構使用工具的代理定義工具處理工具呼叫平行工具使用工具執行器(SDK)嚴格工具使用搭配提示快取的工具使用伺服器工具疑難排解網頁搜尋工具網頁擷取工具程式碼執行工具顧問工具記憶體工具Bash 工具電腦使用工具文字編輯器工具
工具基礎架構
工具參考管理工具上下文工具組合工具搜尋程式化工具呼叫細粒度工具串流
上下文管理
上下文視窗壓縮上下文編輯提示快取對話中系統訊息建構協調模式快取診斷(測試版)Token 計數
處理檔案
Files APIPDF 支援圖片與視覺
技能
概覽快速入門最佳實務企業技能API 中的技能
MCP
遠端 MCP 伺服器MCP 連接器
雲端平台上的 Claude
Amazon BedrockAmazon Bedrock(舊版)AWS 上的 Claude PlatformMicrosoft FoundryVertex AI

Log in
Token 計數
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

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

Partners

  • Amazon Bedrock
  • Google Cloud's Vertex AI

Learn

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

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
訊息/上下文管理

Token 計數

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

  • 主動管理速率限制和成本
  • 做出明智的模型路由決策
  • 將提示最佳化為特定長度


此功能符合「Zero Data Retention」(零資料保留),即 ZDR 的資格。當您的組織具有 ZDR 安排時,透過此功能傳送的資料在 API 回應返回後不會被儲存。


如何計算訊息 token

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



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

Token 計數可能包含 Anthropic 為系統最佳化而自動新增的 token。系統新增的 token 不會向您收費。計費僅反映您的內容。

支援的模型

所有現行模型均支援 token 計數。

計算基本訊息中的 token

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-4-8",
    system="You are a scientist",
    messages=[{"role": "user", "content": "Hello, Claude"}],
)

print(response.json())
Output
{ "input_tokens": 14 }

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



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

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-4-8",
    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())
Output
{ "input_tokens": 403 }

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

import base64
import httpx

image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
image_media_type = "image/jpeg"
image_data = base64.standard_b64encode(httpx.get(image_url).content).decode("utf-8")

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-4-8",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": image_media_type,
                        "data": image_data,
                    },
                },
                {"type": "text", "text": "Describe this image"},
            ],
        }
    ],
)
print(response.json())
Output
{ "input_tokens": 1551 }

計算包含擴展思考的訊息中的 token



請參閱擴展思考如何計算上下文視窗以了解更多詳細資訊

  • 先前助手回合的思考區塊會被忽略,不會計入您的輸入 token
  • 目前助手回合的思考會計入您的輸入 token
client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-sonnet-4-6",
    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. Let's 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?"},
    ],
)

print(response.json())
Output
{ "input_tokens": 88 }

計算包含 PDF 的訊息中的 token



Token 計數支援 PDF,其限制與 Messages API 相同。

import base64
import anthropic

client = anthropic.Anthropic()

with open("document.pdf", "rb") as pdf_file:
    pdf_base64 = base64.standard_b64encode(pdf_file.read()).decode("utf-8")

response = client.messages.count_tokens(
    model="claude-opus-4-8",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "base64",
                        "media_type": "application/pdf",
                        "data": pdf_base64,
                    },
                },
                {"type": "text", "text": "Please summarize this document."},
            ],
        }
    ],
)

print(response.json())
Output
{ "input_tokens": 2188 }

Claude Fable 5 和 Claude Mythos 5 的 Token 計數

Claude Fable 5 和 Claude Mythos 5 使用隨 Claude Opus 4.7 推出的 tokenizer(分詞器),對於相同的文字,其產生的 token 數量比 Claude Opus 4.7 之前的模型多約 30%。Token 計數端點會根據您傳入的 model 所使用的 tokenizer 回傳計數,因此若要測量您工作負載的差異,請對同一個請求計數兩次:一次使用您目前的模型,一次使用 model: "claude-fable-5"(或 "claude-mythos-5"),然後比較兩個 input_tokens 值。



**計費與遷移:**Claude Fable 5 和 Claude Mythos 5 的使用量和計費反映此 tokenizer 的計數。如果您從 Claude Opus 4.7 之前的模型遷移,相同的內容會消耗約 30% 更多的 token。將工作負載遷移至 Claude Fable 5 和 Claude Mythos 5 時,請勿重複使用在 Claude Opus 4.7 之前的模型上測量的 token 計數來估算成本或上下文視窗的適配性。請使用 model: "claude-fable-5"(或 "claude-mythos-5")來計算您的提示。


定價與速率限制

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

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

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


常見問題

Was this page helpful?

  • 如何計算訊息 token
  • 支援的模型
  • 計算基本訊息中的 token
  • 計算包含工具的訊息中的 token
  • 計算包含圖片的訊息中的 token
  • 計算包含擴展思考的訊息中的 token
  • 計算包含 PDF 的訊息中的 token
  • Claude Fable 5 和 Claude Mythos 5 的 Token 計數
  • 定價與速率限制
  • 常見問題