Gemini token counter
Google exposes a free countTokens endpoint that returns the exact number the Gemini API bills. This counter uses it, so the count matches your invoice rather than approximating it.
Why the exact count matters more on Gemini
Gemini 2.5 Pro has a pricing tier boundary at 200,000 input tokens: prompts above it bill at double the input rate and a higher output rate. An estimate that is off by 15% around that line does not just misprice your request, it can put you on the wrong side of a 2x price step without you noticing. If your prompts run long, count before you send. The tier mechanics are laid out on the Gemini API pricing page.
Counting notes
- Counts are model-specific. Gemini 2.5 Pro and Flash share a context window of roughly a million tokens, but always count against the model you will call.
- The countTokens endpoint is free and does not consume generation quota.
- Window sizes and what fits are on the Gemini 2.5 Pro context window page.
Other counters: Claude · OpenAI · methods in how we count