POST/v1beta/models/{model}:countTokens

Count tokens (Gemini)

Estimates input tokens with the gateway's own tokenizer. Free and never billed; an estimate, not the provider's count.

Authentication: Bearer API key in the Authorization header

Path parameters

  • modelstringrequired

    A model slug.

Request body

  • contentsarray<object>required

    Turns of parts: text, inlineData, public fileData URLs, functionCall, functionResponse.

  • systemInstructionobject
  • toolsarray<object>

    functionDeclarations only; Google server tools are refused.

  • toolConfigobject
  • generationConfigobject

    Sampling, maxOutputTokens, stopSequences, responseSchema or responseJsonSchema, thinkingConfig.

  • providerobject

    OpenRouter's routing-preference object, resolved against the model's own provider routes. Only tightens, never loosens, your organization's policy.

    provider
    • zdrboolean

      true serves only zero-data-retention routes for this request, or refuses with a 403 naming the excluded providers.

    • onlyarray<string>

      Keep only these providers.

    • ignorearray<string>

      Drop these providers.

    • orderarray<string>

      Try these providers first, in order; the rest stay as fallbacks.

    • allow_fallbacksboolean

      false with order serves only the listed providers; without order, only the first eligible route.

Responses

  • 200

    The estimate.

    Response
    • totalTokensinteger
    • x-experiential-ignored-parametersarray<string>
  • 400

    Invalid request, in the Google envelope.

    Response
    • errorobject
      error
      • codeinteger

        The HTTP status.

      • messagestring
      • statusstring

        For example INVALID_ARGUMENT, UNAUTHENTICATED, RESOURCE_EXHAUSTED.

      • detailsarray<object>
  • 401

    The key is missing or invalid.

    Response
    • errorobject
      error
      • codeinteger

        The HTTP status.

      • messagestring
      • statusstring

        For example INVALID_ARGUMENT, UNAUTHENTICATED, RESOURCE_EXHAUSTED.

      • detailsarray<object>

Request

curl https://api.experientiallabs.ai/v1beta/models/gpt-6-luna:countTokens \
  -H "x-goog-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"contents": [{"role": "user", "parts": [{"text": "Say hi."}]}]}'

Response

{
  "totalTokens": 3,
  "x-experiential-ignored-parameters": [
    "input_tokens->estimated(gateway_tokenizer)"
  ]
}

https://api.experientiallabs.ai/v1beta/models/gpt-6-luna:countTokens