Gemini Context Window Exceeded
Your prompt exceeded the model's context window: 2M tokens on gemini-2.5-pro, 1M on gemini-2.5-flash and flash-lite. Multimodal inputs eat tokens fast — video ~300 tok/sec, audio ~32 tok/sec, image ~258 tok. Fix by counting tokens pre-flight, trimming, or moving to Pro for larger context.
Call client.models.count_tokens() before generate_content. If total > window minus max_output_tokens minus thinking_budget, trim. For long docs, chunk with overlap. For video, trim with start_offset/end_offset. For Flash overflow, route to Pro (2M window).
The Error Messages You're Seeing
HTTP/1.1 400 Bad Request
{
"error": {
"code": 400,
"message": "The input token count (1547328) exceeds the
maximum number of tokens allowed (1048576) for model
'gemini-2.5-flash'.",
"status": "INVALID_ARGUMENT"
}
}
Variants:
"Combined input and output token count exceeds maximum context
window of 2097152 tokens for gemini-2.5-pro."
"Total tokens including thinking_budget cannot exceed context
window."
"Video content exceeds 45-minute duration limit for default
sampling."Context Windows & Multimodal Token Costs
| Model | Context window | Max output | Max thinking |
|---|---|---|---|
| gemini-2.5-pro | 2,097,152 (2M) | 65,536 | 32,768 |
| gemini-2.5-flash | 1,048,576 (1M) | 65,536 | 24,576 |
| gemini-2.5-flash-lite | 1,048,576 (1M) | 65,536 | 24,576 |
| gemini-2.0-flash | 1,048,576 (1M) | 8,192 | N/A |
| Multimodal token rates (approximate) | |||
| Image | 258 tokens (default) | - | MEDIA_RES_HIGH: up to 1290 |
| Video @ 1fps | ~300 tokens/sec | - | Audio adds ~32/sec on top |
| Audio | ~32 tokens/sec | - | Separately if extracted |
| PDF page | ~258 (as image) | - | Plus text tokens |
What Actually Causes This Error
Fixes That Work (Tested Nov 2026)
1Pre-Flight Token Count
2Chat History Trimming
If a workload's tokens fit in Flash's 1M window, use it — Pro's 2M costs ~3× more per token. Build router that checks size, uses Flash under 900K tokens, Pro above. Cost savings compound at scale.
3Video Trimming
Preventing This Error Going Forward
- Always count_tokens before generate_content on large inputs.
- Reserve output + thinking budget in your budget math.
- Trim chat history proactively; don't wait for overflow.
- Use context caching for repeated large contexts. (See related error.)
- Route to Pro for anything > 900K tokens. Flash margin thin.
- Use MEDIA_RESOLUTION_LOW when content isn't detail-critical.
- For video, transcribe first, pass audio as text where possible.
Researcher · AI Error Hub
Frequently Asked Questions
count_tokens is free (no billed input/output). Use liberally as a pre-flight check. Small latency cost (~50ms) — cache the result if you call generate_content immediately after.
Yes — system instructions occupy input tokens like any other content. Long system prompts + long docs quickly consume budget. Compress system instructions; use context caching for stable prefix.
Practically ~1.9M input tokens after reserving 65K output + 32K thinking + safety margin. Quality also degrades at extreme lengths — for critical retrieval, keep relevant content in top or bottom portion of context (not middle).
Yes — tool schemas count. Verbose function definitions with many tools can hit 5-15K tokens easily. Trim descriptions, minimize enums where possible.
Yes — context caching (see related error). Once cached, subsequent calls reference the cache at a fraction of the cost (~75% discount on cached input tokens). Great for long docs referenced across many queries.
Related Errors
Master Gemini's 2M Context
Weekly deep dives on long-context strategies, caching, and multimodal token math. 12,000+ developers subscribe.
Subscribe Free →