TokenLab

Gemini Native

Count Tokens

Counts the input tokens of a Gemini request without generating or billing.

POST
/v1beta/models/{model}:countTokens

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

contentsarray

Conversation contents in the same shape as Generate Content.

generateContentRequestobject

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
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

200 OK
{
  "totalTokens": 7
}
totalTokensinteger
Number of input tokens in the request.

Authorization

AuthorizationBearer <token>

API Key authentication. Create or manage API keys in Dashboard > API > API Keys.

In: header

Path Parameters

model*string

Model name (e.g., gemini-2.5-pro)

Query Parameters

key?string

API key (alternative to header auth)

Request Body

application/json

Response

application/json

application/json

application/json

application/json