Gemini Context Window Exceeded — Fix (2026) | AI Error Hub
GeminiContextSeverity: MediumHTTP 400

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.

Error code: INVALID_ARGUMENTHTTP: 400Category: ContextLast tested: Nov 15, 2026
Quick Fix (TL;DR)

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

Response body variants
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

ModelContext windowMax outputMax thinking
gemini-2.5-pro2,097,152 (2M)65,53632,768
gemini-2.5-flash1,048,576 (1M)65,53624,576
gemini-2.5-flash-lite1,048,576 (1M)65,53624,576
gemini-2.0-flash1,048,576 (1M)8,192N/A
Multimodal token rates (approximate)
Image258 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

32%
Long video without trimming1hr video ≈ 1.08M tokens — hits Flash 1M cap.
24%
Massive PDF or document500+ page PDF pushes past 1M easily.
18%
Chat history accumulationLong conversation never trimmed; adds up.
12%
Ignoring output + thinking reservationFilled context to max; no room for output.
8%
Wrong model for sizeSending 1.5M-token doc to Flash instead of Pro.
6%
High-res image mode on many imagesMEDIA_RES_HIGH × 30 images = 39K tokens.

Fixes That Work (Tested Nov 2026)

1Pre-Flight Token Count

Python · always count before sending
from google import genai from google.genai import types client = genai.Client() MODEL_LIMITS = { "gemini-2.5-pro": {"context": 2_097_152, "output": 65_536}, "gemini-2.5-flash": {"context": 1_048_576, "output": 65_536}, "gemini-2.5-flash-lite": {"context": 1_048_576, "output": 65_536}, } def check_fits(model, contents, max_output=8192, thinking_budget=0): limit = MODEL_LIMITS[model] resp = client.models.count_tokens(model=model, contents=contents) input_tokens = resp.total_tokens reserved = max_output + thinking_budget + 1000 # safety margin budget = limit["context"] - reserved return { "input_tokens": input_tokens, "budget": budget, "fits": input_tokens <= budget, "overflow": max(0, input_tokens - budget), } check = check_fits("gemini-2.5-flash", my_contents, max_output=4096) if not check["fits"]: print(f"Overflow by {check['overflow']} tokens")

2Chat History Trimming

Python · trim old messages, keep recent
def trim_conversation(model, history, system, target_budget): """Drop oldest turns until conversation fits.""" while history: contents = ([{"role": "user", "parts": [{"text": system}]}] + history) tokens = client.models.count_tokens( model=model, contents=contents ).total_tokens if tokens <= target_budget: return history # Drop oldest user+assistant pair history = history[2:] return [] # Even better: summarize dropped turns instead of discarding def trim_with_summary(model, history, system, budget): if len(history) < 10: return history # Summarize older half of history older = history[:len(history) // 2] recent = history[len(history) // 2:] summary_resp = client.models.generate_content( model="gemini-2.5-flash-lite", contents=[{"role": "user", "parts": [{ "text": f"Summarize this conversation compactly: {older}" }]}], config=types.GenerateContentConfig(max_output_tokens=1000) ) return [{ "role": "user", "parts": [{"text": f"Previous conversation: {summary_resp.text}"}] }] + recent
Route by size, save cost

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

Python · reduce video token cost
# Video ~ 300 tokens/sec at default 1fps sampling # 45-min video = 810,000 tokens (fits Flash 1M with room) # 90-min video = 1.6M tokens (needs Pro's 2M) # Option A: trim to specific range video_part = types.Part( file_data=types.FileData(file_uri="gs://bucket/video.mp4"), video_metadata=types.VideoMetadata( start_offset="120s", # skip first 2 min end_offset="600s", # stop at 10 min ) ) # Option B: reduce media resolution (fewer tokens per frame) config = types.GenerateContentConfig( media_resolution="MEDIA_RESOLUTION_LOW" # ~1/4 tokens ) # Option C: custom fps for video video_part = types.Part( file_data=types.FileData(file_uri="gs://bucket/vid.mp4"), video_metadata=types.VideoMetadata(fps=0.5) # half fps → half tokens ) # Option D: transcribe audio separately (~32 tok/sec) # and pass transcript text instead of full video

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.
SK
Tested by Sana K.
Researcher · AI Error Hub
Last verified: Nov 15, 2026 · SDK: google-genai 0.8+

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.