Retrieval & RAGPatternValidated

Contextual retrieval

Also called: contextual chunk headers

Contextual retrieval prepends a short, generated description of where a chunk came from before embedding it. It fixes the fact that chunking strips away the context a chunk needs in order to be findable.

James Phoenix
Understanding Data Updated July 26, 2026

Chunking has a quiet flaw. Cutting a document into pieces also cuts each piece off from the thing that made it meaningful. A chunk reading:

Note
Revenue fell 3% for the quarter.

is nearly unretrievable. Which company? Which quarter? A query like "how did Acme do in Q2 2026" will not match it, because none of those words are in the chunk. The information was in the document; chunking threw it away.

Restoring what chunking removed

Before embedding, generate a sentence or two situating the chunk in its parent document, and prepend it:

TypeScript
const context = await summarise(`
  Document: ${doc.title}
  Write one sentence situating this excerpt in the document.
  Excerpt: ${chunk}
`)

const embedded = `${context}\n\n${chunk}`   // embed this, not the bare chunk

The stored chunk becomes:

Note
This excerpt is from Acme Corp's Q2 2026 earnings report, discussing the hardware division. Revenue fell 3% for the quarter.

Now the query matches, because the words the query uses are finally present.

The cost, and why it is usually worth paying

You pay one extra model call per chunk at index time. That is a one-off, offline cost, and it buys better retrieval on every query forever after. It is one of the rare cases where the expensive thing happens where nobody is waiting for it.

  • Keep the generated context short. Two sentences. You are adding retrieval handles, not summarising.
  • Generate it from the whole document, or at least surrounding sections, or the model has no more context than the chunk did.
  • Prepend it to the text you embed and to the text you index for keyword matching, so hybrid search benefits too.
Tip
Cheaper approximation, no model calls: mechanically prepend the document title and section headings to every chunk. It captures much of the benefit for none of the cost, and it is a sensible thing to do before reaching for generation.

Related terms

Engineering context for real systems?

Getting the right information into the window at the right time is most of the job. If you want that thinking applied to your product, that is what I do.

See how I can help