How to Feed a Long Document into Gemini's Long Context
Use Gemini's large context window to analyze a whole book or a stack of PDFs in one prompt instead of chunking it.
Gemini's long context window lets you put an entire book, a long transcript, or many PDFs into a single request, so the model can answer across the whole thing without you splitting it into pieces. This guide loads a large PDF and asks questions that span the full document.
What you need
- A Gemini API key
- The google-genai Python SDK installed
- One or more long PDFs or text files
- Awareness that very large inputs cost more tokens per request
Step 1: Upload the document
For files over a few megabytes, upload through the File API rather than inlining the bytes. Smaller PDFs can be passed inline, but the File API is the reliable path for long documents.
import os
from google import genai
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
doc = client.files.upload(file="annual-report.pdf")Step 2: Ask a question that spans the whole file
The point of long context is questions that need information from many places at once. Ask the model to compare sections, trace a theme, or pull every mention of something across hundreds of pages.
response = client.models.generate_content(
model="gemini-2.5-pro",
contents=[
doc,
"List every figure mentioned for total revenue across all "
"quarters, with the page number for each.",
],
)
print(response.text)Step 3: Check how many tokens you used
Long inputs add up fast. Read the usage metadata on the response to see how many tokens the document consumed, which helps you estimate cost and decide whether to enable context caching for repeat queries.
u = response.usage_metadata
print("prompt tokens:", u.prompt_token_count)
print("output tokens:", u.candidates_token_count)
print("total tokens:", u.total_token_count)Result
You get answers that draw on the entire document in one shot, complete with page references, without writing any chunking or retrieval code. For one-off deep reads this is far simpler than building a retrieval pipeline.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31