Claude Platform Docs
Messagesコンテキスト管理

トークンカウント

Claudeに送信する前に、メッセージ内のトークン数をカウントします。トークン数を使用して、レート制限とコストの管理、モデルルーティングの判断、目標の長さに合わせたプロンプトの調整を行えます。

「token counting」(トークンカウント)を使用すると、Claudeにメッセージを送信する前に、そのメッセージ内のトークン数を確認できます。これにより、プロンプトと使用量について、情報に基づいた判断を下せます。トークンカウントを使用すると、次のことが可能になります。

  • 「rate limit」(レート制限)とコストを事前に管理する
  • 適切なモデルルーティングの判断を行う
  • プロンプトを特定の長さに最適化する

メッセージのトークンをカウントする方法

トークンカウントエンドポイントは、メッセージ作成時と同じ構造化された入力リストを受け付けます。これには、「system prompt」(システムプロンプト)、ツール、画像、PDFのサポートも含まれます。レスポンスには入力トークンの合計数が含まれます。

このエンドポイントは、Messages APIが受け付けるいくつかの入力に対してinvalid_request_errorを返します。対象となるのは、ウェブ検索、ウェブフェッチ、コード実行、ツール検索などのサーバーツール(アドバイザーツールを除くすべてのサーバーツール)、「Model Context Protocol」、すなわちMCPのコネクタ、およびurlまたはfileソースを持つimageまたはdocumentブロックです。画像とPDFをカウントするには、base64として送信してください。サーバーツールまたはMCPサーバーを使用するリクエストの場合、Messages APIのレスポンスのusageオブジェクトで、使用されたトークンが報告されます。

サポートされているモデル

すべてのアクティブなモデルがトークンカウントをサポートしています。

基本的なメッセージのトークンをカウントする

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-5-5",
    system="You are a scientist",
    messages=[{"role": "user", "content": "Hello, Claude"}],
)

print(response.json())
Output
{ "input_tokens": 14 }

ツールを含むメッセージのトークンをカウントする

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-5-5",
    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())
Output
{ "input_tokens": 403 }

画像を含むメッセージのトークンをカウントする

import base64
import httpx2

image_url = "https://platform.claude.com/docs/images/vision-example.jpg"
image_media_type = "image/jpeg"
image_data = base64.standard_b64encode(httpx2.get(image_url).content).decode("utf-8")

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-5-5",
    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())
Output
{ "input_tokens": 1028 }

"oversized_image": "error"を設定した埋め込み画像ブロックは、Messages APIが拒否するのとまったく同じように、カウント時に拒否されます。

思考を含むメッセージのトークンをカウントする

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-5-5",
    thinking={"type": "adaptive"},
    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())
Output
{ "input_tokens": 88 }

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-5-5",
    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())
Output
{ "input_tokens": 2188 }

Claude FableおよびClaude Mythosモデルでのトークン数

Claude Fable 5.1、Claude Mythos 5.1、Claude Fable 5、Claude Mythos 5は、Claude Opus 4.7で導入されたトークナイザーを共有しています。プロンプトのカウントはこの4つのモデルすべてで同じであり、Claude Opus 4.7より前のモデルと比べて約30パーセント多くなります(正確な増加量はコンテンツによって異なります)。トークンカウントエンドポイントは、渡したmodelのトークナイザーに基づいてカウントします。ワークロードでの差を測定するには、同じリクエストを2回(1回は現在のモデルで、もう1回は移行予定のモデルで)カウントし、2つのinput_tokensの値を比較してください。


料金とレート制限

トークンカウントは無料で使用できますが、使用量ティアに基づく1分あたりのリクエスト数のレート制限が適用されます。より高い制限が必要な場合は、レート制限ページのRequest rate limit increaseを使用してください。

使用量ティア1分あたりのリクエスト数(RPM)
Start5,000
Build10,000
Scale20,000

よくある質問


次のステップ

トークンカウントエンドポイントの完全なAPIリファレンスをお読みください。

トークン数を使用して、プロンプトをモデルのコンテキストウィンドウ内に収めます。

リクエストを送信する前にトークン数を確認し、使用量ティアの範囲内に収めます。

プロンプトのプレフィックスをキャッシュすることで、繰り返し使用するプロンプトのコストとレイテンシを削減します。

Compatibility

Supported platforms
  • Claude API
  • Claude Platform on AWS
  • Amazon Bedrock
  • Google Cloud
  • Microsoft Foundry

Was this page helpful?