---
title: "Count message tokens (Anthropic-compatible)"
method: POST
path: "/v1/messages/count_tokens"
tags: ["Anthropic"]
---

# Count message tokens (Anthropic-compatible)

`POST /v1/messages/count_tokens`

## Request

- Base URL: `https://api.tokenlab.sh`
- URL: `https://api.tokenlab.sh/v1/messages/count_tokens`
- Auth: HTTP bearer

Counts input tokens for an Anthropic-compatible messages request without creating a model response. Authentication is required; the operation is not billed. Verified upstream counting is preferred. Upstream availability failures return a local estimate marked by X-TokenLab-Count-Tokens-Source: local_estimate. Platform authentication, permission and input errors remain errors. Bound files unreadable locally require upstream counting.

## Request body

- AnthropicCountTokensRequest
  - `model` string, required: Model to count tokens for
  - `messages` object[], required: Messages in the conversation
    - `role` 'user' | 'assistant', required
    - `content` union, required
      - string
      - object[]
        - `type` string
        - `text` string
        - `source` object
          - `type` string
          - `media_type` string
          - `data` string
          - `url` string
  - `cache_control` object: Top-level Anthropic cache control marker for count_tokens requests.
  - `output_config` object: Anthropic output configuration fields that may affect token counting.
  - `system` union: System prompt
    - string
    - object[]
      - `type` string
      - `text` string
  - `thinking` object: Anthropic extended thinking configuration for token counting.
  - `tools` object[]: Tool definitions available to the model.
  - `tool_choice` union: Tool choice policy or explicit tool selection.
    - string
    - object

## Response `200`

Token count response

- AnthropicCountTokensResponse
  - `input_tokens` integer, required: Number of input tokens in the supplied Anthropic messages request

## Other responses

- `400`: Bad Request - Invalid input
- `401`: Unauthorized - Invalid or missing API key
- `402`: Payment Required - Insufficient balance or quota exceeded
- `429`: Rate Limit Exceeded
- `500`: Internal Server Error
- `502`: Service Error

---

[API](https://skmtc.dev/tokenlab/apis/tokenlab-ai-gateway.md) · [All operations](https://skmtc.dev/tokenlab/apis/tokenlab-ai-gateway/llms.txt) · [OpenAPI document](https://skmtc.dev/tokenlab/apis/tokenlab-ai-gateway/revisions/3208bfbcda22?raw)
