令牌计数使您能够在向 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 的消息中的令牌

令牌计数支持 PDF,与消息 API 具有相同的限制

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 }

定价和速率限制

令牌计数免费使用,但根据您的使用层级受每分钟请求数限制。如果您需要更高的限制,请通过 Anthropic Console 联系销售。

使用层级每分钟请求数 (RPM)
1100
22,000
34,000
48,000

令牌计数和消息创建有独立的速率限制 — 使用其中一个不会计入另一个的限制。


常见问题

Was this page helpful?