By Sagar Shankaran, Founder of CallSphere
Explore how to leverage Gemini's massive 1 million token context window for analyzing entire codebases, processing full books, and building agents that reason over large document collections.
Key takeaways
Most large language models operate with context windows between 8K and 128K tokens. Gemini 2.0 Pro supports up to 1 million tokens of input — roughly 700,000 words or an entire codebase of 30,000 lines. This eliminates the need for complex chunking, retrieval augmented generation, or summarization pipelines in many use cases.
For agent developers, long context means your agent can reason over an entire repository, a complete legal contract, or hours of meeting transcripts without losing information to truncation or retrieval errors. The model sees everything at once.
Before loading large documents, understand how tokens map to your content:
flowchart LR
INPUT(["User intent"])
PARSE["Parse plus<br/>classify"]
PLAN["Plan and tool<br/>selection"]
AGENT["Agent loop<br/>LLM plus tools"]
GUARD{"Guardrails<br/>and policy"}
EXEC["Execute and<br/>verify result"]
OBS[("Trace and metrics")]
OUT(["Outcome plus<br/>next action"])
INPUT --> PARSE --> PLAN --> AGENT --> GUARD
GUARD -->|Pass| EXEC --> OUT
GUARD -->|Fail| AGENT
AGENT --> OBS
style AGENT fill:#4f46e5,stroke:#4338ca,color:#fff
style GUARD fill:#f59e0b,stroke:#d97706,color:#1f2937
style OBS fill:#ede9fe,stroke:#7c3aed,color:#1e1b4b
style OUT fill:#059669,stroke:#047857,color:#fff
import google.generativeai as genai
import os
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
model = genai.GenerativeModel("gemini-2.0-pro")
# Count tokens before sending
sample_text = open("large_document.txt").read()
token_count = model.count_tokens(sample_text)
print(f"Document tokens: {token_count.total_tokens}")
print(f"Percentage of context used: {token_count.total_tokens / 1_000_000 * 100:.1f}%")
A rough rule of thumb: 1 token is approximately 4 characters of English text, or about 0.75 words. A 500-page book is typically 200K-400K tokens.
Hear it before you finish reading
Talk to a live CallSphere AI voice agent in your browser — 60 seconds, no signup.
Here is a practical pattern for loading a full project into Gemini's context:
import os
import google.generativeai as genai
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
def load_codebase(root_dir: str, extensions: set = None) -> str:
"""Load all source files from a directory into a single string."""
if extensions is None:
extensions = {".py", ".ts", ".js", ".sql", ".yaml", ".json"}
files_content = []
for dirpath, dirnames, filenames in os.walk(root_dir):
# Skip common non-source directories
dirnames[:] = [d for d in dirnames if d not in {
"node_modules", ".git", "__pycache__", ".venv", "dist"
}]
for filename in sorted(filenames):
if any(filename.endswith(ext) for ext in extensions):
filepath = os.path.join(dirpath, filename)
relative = os.path.relpath(filepath, root_dir)
try:
content = open(filepath).read()
files_content.append(
f"=== FILE: {relative} ===\n{content}\n"
)
except (UnicodeDecodeError, PermissionError):
continue
return "\n".join(files_content)
codebase = load_codebase("./my-project")
model = genai.GenerativeModel("gemini-2.0-pro")
token_count = model.count_tokens(codebase)
print(f"Codebase size: {token_count.total_tokens} tokens")
Once the content is loaded, you can ask complex analytical questions:
model = genai.GenerativeModel(
"gemini-2.0-pro",
system_instruction="""You are a senior software architect reviewing a codebase.
Identify architectural patterns, potential issues, and improvement opportunities.
Always reference specific file paths and line numbers in your analysis.""",
)
response = model.generate_content([
f"Here is the complete codebase:\n\n{codebase}",
"\nAnalyze the error handling patterns across this codebase. "
"Which files have inconsistent error handling? "
"What patterns should be standardized?"
])
print(response.text)
When you need to ask multiple questions about the same large document, context caching avoids re-sending the full content each time:
# Create a cached context
cache = genai.caching.CachedContent.create(
model="models/gemini-2.0-flash",
display_name="project-codebase",
system_instruction="You are a code review assistant.",
contents=[codebase],
)
# Use the cache for multiple queries — much faster and cheaper
model = genai.GenerativeModel.from_cached_content(cache)
response1 = model.generate_content("List all API endpoints in this codebase.")
response2 = model.generate_content("Find any SQL injection vulnerabilities.")
response3 = model.generate_content("What database migrations are pending?")
# Clean up when done
cache.delete()
Context caching reduces costs by up to 75% for repeated queries against the same base content. The cache has a minimum size of 32K tokens and a default TTL of 1 hour.
Still reading? Stop comparing — try CallSphere live.
CallSphere ships complete AI voice agents per industry — 14 tools for healthcare, 10 agents for real estate, 4 specialists for salons. See how it actually handles a call before you book a demo.
When your content approaches the 1M token limit, apply these strategies:
def prioritized_load(root_dir: str, priority_dirs: list, max_tokens: int = 900_000):
"""Load high-priority directories first, stopping near the token limit."""
model = genai.GenerativeModel("gemini-2.0-pro")
accumulated = []
total_tokens = 0
for priority_dir in priority_dirs:
dir_content = load_codebase(os.path.join(root_dir, priority_dir))
count = model.count_tokens(dir_content).total_tokens
if total_tokens + count > max_tokens:
break
accumulated.append(dir_content)
total_tokens += count
return "\n".join(accumulated), total_tokens
Yes. Latency increases with input size, roughly linearly. A 1M token request takes significantly longer than a 10K token request. For interactive agents, consider whether the full context is necessary or if a targeted subset would suffice.
Context caching is available for Gemini 2.0 Flash and Gemini 2.0 Pro through both AI Studio and Vertex AI. The cached content must be at least 32,768 tokens and has a configurable TTL with a minimum of 1 minute.
Long context is simpler and avoids retrieval errors — the model sees everything. RAG is necessary when your data exceeds the context window or changes frequently. For documents under 500K tokens, long context typically produces more accurate answers because the model can cross-reference information across the entire document without depending on retrieval quality.
#GoogleGemini #LongContext #DocumentAnalysis #Python #AIAgents #AgenticAI #LearnAI #AIEngineering

