Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
トークンカウントを使用すると、Claudeにメッセージを送信する前にメッセージ内のトークン数を確認でき、プロンプトや使用量について情報に基づいた判断を行うことができます。トークンカウントにより、以下のことが可能になります:
トークンカウントエンドポイントは、メッセージ作成と同じ構造化された入力リストを受け付け、システムプロンプト、ツール、画像、PDFをサポートしています。レスポンスには入力トークンの合計数が含まれます。
トークン数は推定値として考慮してください。場合によっては、メッセージ作成時に実際に使用される入力トークン数がわずかに異なることがあります。
トークン数には、Anthropicがシステム最適化のために自動的に追加したトークンが含まれる場合があります。システムが追加したトークンに対して課金されることはありません。課金はお客様のコンテンツのみに反映されます。
すべてのアクティブなモデルがトークンカウントをサポートしています。
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 }サーバーツールのトークン数は最初のサンプリング呼び出しにのみ適用されます。
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 }トークンカウントは、Messages APIと同じ制限事項でPDFをサポートしています。
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 }トークンカウントは無料で使用できますが、使用量ティアに基づく1分あたりのリクエスト数のレート制限が適用されます。より高い制限が必要な場合は、Claude Consoleからセールスにお問い合わせください。
| 使用量ティア | 1分あたりのリクエスト数(RPM) |
|---|---|
| 1 | 100 |
| 2 | 2,000 |
| 3 | 4,000 |
| 4 | 8,000 |
トークンカウントとメッセージ作成には別々の独立したレート制限があります。一方の使用量がもう一方の制限にカウントされることはありません。
Was this page helpful?