Applying for and Using the Claude Messages Count Tokens API

The Claude Messages Count Tokens API can calculate the number of input tokens in a Message without actually creating a message, including token counts for tools, images, and documents. This is useful when you need to estimate costs or check whether the input exceeds the model's context limit.

This document mainly introduces the process for using the Claude Messages Count Tokens API.

claude-fable-5-2 does not currently provide a Count Tokens endpoint. The final usage for a generation request can be found in usage in the Messages response; do not use counts from other models as a substitute for this model's exact usage.

Application Process

To use the Claude Messages Count Tokens API, first go to the Ace Data Cloud Console to obtain your API Token for later use.

If you have not yet logged in or registered, you will be automatically redirected to the login page to register and log in. Once completed, you will be automatically returned to the current page.

API Keys for general-purpose applications can be used for services enabled for the account; Keys for dedicated applications are used according to the application's service scope. Please check the Key's permissions, validity period, and limits, and confirm that the application balance is sufficient to pay for this request. Available trial credits are subject to what is shown in the account. If the balance is insufficient, click the "Recharge" button.

📘 Full documentation: Claude Messages Count Tokens API →

Basic Usage

The request path for the Claude Messages Count Tokens API is /v1/messages/count_tokens, consistent with the official Anthropic API. We need to provide at least two required parameters:

  • model: Enter the full model ID from this endpoint's model list, and use the same model for the subsequent Messages request.
  • messages: An array of input messages, each containing role and content.

Common optional parameters:

  • system: The system prompt, which is included in the token count.
  • tools: Tool definitions, which are included in the token count.
  • thinking: Extended thinking configuration.
  • tool_choice: Tool selection configuration.
  • cache_control: Top-level or content-block-level cache control configuration.

messages, system, tool call replay, URL images, and document/PDF content blocks use the same stable request structure as the Messages API.

cURL Example

curl -X POST 'https://api.acedata.cloud/v1/messages/count_tokens' \
  -H 'accept: application/json' \
  -H 'authorization: Bearer {token}' \
  -H 'content-type: application/json' \
  -d '{
    "model": "claude-fable-5-1",
    "messages": [
      {
        "role": "user",
        "content": "Hello, Claude"
      }
    ]
  }'

Python Example

import httpx

url = "https://api.acedata.cloud/v1/messages/count_tokens"
headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json",
}
payload = {
    "model": "claude-fable-5-1",
    "messages": [
        {
            "role": "user",
            "content": "Hello, Claude"
        }
    ],
}
response = httpx.post(url, headers=headers, json=payload)
print(response.json())

Example response:

{
  "input_tokens": 11
}

Using the Anthropic SDK

The Claude Messages Count Tokens API accepts the Anthropic SDK's stable request structure and can be called through the anthropic library. The endpoint counts input tokens according to the selected model and can be used to check the input size before sending a Messages request.

from anthropic import Anthropic

client = Anthropic(
    api_key="{token}",
    base_url="https://api.acedata.cloud",
)

result = client.messages.count_tokens(
    model="claude-opus-4-8",
    messages=[
        {
            "role": "user",
            "content": "Hello, Claude"
        }
    ],
)
print(result.input_tokens)

Token Counting with Tools

If your request includes tool definitions, these tools are also included in the token count:

result = client.messages.count_tokens(
    model="claude-opus-4-8",
    messages=[
        {
            "role": "user",
            "content": "What is the weather in San Francisco?"
        }
    ],
    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"]
            }
        }
    ],
)
print(result.input_tokens)

Token Counting with a System Prompt

The system prompt is also included in the token count:

result = client.messages.count_tokens(
    model="claude-opus-4-8",
    system="You are a helpful assistant that speaks Chinese.",
    messages=[
        {
            "role": "user",
            "content": "Hello"
        }
    ],
)
print(result.input_tokens)

Notes

  • This API only counts input tokens and does not produce any model output.
  • The token count can be used to estimate input size and check the context window; final billing is based on usage in the actual Messages response.
  • Images, PDFs, tool definitions, system prompts, and thinking configurations are included in the result according to the selected model's input rules.
  • This API is completely free and does not consume any credits.