> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xhuoapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Claude Messages Count Tokens API Application and Usage

> Claude AI 集成指南 - XHuoAPI

The Claude Messages Count Tokens API can calculate the input token count of a message without actually creating the message, including the token count of tools, images, and documents. This is very useful when estimating costs or checking if the input exceeds the model's context limits.

This document mainly describes the usage process of the Claude Messages Count Tokens API.

## Application Process

To use the Claude Messages Count Tokens API, you can first go to the [Claude Messages Count Tokens API](https://api.xhuoapi.ai/documents/claude-messages-count-tokens) page and click the "Acquire" button to obtain the credentials needed for the request.

If you are not logged in or registered, you will be automatically redirected to the login page inviting you to register and log in. After logging in or registering, you will be automatically returned to the current page.

This API is completely free to use and does not consume any quota.

## 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`: Choose the Claude model to use, such as `claude-sonnet-4-5-20250929`, `claude-opus-4-20250514`, etc.
* `messages`: An array of input messages, each containing `role` and `content`.

Common optional parameters:

* `system`: System prompt, which will be included in the token count.
* `tools`: Tool definitions, which will be included in the token count.
* `thinking`: Extended thinking configuration.

### cURL Example

```bash theme={null}
curl -X POST 'https://api.xhuoapi.ai/v1/v1/messages/count_tokens' \
  -H 'accept: application/json' \
  -H 'authorization: Bearer {token}' \
  -H 'content-type: application/json' \
  -d '{
    "model": "claude-sonnet-4-5-20250929",
    "messages": [
      {
        "role": "user",
        "content": "Hello, Claude"
      }
    ]
  }'
```

### Python Example

```python theme={null}
import httpx

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

Example of return result:

```json theme={null}
{
  "input_tokens": 11
}
```

### Using Anthropic SDK

The Claude Messages Count Tokens API is fully compatible with the official Anthropic SDK and can be called directly using the `anthropic` library.

```python theme={null}
from anthropic import Anthropic

client = Anthropic(
    api_key="{token}",
    base_url="https://api.xhuoapi.ai/v1",
)

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

## Token Count Including Tools

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

```python theme={null}
result = client.messages.count_tokens(
    model="claude-sonnet-4-5-20250929",
    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 Count Including System Prompts

System prompts will also be included in the token count:

```python theme={null}
result = client.messages.count_tokens(
    model="claude-sonnet-4-5-20250929",
    system="You are a helpful assistant that speaks Chinese.",
    messages=[
        {
            "role": "user",
            "content": "Hello"
        }
    ],
)
print(result.input_tokens)
```

## Notes

* This API only calculates the input token count and does not produce any model output.
* The token count result can be used to estimate the cost of calling the Claude Messages API.
* The tokenization method may vary for different models, so please use the same model parameter as in the actual call.
* This API is completely free and does not consume any quota.
