「Token counting」(Token 計數)讓您能夠在將訊息傳送給 Claude 之前確定訊息中的 token 數量,幫助您對提示和使用量做出明智的決策。透過 token 計數,您可以:
此功能符合「Zero Data Retention」(零資料保留),即 ZDR 的資格。當您的組織具有 ZDR 安排時,透過此功能傳送的資料在 API 回應返回後不會被儲存。
Token 計數端點接受與建立訊息相同的結構化輸入列表,包括支援系統提示、工具、圖片和 PDF。回應包含輸入 token 的總數。
Token 計數應被視為估計值。在某些情況下,建立訊息時實際使用的輸入 token 數量可能會有少量差異。
Token 計數可能包含 Anthropic 為系統最佳化而自動新增的 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()){ "input_tokens": 14 }伺服器工具的 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()){ "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-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()){ "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 相同。
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()){ "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 計數免費使用,但會根據您的使用層級受到每分鐘請求數的速率限制。如果您需要更高的限制,請透過 Claude Console 聯絡銷售團隊。
| 使用層級 | 每分鐘請求數(RPM) |
|---|---|
| 1 | 100 |
| 2 | 2,000 |
| 3 | 4,000 |
| 4 | 8,000 |
Token 計數和訊息建立具有獨立且分開的速率限制。使用其中一個不會計入另一個的限制。
Was this page helpful?