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

# Count tokens

> Get an approximate input-size estimate for a Messages request.

This endpoint returns an **estimate**, not an exact model-tokenizer count. It does not run a generation. Use it as a rough input-size signal; final usage and request admission can differ.

<ParamField header="x-api-key" type="string" required>
  Your derestricted API key. A Bearer token is also accepted. The account must
  be active and verified.
</ParamField>

<ParamField body="messages" type="array" required>
  Messages to include in the estimate.
</ParamField>

<ParamField body="system" type="string or array">
  Optional system instructions to include.
</ParamField>

<ParamField body="tools" type="array">
  Optional tool definitions to include.
</ParamField>

## Example

```bash theme={"system"}
curl https://api.derestricted.ai/v1/messages/count_tokens \
  -H "x-api-key: $DERESTRICTED_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "derestricted-llm",
    "messages": [{"role": "user", "content": "Hello"}]
  }'
```

The response is an object containing `input_tokens`, an integer estimate.

## How to interpret the estimate

The current implementation estimates from the serialized UTF-8 size of `system`, `messages`, and `tools`, adds per-message and fixed overhead, divides by four, and rounds upward. It does not invoke the deployed model's tokenizer.

This endpoint does not reserve credit, check whether your output allowance is affordable, or guarantee that the generation will fit the context/input limits. A later generation can still return a size, credit, or capacity error. Use actual response usage for completed-request token counts.

The same 4 MiB JSON-body limit applies. See [Messages](/api/messages) to generate a response.
