Count Message Tokens
POST
/v1/messages/count_tokensCounts tokens for an Anthropic Messages-compatible request body.
Use this endpoint when a client needs the Anthropic Messages token count request shape before sending a generation request. The request body follows /v1/messages semantics.
Authentication
Use either header:
Authorization: Bearer sk-your-api-key
x-api-key: sk-your-api-key
If you use x-api-key, also send the Anthropic version header expected by Anthropic-compatible clients:
anthropic-version: 2023-06-01
Request Body
Parameter model
Claude model ID available to the current API key.
Parameter messages
Messages array using the Anthropic Messages format.
Parameter system
System prompt.
Parameter tools
Tool definitions included in the token count.
Response
The response uses the Anthropic-compatible token count shape returned by the routed provider.
Response Field input_tokens
Input token count.
Request Example
curl -X POST "https://api.anypath.dev/v1/messages/count_tokens" \
-H "x-api-key: sk-your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [
{"role": "user", "content": "Summarize this support case."}
]
}'