トークンカウントを使用すると、Claudeにメッセージを送信する前に、そのメッセージのトークン数を確認できます。これにより、プロンプトと使用量について情報に基づいた意思決定を行うことができます。トークンカウントを使用すると、次のことが可能です。
この機能はZero Data Retention(ZDR)の対象です。組織がZDR契約を締結している場合、この機能を通じて送信されたデータは、APIレスポンスが返された後に保存されることはありません。
トークンカウントエンドポイントは、メッセージ作成時と同じ構造化された入力リストを受け付け、システムプロンプト、ツール、画像、PDFをサポートしています。レスポンスには入力トークンの合計数が含まれます。
トークン数は推定値として考慮する必要があります。場合によっては、メッセージ作成時に実際に使用される入力トークン数がわずかに異なることがあります。
トークン数には、システム最適化のためにAnthropicが自動的に追加するトークンが含まれる場合があります。システムが追加したトークンに対して課金されることはありません。課金はお客様のコンテンツのみを反映します。
Claude Sonnet 5を含む、すべてのアクティブなモデルがトークンカウントをサポートしています。
Claude Opus 4.7以降のOpusモデル、Claude Fable 5、Claude Mythos 5、Claude Mythos Preview、およびClaude Sonnet 5は、新しいトークナイザーを使用しています。同じ入力テキストでも、以前のモデルと比較して約30%多くのトークンが生成されます。正確な増加量はコンテンツとワークロードの形状によって異なります。以前のモデルで測定したカウントを再利用するのではなく、使用する予定のモデルに対してプロンプトを再カウントしてください。
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 }サーバーツールのトークン数は、最初のサンプリング呼び出しにのみ適用されます。
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 }トークンカウントは、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-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で導入されたトークナイザーを使用しており、同じテキストに対してClaude Opus 4.7より前のモデルと比較して約30%多くのトークンを生成します。正確な増加量はコンテンツとワークロードの形状によって異なります。トークンカウントエンドポイントは、渡したmodelのトークナイザーに基づくカウントを返すため、ワークロードでの差異を測定するには、同じリクエストを2回カウントします。1回は現在のモデルで、もう1回はmodel: "claude-fable-5"(または"claude-mythos-5")でカウントし、2つのinput_tokensの値を比較します。
課金と移行: Claude Fable 5およびClaude Mythos 5での使用量と課金は、このトークナイザーのカウントを反映します。Claude Opus 4.7より前のモデルから移行する場合、同じコンテンツで約30%多くのトークンを消費します。正確な増加量はコンテンツとワークロードの形状によって異なります。ワークロードをClaude Fable 5およびClaude Mythos 5に移行する際は、Claude Opus 4.7より前のモデルで測定したトークン数を再利用してコストやコンテキストウィンドウへの適合性を見積もらないでください。model: "claude-fable-5"(または"claude-mythos-5")でプロンプトをカウントしてください。
トークンカウントは無料で使用できますが、使用量ティアに基づく1分あたりのリクエスト数のレート制限が適用されます。より高い制限が必要な場合は、LimitsページのRequest rate limit increaseを使用してください。
| 使用量ティア | 1分あたりのリクエスト数(RPM) |
|---|---|
| Start | 2,000 |
| Build | 4,000 |
| Scale | 8,000 |
トークンカウントとメッセージ作成には、それぞれ独立した別個のレート制限があります。一方の使用が他方の制限にカウントされることはありません。
トークンカウントエンドポイントの完全なAPIリファレンスをお読みください。
トークン数を使用して、プロンプトをモデルのコンテキストウィンドウ内に収めます。
リクエストを送信する前にトークン数を確認して、使用量ティア内に収めます。
プロンプトのプレフィックスをキャッシュすることで、繰り返しのプロンプトのコストとレイテンシを削減します。
Was this page helpful?