moth docs
Reference Count tokens

Count tokens

Returns the input estimate a call would be held at, without calling a model or charging anything.

POSThttps://api.runmoth.si/v1/messages/count_tokens

Body

modelrequired

A chat model ID from the catalog.

messagesrequired

The conversation, as for messages.

system, toolsstring, array

Counted as they would be sent.

The figure is the hold's estimate of your input, the body's bytes divided by three plus 5,000 for each image, and a call with web search holds 40,000 tokens more. It is usually larger than the count a call is billed for, so use it to stay inside a context or a cap rather than to predict a charge.

Errors

400invalid_request_errorThe body isn't valid
401authentication_errorThe key is missing, revoked or expired
403permission_errorThe key may not use this model
404not_found_errorThe model isn't a chat model in the catalog
429rate_limit_errorToo many calls from the key or the account, or to the model, so wait for retry-after when it is sent
529overloaded_errorThe model is paused, or busy or down at its host, so choose another or wait for retry-after when it is sent
curl https://api.runmoth.si/v1/messages/count_tokens \
  -H "Authorization: Bearer $MOTH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "model": "gemini-3.8-flash", "messages": [ { "role": "user", "content": "Say hello in five words" } ] }'
Response
moth-request-id  req_4Gk9tQ2wXb7Lm1Pz8RcV
{
  "input_tokens": 32
}