Skip to content
Learning Lab · 5 min read

Context Window Management: Fitting Long Documents Into LLMs

Context window limits break production systems more often than bad prompts do. Learn token counting, extraction-first strategies, and hierarchical summarization to handle long documents and conversations without losing information or exceeding model limits.

Context Window Management: Handle Long Docs in LLMs

Your 50-page legal document gets uploaded to Claude, and the model returns analysis based on page 3. The rest exists in a token void. This isn’t a hallucination problem — it’s a context window problem, and it breaks more production systems than bad prompts do.

Context window is real estate. Every token you use — prompt, documents, conversation history, system instructions — occupies space that could go toward your actual task. Exceed the limit, and the model either stops responding or, worse, starts ignoring the oldest information to make room for new input. Understanding how to manage this space is the difference between a working system and one that mysteriously fails on Thursday when your document got longer.

Understanding Your Context Budget

Start by knowing exactly what you’re working with.

Claude 3.5 Sonnet gives you 200,000 tokens. GPT-4o gives you 128,000 tokens (or 4,096 if you’re on the older base model). Mistral Large offers 32,000 tokens. These numbers sound large until you start measuring real content.

A typical page of business text is roughly 250–350 tokens. A 50-page document costs you 12,500–17,500 tokens before you’ve written a single instruction. A long conversation with 20 exchanges burns 4,000–8,000 tokens. That leaves you with actual working space measured in what’s left over — your “processing budget.”

Calculate your exact token cost before deployment. Use the model provider’s tokenizer:

# Python example using Anthropic's tokenizer
import anthropic

client = anthropic.Anthropic()
with open('document.txt', 'r') as f:
    doc_text = f.read()

response = client.messages.count_tokens(
    model="claude-3-5-sonnet-20241022",
    messages=[{"role": "user", "content": doc_text}]
)

print(f"Document tokens: {response.input_tokens}")
print(f"Remaining budget: {200000 - response.input_tokens}")

This isn’t optional. Guessing costs you failed queries and wasted API spend.

The Three Compression Techniques That Actually Work

You have three levers: extract what matters, summarize strategically, or break the work into chunks. Most people try to skip straight to summarization and fail because they’re summarizing the wrong parts.

1. Extraction Before Summarization

Don’t summarize everything. Extract the sections relevant to your actual question first.

If you’re analyzing a contract for payment terms, you don’t need summaries of the company history or product specifications. Pull the relevant clauses, pass those to the LLM, and keep 80% of your context window free.

Bad approach:

# Summarize the entire 50-page document
Prompt: "Summarize this legal document in 500 words."
[50-page document]
"What are the payment terms?"

Good approach:

# Extract relevant sections, then answer
Prompt: "Extract all sections mentioning 'payment', 'invoice', 'due date', 
or 'terms'. Keep the exact wording."  
[50-page document]

# Then in a second call or same call with extracted text:
Prompt: "Based on these sections, what are the payment terms and when 
are invoices due?"
[Extracted sections only - ~2000 tokens instead of 15000]

Use keyword extraction, regex patterns, or a lightweight search pass (Claude Mini on shorter docs) to identify relevant chunks before passing to your main analysis model.

2. Hierarchical Summarization

If extraction isn’t viable — say, you need the whole context — summarize in layers. Don’t summarize 50 pages to 500 words in one pass. Summarize chunks to summaries, then summarize those.

Break the document into 5–10 page chunks. Summarize each to 150–200 tokens. Then summarize those summaries. You compress 15,000 tokens down to 500–800 while preserving structure and relationships.

This preserves nuance better than one aggressive summary because the model never discards detail arbitrarily — it condenses proportionally at each layer.

3. Sliding Window Processing for Conversations

Long conversations fill context fast. Don’t keep the entire history. Keep recent exchanges (last 5–10) plus summaries of older ones.

# Conversation context management
conversation = [
    {"role": "user", "content": "Tell me about AWS pricing"},
    {"role": "assistant", "content": "AWS pricing is..."}
    # ... 15 more exchanges ...
]

# Before API call, trim and summarize
if conversation_token_count > 100000:
    # Keep last 5 exchanges
    recent = conversation[-10:]  # 5 user/assistant pairs
    
    # Summarize everything before that
    older_summary = summarize_conversation(conversation[:-10])
    
    # Reconstruct with explicit history marker
    trimmed = [
        {"role": "system", 
         "content": "Earlier in this conversation: " + older_summary}
    ] + recent

Temperature and Sampling: Hidden Context Problems

You can also manage context efficiency through model behavior, not just input size.

Lower temperature (0.3–0.5) makes the model focus on what it already knows — useful for fact extraction from provided context. Higher temperature (0.7–1.0) increases randomness, which burns tokens on less predictable outputs.

For context-heavy tasks (“answer only from this document”), run at 0.3–0.4. The model’s attention stays on your input, not on exploring alternative phrasings.

Max tokens parameter matters too. Set it deliberately. If you’re extracting payment terms, you don’t need 4,096 token outputs. Set max_tokens=500. This reserves budget for your actual task instead of letting the model ramble into empty space.

The Real Setup: A Production Pattern

Here’s what actually works at scale:

  1. Extraction layer: Use regex, keyword search, or a fast model (Claude 3.5 Haiku) to pull relevant sections from large documents. Cost: minimal.
  2. Token budget check: Count tokens of extracted sections. If under 30,000 tokens with Claude Sonnet, proceed. If not, summarize.
  3. Summarize if needed: Hierarchical summaries of chunks, not aggressive single-pass compression.
  4. Query with context: Pass extracted or summarized material plus explicit instructions: “Answer only based on these sections. Do not use external knowledge.”
  5. Conversation trim: After 10 user messages, snapshot the conversation, summarize it, and start a new window.

