Guides

Counting tokens

What it is for

Tokens are what you are billed for and how much a conversation can hold is measured in. To see how large a request is before you send it, post the same body to /v1/count-tokens. It returns input_tokens and does not produce a reply. Counting is free and not billed, and is limited to 100 a minute per key. The SDK methods are client.chat.countTokens in TypeScript and client.chat.count_tokens in Python.

Count a request

curl https://antarya.ai/v1/count-tokens \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "system": "Answer in plain English.",
    "messages": [
      {"role": "user", "content": "Summarise this contract'"'"'s termination clause."}
    ]
  }'
Output
{"input_tokens": 23}

What it counts

The count covers everything the request sends: system, every message, tool descriptions, a response schema, and files and images in the messages. It does not count the reply, which is only known afterwards in usage.output_tokens.

Parameters

NameTypeRequiredDescription
messagesarrayYesRequiredThe conversation to count, as in a chat request.
systemtextNoCounted with the messages.
toolsarrayNoCounted with the messages.
response_formatobjectNoCounted with the messages.

Limits

Limit Value
Counting requests per minute 100 a minute per key
Cost of a count Free, not billed

Next steps