Sentence-Window Retrieval
Embed single sentences, return a window around the hit — precision retrieval without losing context.
Sentence-window retrieval embeds individual sentences for precise vector search, then expands each hit by ±N neighbouring sentences before generation. The search unit and the generation unit are deliberately different sizes: the sentence is the needle; the window is the context the model reads. The price is one embedding per sentence plus prompt tokens that grow with window size and top-k. The chunking-side family lives on hierarchical and parent-document chunking; this page is the retrieval mode.
How does sentence-window retrieval work?
Sentence-window retrieval builds sentence-level retrieval atoms at ingest and expands them at query time before the generator runs.
- Split, embed, and store a window. The document is split into sentences. Each sentence is embedded and indexed. Alongside the sentence text, the parser stores a surrounding window in node metadata — LlamaIndex’s SentenceWindowNodeParser uses the keys window and original_text (live LlamaIndex API reference, as of July 2026). The stored window spans sentences from i − window_size through i + window_size, inclusive of the hit.
- Search the sentence embeddings. The query vector ranks sentence nodes. Small units keep similarity sharp; a section-sized chunk that mixes topics would blur the match (Guillaume Laforge, Advanced RAG — Sentence Window Retrieval, February 2025; Rushank Savant, DEV Community).
- Replace the hit with the window before generation. A post-processor such as LlamaIndex’s MetadataReplacementPostProcessor (TruLens Sentence Window cookbook; Grayson Adkins notebook, 2024) swaps the matched sentence for the stored window text so the LLM sees the neighbours. LangChain4j-style injectors do the same job from segment metadata (Laforge, 2025).
Worked shape: a query about when the lunar module landed hits the sentence with the UTC timestamp; the window supplies the surrounding lines that identify “the Eagle” as the lunar module (Savant’s spaceflight example). The Berlin population case is the same pattern — the hit may miss the city name, but the window resolves the pronoun its (Laforge, 2025).
What does the window size control?
The window size is the runtime knob that decides how much local context rides with each hit — it is not a second embedding model.
- window_size counts sentences on each side. LlamaIndex’s SentenceWindowNodeParser documents window_size as the number of sentences on each side of the matched sentence. The library default is 3 (DEFAULT_WINDOW_SIZE in llama-index-core, live source as of July 2026). With room at both edges, that yields up to 7 sentences in the expanded window: three before, the hit, and three after. TruLens and Grayson Adkins demos call from_defaults(window_size=3) explicitly.
- Smaller vs larger. A tight window keeps precision and limits distractors; a wider window fills pronouns and adjacent exceptions — and spends more prompt tokens (Pratik Saha, Medium, November 2024).
- Asymmetry is allowed. Custom transformers can store different forward and backward counts — Laforge’s LangChain4j demo uses two sentences before and three after. The LlamaIndex default is symmetric; production corpora sometimes need unequal shapes (FutureAGI sentence-window glossary common mistakes).
One global window for every corpus is the wrong default: policy pages, API docs, and transcripts do not need the same neighbourhood (FutureAGI).
What does sentence-window retrieval cost?
The costs are structural — denser embeddings and larger prompts — even before you price a specific embedding API.
- One embedding per sentence at ingest. Every sentence becomes a retrieval atom. That is denser than indexing section-sized parents alone; embedding spend tracks sentence count, not window width.
- Prompt tokens scale with window × top-k. Each hit expands to roughly 2×window_size+1 sentences when edges allow. Raising top-k multiplies that expansion across hits (Saha, 2024; FutureAGI “not a free lunch”).
- Optional structure store. Some designs keep neighbour IDs in a separate document-structure store and fetch them at expand time (AI Engineering Academy sentence-window diagram). LlamaIndex’s common path stores the window string in metadata instead, so the expand step is a metadata swap rather than a second vector query.
Measure on your corpus — ignore unsourced lift tables
Vendor glossaries and single-notebook demos sometimes publish ContextPrecision or Answer Relevance percentage lifts without a reproducible multi-corpus method. Treat those as marketing or one-off lab notes, not citations. The structural facts above hold regardless: denser sentence embeddings, an expand step, and prompt tokens that grow with window size. Family-level cost framing continues on hierarchical chunking.
What goes wrong with sentence-window retrieval?
Most failures are window shape and measurement — not a broken idea. Six modes show up repeatedly in live guides:
- Returning the matched sentence alone. Pronouns, negations, and eligibility exceptions often live in neighbouring sentences; without expansion the generator guesses (Laforge, 2025; FutureAGI).
- One fixed window for every corpus. API docs, policies, and transcripts need different forward/backward shapes (FutureAGI).
- Enlarging the window without measuring cost. Larger windows raise token spend and can bury the useful sentence beside distractors (Saha, 2024; FutureAGI).
- Scoring only the final answer. A grounded-looking answer can still rest on a poorly focused window if window-level precision is never checked (FutureAGI).
- Windows that cross PDF page breaks. Captions and table headers drop out of the local neighbourhood (FutureAGI).
- Facts outside ±N. Sentence-window retrieval is local. Dispersed context across distant chapters needs parent-document or auto-merging retrieval instead (Saha, 2024).
When the true answer still spans distant sections after a sensible window, you still have a chunk-boundary SPLIT — windowing changes the odds for local orphans; it does not remove every boundary failure.
When should you use sentence-window retrieval?
Use sentence-window retrieval when answers live in a tight local neighbourhood — and skip it when the small sentence already is the whole context, or when the needed facts are far apart.
- Strong fit: flatter prose (policies, FAQs, technical docs) with pronouns and adjacent exceptions; systems that retrieve the right sentence but answer thinly; teams that want a runtime window_size knob without pre-defining section parents (FutureAGI; Saha, 2024; Laforge, 2025).
- Weak or harmful fit: short self-contained one-liners; chats without stable sentence order; long documents whose relevant facts are scattered across chapters (Saha, 2024); corpora whose natural parents are real headings — start from structure-aware chunking and parent-document retrieval instead.
Prove the choice on the same queries with and without window expansion rather than adopting the pattern by name — measure under RAG evaluation.
How is sentence-window different from parent-document and auto-merging?
All three are small-to-big. They differ in how the “big” unit is defined and when it is assembled.
- Sentence-window retrieval embeds sentences and expands each hit by ±N neighbours at query time — the window size is a runtime knob (this page).
- Parent-document retrieval fixes parent and child boundaries at index time and returns the pre-defined parent of each child hit. Mode depth continues on parent-document retrieval.
- Auto-merging retrieval builds a multi-level tree and merges sibling leaves upward when enough of them hit. Mode depth continues on auto-merging retrieval.
The chunking-side survey of the family — including when overlap is enough — lives on hierarchical and parent-document chunking.
How do you implement sentence-window retrieval?
LlamaIndex’s canonical path is SentenceWindowNodeParser.from_defaults(window_size=…) plus MetadataReplacementPostProcessor(target_metadata_key="window") on the query engine (live LlamaIndex docs; TruLens cookbook, as of July 2026). LangChain demos often approximate the same pattern with sentence-sized children and window-sized parents via ParentDocumentRetriever (Savant, DEV) or a custom metadata injector (Laforge LangChain4j, 2025). Dual setups work with stores such as Weaviate, Qdrant, Pinecone, and Chroma for the sentence layer, with the window text usually riding in metadata rather than a second vector index. For a minimal runnable pipeline see building the pipeline; for the small-to-big family see hierarchical chunking; for the retrieval cluster see retrieval; measure before and after under evaluation.
What is sentence-window retrieval?
Sentence-window retrieval embeds individual sentences for precise vector search, then expands each hit by ±N neighbouring sentences before generation. The search unit and the generation unit are deliberately different sizes (small-to-big): precision from the sentence, context from the window.
What does window_size mean?
In LlamaIndex’s SentenceWindowNodeParser, window_size is the number of sentences on each side of the matched sentence. The library default is 3 (DEFAULT_WINDOW_SIZE), so a mid-document hit expands to up to seven sentences when both edges have room. Smaller windows stay tighter; larger windows fill pronouns and cost more prompt tokens.
What does sentence-window retrieval cost?
Structurally: one embedding per sentence at ingest, an expand step before generation, and prompt tokens that scale with window size times top-k. Some designs also keep a neighbour ID store; the common LlamaIndex path stores the window string in metadata instead. Measure latency and spend on your own corpus.
When should you skip sentence-window retrieval?
Skip it when sentences are already self-contained, when relevant facts are dispersed across distant chapters (prefer parent-document or auto-merging), or when documents have real heading hierarchies better served by fixed parents. Prefer a measured A/B on the same queries over adopting the pattern by name.
How is sentence-window different from parent-document retrieval?
Sentence-window retrieval embeds sentences and expands each hit by ±N neighbours at query time, so the window size is a runtime parameter. Parent-document retrieval fixes parent and child boundaries at index time and returns the pre-defined parent. Both are small-to-big; they differ in when the big unit is assembled.