Token 計數讓您可以在將訊息傳送給 Claude 之前確定其中的 token 數量。這有助於您對提示和使用方式做出明智的決策。透過 token 計數,您可以:
關於「zero data retention」(零資料保留),即 ZDR 如何適用於此功能,請參閱 API 與資料保留。
Token 計數端點接受與建立訊息相同的結構化輸入清單,包括支援系統提示、工具、圖片和 PDF。回應包含輸入 token 的總數。
Token 計數是一個估計值。在某些情況下,建立訊息時實際使用的輸入 token 數量可能會有少量差異。
Token 計數可能包含 Anthropic 為系統最佳化而自動新增的 token。您不會因系統新增的 token 而被收費。帳單僅反映您的內容。
所有現行模型都支援 token 計數,包括 Claude Opus 5 和 Claude Sonnet 5。
Claude 4.7 及更新的模型以及 Claude Mythos Preview 使用較新的 tokenizer(分詞器)。相同的輸入文字產生的 token 數量比早期模型多約 30%。確切的增加幅度取決於內容和工作負載的形態。請針對您計劃使用的模型重新計算提示的 token 數量,而不是重複使用針對早期模型測量的計數。
client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-opus-5",
system="You are a scientist",
messages=[{"role": "user", "content": "Hello, Claude"}],
)
print(response.json()){ "input_tokens": 14 }伺服器工具的 token 計數僅適用於第一次取樣呼叫。
client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-opus-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()){ "input_tokens": 403 }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-5",
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()){ "input_tokens": 1551 }請參閱思考與上下文視窗以了解更多詳細資訊。
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()){ "input_tokens": 88 }Token 計數支援 PDF,並具有與 Messages API 相同的 PDF 支援限制。
import base64
import anthropic
client = anthropic.Anthropic()
with open("/path/to/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-5",
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()){ "input_tokens": 2188 }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 數量。
Token 計數可免費使用,但受限於基於您的使用層級的每分鐘請求數速率限制。如果您需要更高的限制,請在限制頁面上使用請求提高速率限制。
| 使用層級 | 每分鐘請求數(RPM) |
|---|---|
| Start | 2,000 |
| Build | 4,000 |
| Scale | 8,000 |
Token 計數和訊息建立具有各自獨立的速率限制。使用其中一項不會計入另一項的限制。
閱讀 token 計數端點的完整 API 參考文件。
使用 token 計數讓提示保持在模型的上下文視窗範圍內。
在傳送請求之前檢查 token 計數,以維持在您的使用層級範圍內。
透過快取提示前綴來降低重複提示的成本和延遲。
Was this page helpful?