Count tokens
POST /v1/messages/count_tokens returns the number of input tokens a Messages API request would use, without generating a response. Agents use it to decide when to compact a conversation, and applications use it to enforce prompt budgets or to estimate cost before an expensive call. The endpoint takes the same body as /v1/messages.
Scenario
A coding agent keeps a long conversation and must stay under the model's context window. Before each turn it counts the tokens of the whole conversation through the gateway. When the count crosses a threshold the agent summarises the oldest turns. The count costs nothing at the provider and never touches the key's budget.
Steps
-
Make sure the model is registered. In the Admin UI open Models + Endpoints (
/ui/models-and-endpoints) and check that the public model name exists in Deployed Models. Token counting uses the deployment behind the name to pick the right tokenizer or provider endpoint. -
Count the tokens of a request.
curl -X POST "http://localhost:4000/v1/messages/count_tokens" \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"model": "claude-sonnet-5-5","system": "You are a careful reviewer.","messages": [{"role": "user", "content": "Review this function for bugs: def add(a, b): return a - b"}]}'systemandtoolsare included in the count, because they are part of the prompt the model receives. Athinkingblock in the body is ignored, so the count leaves out any thinking budget. -
Use the count in your application. With the Anthropic SDK call
client.messages.count_tokens(...)against the gateway's base URL and readinput_tokens. -
Count calls do not appear on Logs (
/ui/logs) and write no spend row, so they never count against a budget. A model call made after the count shows up there as usual.
Expected result
{"input_tokens": 61}
How the count is produced
For Anthropic models the gateway forwards the request to Anthropic's own token counting endpoint, so the number matches what the model will bill. For models from other providers the gateway counts with the provider's tokenizer where one is available, and otherwise with its bundled tokenizer, which gives a close estimate. The response is not cached, the call is not charged by providers, and the gateway writes no log row for it. A model name that no deployment carries is not an error here. The gateway falls back to its bundled tokenizer and answers 200 with an estimate, so check the name with GET /v1/models when the number looks off.
Errors
| Status | What it means | Fix |
|---|---|---|
400 {"detail": {"error": "model parameter is required"}} or messages parameter is required | model or messages is missing. | Send both fields, as for /v1/messages. |
401 auth_error | The key is missing, wrong or expired. The body has the OpenAI shape, {"error": {...}}. | Use a live key from Virtual Keys. |
| 403 | The key may not use this model (The requested model '<model>' is not available for this API key, or the model name is invalid.). | Add the model to the key's Models. |
| 503 | Token counting is turned off on this gateway. | Ask the admin to turn it back on. |
Related
- Anthropic Messages for the full request format.
- Responses API for
POST /v1/responses/input_tokens, the same service for Responses API requests. - Token counter and transform request for
POST /utils/token_counter, which counts tokens for any model and format.
Providers
The gateway's endpoint support matrix marks 4 provider integrations as supporting this endpoint:
Anthropic, AWS - Bedrock, Google AI Studio - Gemini, Google - Vertex AI.