Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Token 计数使您能够在将消息发送给 Claude 之前确定消息中的 token 数量,帮助您对提示和使用做出明智的决策。通过 token 计数,您可以
token 计数端点接受与创建消息相同的结构化输入列表,包括对系统提示、工具、图像和 PDF 的支持。响应包含输入 token 的总数。
token 计数应被视为一个估计值。在某些情况下,创建消息时实际使用的输入 token 数量可能会有少量差异。
Token 计数可能包含 Anthropic 为系统优化自动添加的 token。系统添加的 token 不会向您收费。计费仅反映您的内容。
所有活跃模型都支持 token 计数。
import anthropic
client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-opus-4-6",
system="You are a scientist",
messages=[{
"role": "user",
"content": "Hello, Claude"
}],
)
print(response.json()){ "input_tokens": 14 }服务器工具的 token 计数仅适用于第一次采样调用。
import anthropic
client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-opus-4-6",
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 }#!/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-opus-4-6",
"messages": [
{"role": "user", "content": [
{"type": "image", "source": {
"type": "base64",
"media_type": "'$IMAGE_MEDIA_TYPE'",
"data": "'$IMAGE_BASE64'"
}},
{"type": "text", "text": "Describe this image"}
]}
]
}'{ "input_tokens": 1551 }有关扩展思考如何计算上下文窗口的更多详情,请参阅此处
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?"
}
]
}'{ "input_tokens": 88 }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-opus-4-6",
"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."
}
]
}]
}'{ "input_tokens": 2188 }Token 计数免费使用,但受每分钟请求数速率限制的约束,具体取决于您的使用层级。如果您需要更高的限制,请通过 Claude Console 联系销售团队。
| 使用层级 | 每分钟请求数 (RPM) |
|---|---|
| 1 | 100 |
| 2 | 2,000 |
| 3 | 4,000 |
| 4 | 8,000 |
Token 计数和消息创建具有独立的速率限制——使用其中一个不会计入另一个的限制。
Was this page helpful?