Gemini Native
Count Tokens
Counts the input tokens of a Gemini request without generating or billing.
Counts the input tokens of a Gemini request before you send it. The call is not billed and does not need account balance, but it needs an API key. Use it with a model whose Get Model response lists countTokens in supportedGenerationMethods.
Send either contents or a full generateContentRequest. Use the count for planning; the charge for a generation request comes from that response's usageMetadata.
Authentication
Same as Generate Content: ?key=, x-goog-api-key, or Authorization: Bearer.
Request Body
Conversation contents in the same shape as Generate Content.
A full Gemini generate request to count, as an alternative to contents. generate_content_request is also accepted.
Errors
400 INVALID_ARGUMENT: the body is not valid, or the model does not accept Gemini requests.401 UNAUTHENTICATED: the API key is missing or invalid.503 UNAVAILABLE: no route is available for the model right now. Retry later.
Request
curl -X POST "https://api.tokenlab.sh/v1beta/models/gemini-2.5-pro:countTokens" \
-H "x-goog-api-key: sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"parts": [{"text": "Hello, Gemini!"}]
}
]
}'Response
{
"totalTokens": 7
}API Key authentication. Create or manage API keys in Dashboard > API > API Keys.
In: header
Path Parameters
Model name (e.g., gemini-2.5-pro)
Query Parameters
API key (alternative to header auth)
Request Body
application/json
Response
application/json
application/json
application/json
application/json