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

# Count Tokens

> Count the number of tokens in a message before sending it. Useful for cost estimation and context window management.

## Request

```bash theme={null}
POST https://modelslab.com/api/v7/llm/v1/messages/count_tokens
```

```bash theme={null}
curl -X POST https://modelslab.com/api/v7/llm/v1/messages/count_tokens \
  -H "x-api-key: $MODELSLAB_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "Qwen/Qwen2.5-VL-72B-Instruct-together",
    "messages": [
      {"role": "user", "content": "What is the capital of France?"}
    ]
  }'
```

## Body

```json theme={null}
{
  "model": "Qwen/Qwen2.5-VL-72B-Instruct-together",
  "messages": [
    {"role": "user", "content": "What is the capital of France?"}
  ],
  "system": "You are a helpful assistant."
}
```

## Response

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

## Use Cases

* **Cost estimation**: Calculate the cost of a request before sending it
* **Context window management**: Ensure your messages fit within the model's context window
* **Token budgeting**: Allocate token budgets across multiple requests

## Example

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

client = Anthropic(
    api_key="YOUR_MODELSLAB_API_KEY",
    base_url="https://modelslab.com/api/v7/llm",
)

# Count tokens before sending
token_count = client.messages.count_tokens(
    model="Qwen/Qwen2.5-VL-72B-Instruct-together",
    messages=[
        {"role": "user", "content": "Write a detailed essay about AI"}
    ],
)

print(f"Input tokens: {token_count.input_tokens}")
```


## OpenAPI

````yaml POST /v1/messages/count_tokens
openapi: 3.1.0
info:
  title: ModelsLab LLM API
  description: >-
    Unified LLM API with OpenAI and Anthropic SDK compatibility. Access 200+
    language models through a single API.
  version: 7.0.0
servers: []
security: []
paths:
  /v1/messages/count_tokens:
    post:
      summary: Count Tokens
      description: Count the number of tokens in a message before sending it.
      parameters:
        - name: anthropic-version
          in: header
          required: false
          schema:
            type: string
            default: '2023-06-01'
          description: Anthropic API version
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CountTokensRequest'
      responses:
        '200':
          description: Token count response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CountTokensResponse'
      security:
        - apiKeyAuth: []
      servers:
        - url: https://modelslab.com/api/v7/llm
components:
  schemas:
    CountTokensRequest:
      type: object
      required:
        - model
        - messages
      properties:
        model:
          type: string
          description: Model ID to count tokens for
        messages:
          type: array
          description: Messages to count tokens for
          items:
            $ref: '#/components/schemas/AnthropicMessage'
        system:
          type: string
          description: System prompt to include in token count
    CountTokensResponse:
      type: object
      properties:
        input_tokens:
          type: integer
          description: Number of input tokens
    AnthropicMessage:
      type: object
      required:
        - role
        - content
      properties:
        role:
          type: string
          enum:
            - user
            - assistant
          description: Role of the message sender
        content:
          type: string
          description: Content of the message
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
      description: API key authentication via x-api-key header

````