トークンカウントを使用すると、Claudeにメッセージを送信する前に、そのメッセージ内のトークン数を確認できます。これにより、プロンプトと使用量について情報に基づいた判断を下すことができます。トークンカウントを使用すると、次のことが可能になります。
この機能はZero Data Retention(ZDR)の対象です。組織がZDR契約を締結している場合、この機能を通じて送信されたデータは、APIレスポンスが返された後に保存されることはありません。
トークンカウントエンドポイントは、メッセージ作成時と同じ構造化された入力リストを受け付けます。これにはシステムプロンプト、ツール、画像、PDFのサポートが含まれます。レスポンスには入力トークンの合計数が含まれます。
トークン数は推定値と見なしてください。場合によっては、メッセージ作成時に実際に使用される入力トークン数がわずかに異なることがあります。
トークン数には、システム最適化のためにAnthropicによって自動的に追加されたトークンが含まれる場合があります。システムによって追加されたトークンに対しては課金されません。課金はお客様のコンテンツのみを反映します。
すべてのアクティブなモデルがトークンカウントをサポートしています。
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("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分あたりのリクエスト数のレート制限が適用されます。より高い制限が必要な場合は、Claude Consoleから営業担当にお問い合わせください。
| 使用ティア | 1分あたりのリクエスト数(RPM) |
|---|---|
| 1 | 100 |
| 2 | 2,000 |
| 3 | 4,000 |
| 4 | 8,000 |
トークンカウントとメッセージ作成には、それぞれ独立した別個のレート制限があります。一方の使用は、もう一方の制限にはカウントされません。
トークンカウントエンドポイントの完全なAPIリファレンスをお読みください。
トークン数を使用して、プロンプトをモデルのコンテキストウィンドウ内に収めます。
リクエストを送信する前にトークン数を確認し、使用ティア内に収めます。
プロンプトプレフィックスをキャッシュすることで、繰り返しのプロンプトのコストとレイテンシを削減します。
Was this page helpful?