功能
Token 計數
Token 計數讓您能夠在將訊息發送給 Claude 之前確定訊息中的 token 數量,幫助您對提示和使用做出明智的決策。透過 token 計數,您可以
- 主動管理速率限制和成本
- 做出智慧的模型路由決策
- 優化提示以達到特定長度
如何計算訊息 token
如何計算訊息 token
token 計數端點接受與建立訊息相同的結構化輸入列表,包括支援系統提示、工具、圖像和 PDF。回應包含輸入 token 的總數。
token 計數應被視為估計值。在某些情況下,建立訊息時使用的實際輸入 token 數量可能會有少量差異。
Token 計數可能包括 Anthropic 為系統優化自動添加的 token。您不會為系統添加的 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
伺服器工具 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
計算包含圖像的訊息中的 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
- 當前助理回合的思考會計入您的輸入 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
計算包含 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) |
|---|---|
| 1 | 100 |
| 2 | 2,000 |
| 3 | 4,000 |
| 4 | 8,000 |
Token 計數和訊息建立有獨立的速率限制 -- 其中一個的使用不會計入另一個的限制。
常見問題
常見問題