DEV Community

Cover image for Your RAG chunks forgot where they came from: add context before you embed them (Contextual Retrieval)

Your RAG chunks forgot where they came from: add context before you embed them (Contextual Retrieval)

M.T.Ramkrushna on November 23, 2026

Monday AI Insight. One practical RAG lesson: a chunk that makes sense inside its document can be useless on its own. Before you embed it, tell it w...
Collapse
 
iictp profile image
IICTP •

The re-index note is the part I'd want to hear more about — since the context is derived from the full document, editing one section can quietly stale the context of every other chunk, so the re-index unit becomes the document, not the chunk. How are you handling that in practice — full-document re-index on any edit, or a check for which chunks' context actually shifted?

Collapse
 
m_t_ramkrushna profile image
M.T.Ramkrushna •

Right now I'd do a full-document re-index on any edit, since that's the safe default. The cheaper option is to diff the regenerated context per chunk and re-embed only the ones whose context text changed, so you skip most vector writes.

Collapse
 
iictp profile image
IICTP •

Thank you — the diff-the-regenerated-context idea is the part I was hoping to hear more about. Re-embedding only the chunks whose context text changed keeps the safe default's spirit without paying the full re-index on every small edit. The part I'd watch is whether the context diff itself stays cheap when the document is long — does regenerating every chunk's context on each edit end up costing more than the vector writes it saves?

Thread Thread
 
m_t_ramkrushna profile image
M.T.Ramkrushna •

Fair concern. Regenerating context is the expensive part, so I'd only regenerate for the edited section plus neighbours that reference it, and cache the document as a prompt prefix so each extra call is cheap. For long documents, a hash per section lets you skip regeneration for untouched parts, and the heading-path arm is the free fallback if cost still bites.

Thread Thread
 
iictp profile image
IICTP •

That's a practical split — regenerating only the edited section plus the neighbours that reference it keeps the expensive part small, and caching the document as a prompt prefix is a neat way to make the extra calls cheap. The per-section hash is a nice touch too, since untouched parts can just be skipped. How do you decide which neighbours 'reference' an edited section in practice — is it heading structure, links, or something you detect in the text?

Thread Thread
 
m_t_ramkrushna profile image
M.T.Ramkrushna •

In practice I'd start with explicit references: headings, links, and cross-references like 'see section X'. Then add a cheap text check for shared entities or terms between sections. If a neighbour's regenerated context actually changes in the diff, re-embed it; that catches implicit dependencies without extra detection logic.

Collapse
 
recallrundev profile image
RecallRun •

Worth adding a free arm to the before/after: prepend the heading path, no LLM call. On the FastAPI docs with BM25, heading-aware chunks scored 0.712 hit@1; prepending Page title > Section > Subsection to the indexed text took the same chunks to 0.785 (hit@5 0.933 to 0.966). For well-structured docs that recovers part of what contextual retrieval does, at zero indexing cost and with nothing to go stale when one section is edited.

The LLM context should earn its cost where the missing fact isn't in any heading, like your ACME example, where the company and quarter only appear on the filing's cover page. So I'd run three arms on the same eval set: plain chunks, heading path, LLM context. If the last two land close, you skip the per-chunk calls and the re-index problem.

Collapse
 
m_t_ramkrushna profile image
M.T.Ramkrushna •

Good call, a heading-path arm is cheap and stays in sync when one section changes. I'll run the three arms (plain, heading path, LLM context) on one eval set and see if the last two land close enough to skip the per-chunk calls.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

How do you handle re-indexing when one section changes, since the context is derived from the whole document?

Collapse
 
m_t_ramkrushna profile image
M.T.Ramkrushna •

Short answer: the re-index unit becomes the document. I'd hash each section and, on an edit, regenerate context only for chunks in that document, reusing the cached document prompt so cost stays low. Then re-embed only chunks whose generated context text actually changed.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Hashing each section and re-embedding only chunks whose generated context changed is a tight loop. Reusing the cached document prompt is what keeps the cost low.