トークンカウントを使用すると、メッセージをClaudeに送信する前にメッセージ内のトークン数を確認でき、プロンプトと使用量について十分な情報に基づいた判断を下すことができます。トークンカウントにより、以下のことが可能になります:

  • レート制限とコストを事前に管理
  • スマートなモデルルーティングの判断
  • プロンプトを特定の長さに最適化

メッセージのトークン数を数える方法

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

トークン数は推定値として扱う必要があります。場合によっては、メッセージ作成時の実際の入力トークン数が若干異なる可能性があります。

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

トークンカウントエンドポイントは以下のモデルをサポートしています:

  • Claude 3.7 Sonnet
  • Claude 3.5 Sonnet
  • Claude 3.5 Haiku
  • Claude 3 Haiku
  • Claude 3 Opus

基本的なメッセージのトークンを数える

import anthropic

client = anthropic.Anthropic()

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

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

ツールを含むメッセージのトークンを数える

import anthropic

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-3-7-sonnet-20250219",
    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())
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-3-7-sonnet-20250219",
    "messages": [
        {"role": "user", "content": [
            {"type": "image", "source": {
                "type": "base64",
                "media_type": "'$IMAGE_MEDIA_TYPE'",
                "data": "'$IMAGE_BASE64'"
            }},
            {"type": "text", "text": "Describe this image"}
        ]}
    ]
}'
JSON
{ "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-3-7-sonnet-20250219",
      "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?"
        }
      ]
    }'
JSON
{ "input_tokens": 88 }

PDFを含むメッセージのトークンを数える

トークンカウントは、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-3-7-sonnet-20250219",
      "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."
          }
        ]
      }]
    }'
JSON
{ "input_tokens": 2188 }

価格設定とレート制限

トークンカウントは無料で使用できますが、使用量層に基づく1分あたりのリクエスト制限が適用されます。より高い制限が必要な場合は、Anthropic Consoleを通じて営業部門にお問い合わせください。

使用量層1分あたりのリクエスト数 (RPM)
1100
22,000
34,000
48,000

トークンカウントとメッセージ作成には別個の独立したレート制限があります - 一方の使用が他方の制限にカウントされることはありません。


よくある質問

Was this page helpful?