トークンカウントを使用すると、メッセージをClaudeに送信する前にメッセージ内のトークン数を決定できるため、プロンプトと使用状況について情報に基づいた決定を下すのに役立ちます。トークンカウントを使用すると、以下のことができます。
This feature is eligible for Zero Data Retention (ZDR). When your organization has a ZDR arrangement, data sent through this feature is not stored after the API response is returned.
トークンカウントエンドポイントは、システムプロンプト、ツール、画像、PDFのサポートを含む、メッセージを作成するための同じ構造化入力リストを受け入れます。レスポンスには、入力トークンの総数が含まれます。
トークンカウントは推定値と見なされるべきです。場合によっては、メッセージを作成するときに実際に使用される入力トークンの数が少量異なる可能性があります。
トークンカウントには、システム最適化のためにAnthropicによって自動的に追加されたトークンが含まれる場合があります。システムが追加したトークンについては課金されません。課金はコンテンツのみを反映します。
すべてのアクティブなモデルがトークンカウントをサポートしています。
client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-opus-4-7",
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-7",
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-7",
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-7",
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 }トークンカウントは無料で使用できますが、使用階層に基づいた1分あたりのリクエスト数のレート制限の対象です。より高い制限が必要な場合は、Claude Consoleを通じて営業に連絡してください。
| 使用階層 | 1分あたりのリクエスト数(RPM) |
|---|---|
| 1 | 100 |
| 2 | 2,000 |
| 3 | 4,000 |
| 4 | 8,000 |
トークンカウントとメッセージ作成には、別個の独立したレート制限があります。一方の使用は他方の制限に対してカウントされません。
Was this page helpful?