In this categoryGemini ยท 24
- How to Install the Gemini CLI and Run Your First PromptStart
- How to Get a Gemini API Key from Google AI Studio
- How to Call the Gemini API from Python
- How to Give Gemini CLI Project Context with a GEMINI.md File
- How to Choose the Right Gemini Model for Coding Tasks
- How to Fix a Bug in Your Codebase with the Gemini CLI
- How to Set Up Gemini Code Assist in VS Code
- How to Stream Gemini Responses in Node.js
- How to Feed a Long Document into Gemini's Long Context
- How to Run the Gemini CLI Non-Interactively in Scripts
- How to Analyze a Video File with the Gemini API
- How to Connect an MCP Server to the Gemini CLI
- How to Fix Common Gemini CLI Authentication Errors
- How to Cache Long Context in the Gemini API to Cut Costs
- How to Summarize a YouTube Video with Gemini
- How to Analyze PDFs and Docs in the Gemini Chat App
- How to Extract Data from Images and Screenshots with Gemini
- How to Generate a Short Video with Veo in Gemini
- How to Ground Gemini Answers in Your Own Documents
- How to Use Gemini Inside Google Docs to Draft Faster
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
16:12
3:58:44
2:14
23:41
12:00
09:00New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.