Written by
Sagar Shankaran· Founder, CallSphere
LinkedInSagar Shankaran is the founder of CallSphere, where he builds production AI voice and chat agents deployed across healthcare, hospitality, real estate, and home services. He writes about agentic AI, LLM engineering, and shipping voice agents that handle real calls in production.
See how AI voice agents work for your industry. Live demo available -- no signup required.
A founder's guide to the personal AI assistant market: best AI assistant apps, business-grade options, and how CallSphere's voice agent fits in.
A founder's guide to free AI agents, low-code AI agent builders, and how to know when you should pay for a real platform like CallSphere.
Graphiti is the open-source temporal knowledge graph for AI agents in 2026. Learn how bi-temporal memory beats vector RAG for voice agents and long-running LLMs.
Chatbot app vs ChatGPT in 2026: a founder's clear take on the difference, when to use which, and how a real AI chatbot app development works.
How we built a fault-tolerant HVAC emergency triage and tech-dispatch platform on Kubernetes — three-tier CQRS, 11 micro-agents on the OpenAI Agents SDK + LangGraph, NATS JetStream, DTMF/SMS/WebSocket acceptance, circuit breakers, and an evaluation pipeline that catches regressions before they wake a tech at 3 AM.
Gemini 3.1 Ultra ships with a 2-million token context window and full text, image, audio, and video multimodality. What changes and how to build for it.
© 2026 CallSphere Inc. All rights reserved.
Made within San Francisco
Watch how CallSphere handles real customer calls, schedules appointments, and processes payments — live.