Sentence Window Retrieval
What you'll learn: How to retrieve small, precise text chunks while dynamically expanding their surrounding context for better LLM understanding.
The Core Idea
Sentence Window Retrieval solves a key RAG dilemma: precise retrieval versus sufficient context. Instead of choosing a fixed chunk size upfront, you retrieve at the sentence level (very granular) but then dynamically expand the context window around matching sentences based on relevance scores.
Think of it like using a magnifying glass to find the exact sentence you need, then zooming out to include surrounding paragraphs—but only when the match is strong enough to warrant more context.
How It Works
- Index at sentence granularity — Each sentence becomes its own retrievable unit with an embedding
- Retrieve the most relevant sentences — Your vector search returns individual sentences with similarity scores
- Expand context dynamically — For high-scoring sentences, include N sentences before and after (the "window")
- Pass enriched context to LLM — The model receives the precise sentence plus surrounding paragraphs for coherence
The window size can scale with relevance: a 0.95 similarity score might expand ±5 sentences, while 0.75 expands only ±2.
Why This Matters
- Precision: You retrieve exactly what matches, not arbitrary 500-token chunks
- Context: The LLM still gets enough surrounding text to understand references, pronouns, and flow
- Flexibility: Window expansion adapts per query—complex questions get more context automatically
Key Takeaway: Sentence Window Retrieval gives you surgical precision during search while preserving contextual coherence through dynamic expansion, optimizing both retrieval accuracy and LLM comprehension.