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

# Count Tokens

> POST /v1/messages/count_tokens — estimate token count without billing

Estimate the token count before actually calling `/v1/messages`, useful for:

* Checking in advance whether you'll exceed the context window.
* Precisely estimating cost in batch jobs.
* Showing the token count on the client.

## Request

```bash theme={null}
curl https://openp.ai/v1/messages/count_tokens \
  -H "x-api-key: $OPENPAI_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-8",
    "messages": [{"role": "user", "content": "hello"}]
  }'
```

The fields are the same as `/v1/messages` (can include `system` / `tools` / `messages`),
and you **don't** need to provide generation parameters like `max_tokens`.

## Response

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

## Billing

The `count_tokens` endpoint **doesn't consume model quota**, but it is still subject to the OpenPAI gateway's RPM rate limiting.
