Count tokens (Messages)
⚠️
Preview — this feature is being built; the design below is the spec.
POST https://app.syntic.ai/v1/messages/count_tokensThe Messages counterpart of token counting: send a Messages request body and get the input token count without running the model. Free, and validated exactly like the real request.
For how this fits into a full workflow, see Token counting.
Headers
| Header | Required | Value |
|---|---|---|
Authorization | Yes | Bearer <key>: a private inference key (sk_live_...). See Authentication. |
Content-Type | Yes | application/json |
Body parameters
| Parameter | Type | Required | Notes |
|---|---|---|---|
model | string | Yes | The model whose tokenizer to use. |
system | string or array | No | System instructions. |
messages | array | Yes | Content-block messages. |
tools | array | No | Tool definitions. |
Response
200 OK, Content-Type: application/json
{
"object": "token_count",
"model": "Amara S1.2",
"input_tokens": 18342,
"details": {
"system_tokens": 412,
"message_tokens": 15210,
"tool_tokens": 1890,
"image_tokens": 765,
"document_tokens": 0
},
"context_window": 262144,
"remaining_tokens": 243802
}| Field | Type | Description |
|---|---|---|
input_tokens | integer | What usage.prompt_tokens would report for the same request, including the Syntic identity instruction. |
details | object | Breakdown by source; the parts add up to input_tokens. |
context_window | integer | The model’s context size. |
remaining_tokens | integer | context_window minus input_tokens. The reply must fit here. |
Examples
curl -sS https://app.syntic.ai/v1/messages/count_tokens \
-H "Authorization: Bearer $SYNTIC_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "Amara S1.2",
"system": "You are a careful contracts assistant.",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Summarise clause 14."
}
]
}
]
}'Errors
Errors use the error envelope and carry a request_id. Codes specific to this operation:
| Status | type | code | When |
|---|---|---|---|
| 400 | invalid_request_error | messages_required | No messages. |
| 400 | invalid_request_error | model_required | model is missing. Counting has no default model. |
| 404 | not_found_error | model_not_found | Unknown model name. |
| 401 | authentication_error | invalid_api_key | The key is missing, unknown, revoked or expired. |
| 429 | rate_limit_error | rate_limited | Too many requests; wait for Retry-After. See Rate-limit headers. |
Related
Was this page helpful?