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.
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?"}]
}'
from anthropic import Anthropic
client = Anthropic(base_url="https://platform.memtro.com", api_key="sk-memtro-…")
count = client.messages.count_tokens(
model="anthropic/claude-opus-5",
messages=[{"role": "user", "content": "What changed on the Baltic account this week?"}],
)
print(count.input_tokens)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ baseURL: "https://platform.memtro.com", apiKey: process.env.MEMTRO_API_KEY });
const count = await client.messages.countTokens({
model: "anthropic/claude-opus-5",
messages: [{ role: "user", content: "What changed on the Baltic account this week?" }],
});
console.log(count.input_tokens);
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-keyand Anthropic version headers.
Request body
The Messages request without max_tokens. The fields that matter to the count:
modelstringRequiredThe model the request is bound for.
messagesarray of objectsRequiredAs on Messages.
systemstring or array of objectsOptionalThe system prompt.
toolsarray of objectsOptionalYour tools; their schemas count.
tool_choiceobjectOptionalAs on Messages.
Response
200application/json
The count.
input_tokensintegerOptionalThe 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.
| Status | Type | When |
|---|---|---|
| 400 | invalid_request_error | The body failed validation. |
| 401 | authentication_error | The key is missing, malformed, revoked or expired. |
| 402 | authentication_error | The organisation has no active subscription. This route types every refusal of the key that way. |