Docs

Count Tokens

POST/v1beta/models/{model}:countTokens

Returns a gateway token estimate for a Gemini-compatible request body.

On this page
  1. Path Parameters
  2. Authentication
  3. Request Body
  4. Response
  5. Request Example
  6. Response Example

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

Type: string

Location: path

Required: yes

Gemini-compatible model name available to the current API key.

Authentication

Gemini /v1beta endpoints accept:

  • Authorization: Bearer sk-your-api-key
  • x-goog-api-key: sk-your-api-key
  • ?key=sk-your-api-key

Request Body

Send exactly one of these shapes:

Parameter contents

Type: array

Location: body

Gemini content array.

Parameter generateContentRequest

Type: object

Location: body

Nested generateContent request object containing contents.

Response

Response Field totalTokens

Type: integer

Estimated token count.

Response Field cachedContentTokenCount

Type: integer | null

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": []
}