Need authoritative preflight fit guarantee for countTokens vs generateContent with audio

I’m building a system that uses gemini-3.8-flash through the Gemini Developer API, and I need to determine, before calling generateContent, whether a complete multimodal request is guaranteed to fit the model’s input/context limit.

Our intended request contains:

  • a system instruction;
  • textual content;
  • multiple ordered inline WAV/audio parts (up to 16 within one request);
  • generation configuration;
  • response-format/schema configuration where applicable.

We can call:

POST /v1beta/models/{model}:countTokens

using the generateContentRequest form with the same request contents/configuration intended for generateContent.

The public documentation and examples appear to show that countTokens.totalTokens and the later generation promptTokenCount can differ. We therefore do not want to assume that countTokens <= inputTokenLimit by itself guarantees that the request will be accepted.

Could someone from the Gemini API team clarify the authoritative rule?

1. When models.countTokens receives the full generateContentRequest, does totalTokens include every component relevant to input-context admission for the later generateContent request, including multimodal/audio tokenization, media quantization, and any provider-added framing?

2. If not, is there a documented conservative maximum overhead or another supported preflight rule such that:

countTokens.totalTokens + documented_bound <= inputTokenLimit

guarantees that the subsequent generateContent request will fit?

We do not need an exact prediction of final usage or billing. We need a conservative admission guarantee that can be checked before submitting the generation request.

Separately, the Gemini billing FAQ states that the “GetTokens API” is not billed and does not count against inference quota. Does that statement apply specifically to:

POST /v1beta/models/{model}:countTokens

including multimodal/audio requests using the full generateContentRequest form?

If so, are there any separate count-specific quotas, media restrictions, or audio exceptions we should account for?

An attributable Google API-team answer or pointer to the governing documentation would be very helpful.

Hello @Sean_Drewett ,

No, countTokens cannot guarantee that a request will fit under inputTokenLimit, and there is no published safety margin. The reliable check is the request itself: if it is too big, generateContent fails straight away with a 400 error saying “The input token count exceeds the maximum number of tokens allowed”. Catch that error, shorten the input (for example, drop or trim an audio clip), and try again.

  • Why countTokens can be off: countTokens counts your contents (audio at 32 tokens per second), systemInstruction, and tools. It does not count your responseSchema, and a few extra tokens get added to every request. Both of these count toward the limit, so use countTokens as an estimate and stay a bit below the limit, more so if your schema is large. See Understand and count tokens.
  • Cost: Token counting is free and does not use your quota (Billing FAQ). The Rate limits page does not list a separate limit for countTokens.
  • Audio: You can send many audio clips in one request. The limits are 9.5 hours of audio per prompt (Audio understanding) and 20 MB for the whole request when you send files inline. For anything bigger, use the Files API.