Count the tokens in a message
/v1/messages/count_tokensCount the tokens in a message.
Compatible with Anthropic's Count Message Tokens API: the body is a Messages request (without max_tokens) and the answer is {"input_tokens": N}, the input tokens that request would use, from Anthropic's own counter (Anthropic, or Claude on Vertex). Same credentials and virtual key checks as creating a message. Nothing is generated, so nothing is billed or logged, and no cache, retries, fallbacks or guardrails apply.
Errors are in Anthropic's shape: 400 for an invalid request, a key for any other provider, or a routing config (x-config); 401 for a missing or invalid credential; 403 when the virtual key is disabled, expired or the model is not allowed.
Authorization
AuthorizationBearer <slug>.<secret>A virtual key's API key (
vk-....<secret>), asAuthorization: Bearer. What the OpenAI SDK sends from itsapi_key. Used by the inference endpoints.- or
x-virtual-keyheaderA virtual key's API key in the
x-virtual-keyheader, for clients that keepAuthorizationfor something else. - or
x-api-keyheaderDirect credentials: the provider's own API key in
x-api-key, with the provider named inx-provider(for exampleopenai). On/v1/messagesa virtual key is accepted inx-api-keytoo, which is what the Claude SDK sends.
Headers
x-providerstringWith direct credentials: the provider
x-api-keybelongs to, for exampleopenaioranthropic(case and dashes are normalised). Not used with a virtual key.x-vertex-projectstringGoogle Vertex AI keys only: the GCP project to call, instead of the one stored with the key.
x-vertex-regionstringGoogle Vertex AI keys only: the region to call (or
global), instead of the one stored with the key.anthropic-betastringAnthropic beta features, comma-separated (for example
context-management-2025-06-27). Forwarded to Anthropic and Claude on Vertex only; at most 32 names of lower-case letters, digits and dashes.