Loading...
  • ビルド
  • 管理
  • モデルと料金
  • クライアントSDK
  • APIリファレンス
Search...
⌘K
Log in
トークンカウント
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Amazon Bedrock
  • Google Cloud's Vertex AI

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
ビルド/コンテキスト管理

トークンカウント

メッセージをClaudeに送信する前にメッセージ内のトークン数を決定し、プロンプトと使用状況について情報に基づいた決定を下すのに役立つトークンカウント機能について説明します。

トークンカウントを使用すると、メッセージを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())
Output
{ "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())
Output
{ "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())
Output
{ "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())
Output
{ "input_tokens": 88 }

PDFを使用したメッセージ内のトークンをカウントする

トークンカウントは、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())
Output
{ "input_tokens": 2188 }

価格とレート制限

トークンカウントは無料で使用できますが、使用階層に基づいた1分あたりのリクエスト数のレート制限の対象です。より高い制限が必要な場合は、Claude Consoleを通じて営業に連絡してください。

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

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


FAQ

Was this page helpful?

  • PDFを使用したメッセージ内のトークンをカウントする
  • FAQ