Count tokens API: free pre-send token estimate

Estimate the input tokens of a Messages request for free, on an ordinary API key, without the gateway calling a model.

Checked 2026-11-22

·API reference·Models

Size a Messages request before you send it with POST /v1/messages/count_tokens at https://api.uplinkswitch.com/v1, for free and with no model call.

Route

MethodPathAuthCost
POST/v1/messages/count_tokensan ordinary API keyfree (no model call)

Request

FieldTypeNotes
modelstringinclude the Messages model id: the schema only requires messages, but the documented 400 response includes a missing model
messagesarrayrequired; the message list, same shape as a Messages request
systemstring or arrayoptional; system prompt
toolsarrayoptional; tool definitions

Response

FieldNotes
input_tokensinteger; estimated input tokens. Counted locally (roughly four characters per token plus a few per message, each image about 1,600 tokens). It errs high, so treat it as an estimate.

Example

bash
curl https://api.uplinkswitch.com/v1/messages/count_tokens \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"claude-sonnet-5-5","messages":[{"role":"user","content":"hello"}]}'

Errors

StatusCodeWhen
400invalid_requestno model, or messages is not a list
401invalid_api_keymissing or invalid key
413invalid_requestbody too large

Questions and answers

No. It is free and the gateway never calls a model, so nothing bills.

It is an estimate that runs locally and errs high, but is not a ceiling or a tokenizer guarantee. Billing uses the model-reported count.

Any ordinary API key on your org. There is no admin requirement.

Each image counts as about 1,600 tokens, added to the text estimate.

You get 413 invalid_request. Trim the messages or send fewer at once. ## Sources Live OpenAPI, checked 2026-11-22.
Join on Telegram
Help