EasyRouterEasyRouter
User GuideAPI DocsConnect Agent Tools
AI Model APIChat

Claude Message Token Counting

Claude Message Token Counting

This endpoint is used to accurately calculate and estimate the number of input tokens sent to Claude models before making a real inference request. This helps users control costs, optimize prompts, and avoid exceeding model window limits.

🌟 Endpoint Highlights

  • 100% Free: Calling this token counting endpoint does not consume any account credits or subscription package quotas, and you can invoke it even if your account balance is 0.
  • Multimodal Support: Perfectly supports system prompts, tools definitions, text, and image inputs (both URL and Base64 formats).
  • Extreme Reliability: Features a built-in regional fallback mechanism. Even for custom ARNs or cross-region profiles that do not natively support the bedrock-runtime CountTokens, it automatically translates and returns a 100% mathematically correct token count.

Endpoint Definition

  • Method: POST
  • Request URL: https://easyrouter.io/v1/messages/count_tokens
  • Headers:
    • Authorization: Bearer sk-your-token (Your EasyRouter API Key)
    • Content-Type: application/json

Request Parameters

The request payload format is identical to the standard Claude Messages API.

Response Parameters

Returns a standard JSON object:

  • input_tokens (integer): The total number of calculated input tokens.

Request Examples

Example 1: Text-only with System Prompts

curl -X POST https://easyrouter.io/v1/messages/count_tokens \
  -H "Authorization: Bearer sk-your-token" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-6",
    "system": "You are a professional system architect.",
    "messages": [
        {
            "role": "user",
            "content": "How to design a high-availability cache layer?"
        }
    ]
  }'

Example Response:

{
  "input_tokens": 32
}

Example 2: Multimodal Request with Images

This endpoint automatically downloads and converts image inputs to calculate their precise token weight.

curl -X POST https://easyrouter.io/v1/messages/count_tokens \
  -H "Authorization: Bearer sk-your-token" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-6",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Identify the content of this image."
                },
                {
                    "type": "image",
                    "source": {
                        "type": "url",
                        "url": "https://easyrouter.io/logo.png"
                    }
                }
            ]
        }
    ]
  }'

Example Response:

{
  "input_tokens": 542
}