Docs

Count Message Tokens

POST/v1/messages/count_tokens

Counts tokens for an Anthropic Messages-compatible request body.

On this page
  1. Authentication
  2. Request Body
  3. Response
  4. Request Example

Use this endpoint when a client needs the Anthropic Messages token count request shape before sending a generation request. The request body follows /v1/messages semantics.

Authentication

Use either header:

Authorization: Bearer sk-your-api-key
x-api-key: sk-your-api-key

If you use x-api-key, also send the Anthropic version header expected by Anthropic-compatible clients:

anthropic-version: 2023-06-01

Request Body

Parameter model

Type: string

Location: body

Required: yes

Claude model ID available to the current API key.

Parameter messages

Type: array

Location: body

Required: yes

Messages array using the Anthropic Messages format.

Parameter system

Type: string

Location: body

System prompt.

Parameter tools

Type: array

Location: body

Tool definitions included in the token count.

Response

The response uses the Anthropic-compatible token count shape returned by the routed provider.

Response Field input_tokens

Type: integer

Input token count.

Request Example

curl -X POST "https://api.anypath.dev/v1/messages/count_tokens" \
  -H "x-api-key: sk-your-api-key" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-6",
    "messages": [
      {"role": "user", "content": "Summarize this support case."}
    ]
  }'