API Reference
Count tokens
POST /v1/messages/count_tokens — preflight token count for Anthropic-shape requests
Estimate how many input tokens a Messages-API request will use, before you send it. Useful for staying under context limits or budgeting cost per request.
Endpoint:
POST /v1/messages/count_tokensAuth: Bearer token or x-api-key. Requires scope inference.chat.
Required header: anthropic-version — any recent version string.
Example
curl https://api.melious.ai/v1/messages/count_tokens \
-H "x-api-key: sk-mel-<YOUR_API_KEY>" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4",
"system": "You are concise.",
"messages": [{"role": "user", "content": "Name three Hanseatic cities."}]
}'Response:
{ "input_tokens": 18 }Request
| Parameter | Type | Description |
|---|---|---|
messages | array | The conversation, same shape as Messages. |
system | string | array | System prompt (optional). |
tools | array | Tool definitions (optional). Name, description, and schema all count. |
model | string | Model ID (optional). Present for SDK compatibility. |
Image content blocks contribute a fixed per-image cost (the high-detail rate). Tool schemas count as their serialized JSON.
Response
{ "input_tokens": 18 }Always input_tokens, always a non-negative integer. No environment or billing fields — this endpoint is free.
Errors
400— missinganthropic-versionheader or malformed body.AUTH_1015— insufficient scope.
Related
Messages for the full endpoint • Context window errors.