Count tokens
⚠️
Preview — this feature is being built; the design below is the spec.
POST https://app.syntic.ai/v1/chat/completions/count_tokensCount the input tokens a chat completion would use, without running the model. The body is the same as for Create a chat completion; output-only fields such as max_tokens and stream are accepted and ignored, so you can pass your real request unchanged. Counting is free and does not use plan allowance.
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 | Tokenizers differ between models, so the model is required here. |
messages | array | Yes | Counted in full, including tool calls and results. |
tools | array | No | Counted as presented to the model. |
response_format | object | No | A schema adds input tokens. |
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/chat/completions/count_tokens \
-H "Authorization: Bearer $SYNTIC_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "Amara S1.2",
"messages": [
{
"role": "system",
"content": "You are a careful contracts assistant."
},
{
"role": "user",
"content": "Summarise clause 14 of the lease below. ..."
}
]
}'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. |
Limits
Counting is capped separately from inference, at 100 calls a minute per key. Server-side tool results added during a real request (for example web search results) cannot be known in advance and are not included.
Related
Was this page helpful?