The bottleneck is rarely the model. It’s the prep work before the API call.

Start With Token Counting, Not Summarization

Today: measure your actual documents. Run them through the tokenizer for the model you’re using. Calculate how many tokens remain after your document uploads. That remaining number is your actual working budget.

If it’s under 30,000 for Claude Sonnet or under 50,000 for production work, you need to extract, not summarize. If it’s negative, you need a different approach entirely — split documents, use vector search to retrieve only relevant sections, or use a model with a larger window.

Most context problems aren’t solved by better prompts. They’re solved by not asking the model to process more than it needs to.

Batikan
· 5 min read
Topics & Keywords
Share

Stay ahead of the AI curve

Weekly digest of the most impactful AI breakthroughs, tools, and strategies.

Related Articles

Cursor vs GitHub Copilot vs Claude Code: Which Wins for Production Work
Learning Lab

Cursor vs GitHub Copilot vs Claude Code: Which Wins for Production Work

Three AI coding assistants dominate production environments. This isn't a feature list. It's a breakdown of what each actually does, where it fails, and which to use for architecture, boilerplate, and debugging.

· 10 min read
→
Analyze Spreadsheets With Claude and GPT-4o
Learning Lab

Analyze Spreadsheets With Claude and GPT-4o

Claude and GPT-4o can analyze your spreadsheets and CSVs, but only if you structure the data correctly and ask with precision. Learn how to upload files, write analysis prompts, and avoid hallucination pitfalls.

· 2 min read
→
LLM Hallucinations: Why They Happen and 5 Ways to Stop Them
Learning Lab

LLM Hallucinations: Why They Happen and 5 Ways to Stop Them

Why do language models confidently invent facts? Because they predict tokens, not truth. Learn how grounding, constraint prompting, and temperature settings cut hallucination rates from 15%+ to under 5% in production systems.

· 5 min read
→
Freelancer AI Workflows That Actually Increase Billable Hours
Learning Lab

Freelancer AI Workflows That Actually Increase Billable Hours

AI can double your freelance output without replacing your judgment. Learn four production workflows that compress administrative tasks and recover 10+ billable hours per month.

· 6 min read
→
Stop Hallucinating: How RAG Actually Grounds LLMs
Learning Lab

Stop Hallucinating: How RAG Actually Grounds LLMs

RAG grounds LLMs with your actual data, eliminating hallucinations. This guide explains how RAG works in production, why basic setups fail, and the specific patterns that work — with code examples and trade-offs.

· 6 min read
→
Where Your Prompts Go: Data Handling in ChatGPT, Claude, and Gemini
Learning Lab

Where Your Prompts Go: Data Handling in ChatGPT, Claude, and Gemini

ChatGPT stores your data and uses it for training by default. Claude doesn't train on web conversations unless you opt in. Gemini links your chats to your entire Google account. Here's what each model does with your prompts and how to protect sensitive information.

· 4 min read
→

More from Prompt & Learn

Otter vs Fireflies vs tl;dv: Meeting Transcription Shootout
AI Tools Directory

Otter vs Fireflies vs tl;dv: Meeting Transcription Shootout

Three tools promise to transcribe your meetings and extract action items. Only one integrates cleanly with your workflow. Here's the real comparison: Otter vs Fireflies vs tl;dv — accuracy data, pricing breakdowns, and honest pros/cons for each.

· 4 min read
→
Gamma vs Beautiful.ai vs Tome: Slide Generation Tested
AI Tools Directory

Gamma vs Beautiful.ai vs Tome: Slide Generation Tested

I tested Gamma, Beautiful.ai, and Tome on production presentations. Gamma generates fastest but struggles with branding. Beautiful.ai delivers visual consistency and data handling. Tome offers flexibility and collaboration. Here's what actually works in practice — and when each tool wins.

· 11 min read
→
App Store Launches Spike in 2026. AI Tooling Is the Catalyst
AI News

App Store Launches Spike in 2026. AI Tooling Is the Catalyst

Appfigures reports a measurable surge in app launches in 2026, driven by AI development tools that compress timelines from weeks to days. A solo developer with Claude or Mistral can now ship what required a full engineering team in 2022.

· 3 min read
→
Julius AI vs ChatGPT vs Claude for Data Analysis
AI Tools Directory

Julius AI vs ChatGPT vs Claude for Data Analysis

Julius AI, ChatGPT Advanced Data Analysis, and Claude Artifacts all handle data tasks, but execution speed, pricing, and workflow differ significantly. Here's how to pick the right one for your use case.

· 4 min read
→
Perplexity vs Google AI vs Consensus: Which Wins for Academic Research
AI Tools Directory

Perplexity vs Google AI vs Consensus: Which Wins for Academic Research

Perplexity, Google AI, and Consensus each excel at different research tasks. Perplexity wins on recent topics with real-time synthesis. Consensus delivers unmatched citation precision for peer-reviewed work. Google Scholar provides historical depth. This breakdown shows exactly which tool to use for your next paper—and why.

· 10 min read
→
Google’s Travel Tools Cut Planning Time in Half. Here’s What Actually Works
AI Tools Directory

Google’s Travel Tools Cut Planning Time in Half. Here’s What Actually Works

Google released seven integrated travel tools this spring. Price tracking predicts optimal booking windows, restaurant availability pulls real-time data, and offline maps work without cell coverage. Here's which features earn trust and where to set expectations.

· 3 min read
→

Stay ahead of the AI curve

Weekly digest of the most impactful AI breakthroughs, tools, and strategies. No noise, only signal.

Follow Prompt Builder Prompt Builder