Count Tokens
POST
/v1beta/models/{model}:countTokensReturns a gateway token estimate for a Gemini-compatible request body.
countTokens accepts Gemini-compatible content shapes and returns the gateway’s token estimate for that request body. In the current gateway implementation this is an AnyPath estimate, not an upstream provider billing statement.
Use settlement records and billing exports for charged usage.
Path Parameters
Parameter model
Gemini-compatible model name available to the current API key.
Authentication
Gemini /v1beta endpoints accept:
Authorization: Bearer sk-your-api-keyx-goog-api-key: sk-your-api-key?key=sk-your-api-key
Request Body
Send exactly one of these shapes:
Parameter contents
Gemini content array.
Parameter generateContentRequest
Nested generateContent request object containing contents.
Response
Response Field totalTokens
Estimated token count.
Response Field cachedContentTokenCount
Cached content token count when available.
Request Example
curl -X POST "https://api.anypath.dev/v1beta/models/gemini-2.5-flash:countTokens?key=sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"role": "user",
"parts": [{"text": "Summarize this support case."}]
}
]
}'
Response Example
{
"totalTokens": 8,
"cachedContentTokenCount": null,
"promptTokensDetails": [],
"cacheTokensDetails": []
}