API reference · Messages

Count tokens

POST/v1/messages/count_tokens

Count the input tokens of a Messages request: Anthropic's own count for an Anthropic model, otherwise an estimate marked as one.

Open Memtro

Counts the input tokens of a Messages request, so an SDK that budgets before it asks keeps working. Where the model is an anthropic/* model and you hold an Anthropic key, the count is Anthropic's own; otherwise, or if that call fails or takes over ten seconds, it is an estimate of about 3.6 characters a token, marked memtro.estimated.

It counts your request, not what Memtro sends upstream: the prompt, tools and recalled context Memtro adds are not yours to budget for, and they vary with where the request routes.

Example

Against https://platform.memtro.com, with your key in MEMTRO_API_KEY.

curl https://platform.memtro.com/v1/messages/count_tokens \
  -H "x-api-key: $MEMTRO_API_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "anthropic/claude-opus-5",
    "messages": [{"role": "user", "content": "What changed on the Baltic account this week?"}]
  }'

A model with no Anthropic tokeniser behind it gets an estimate, marked as one.

{ "input_tokens": 18, "memtro": { "estimated": true } }

Authentication

An API key, as x-api-key or Authorization: Bearer.

  • This route sets no request id header.
  • Browsers may call it from any origin: it answers CORS preflights and allows the Authorization, x-api-key and Anthropic version headers.

Request body

The Messages request without max_tokens. The fields that matter to the count:

modelstringRequired

The model the request is bound for.

messagesarray of objectsRequired

As on Messages.

systemstring or array of objectsOptional

The system prompt.

toolsarray of objectsOptional

Your tools; their schemas count.

tool_choiceobjectOptional

As on Messages.

Response

200application/json

The count.

input_tokensintegerOptional

The number of input tokens.

memtroobjectOptional

{"estimated": true} when the number is an estimate rather than Anthropic's count.

Errors

In Anthropic's envelope, {"type": "error", "error": {"type", "message"}}, with no code: the type is chosen from the status. See Errors on the overview.

StatusTypeWhen
400invalid_request_error

The body failed validation.

401authentication_error

The key is missing, malformed, revoked or expired.

402authentication_error

The organisation has no active subscription. This route types every refusal of the key that way.

Where agents remember your company

Sign in, add a provider key, create an API key and point your tools at Memtro. It takes about five minutes.

7-day free trial · card on file, nothing charged until it ends · cancel any time