New Tool Radar

AI Concepts

How to Add Document Context to RAG Chunks Before Embedding

Give each RAG chunk a short note about where it sits in the document, so the retriever can tell two similar chunks apart.

Schematic illustration with three step boxes: write the context note, prepend before embedding, and measure failed retrievals.

A chunk taken out of its document can be ambiguous: a paragraph about a 30-day window means little without knowing whether it is the refund policy or the warranty policy. Contextual retrieval writes a short note of 50 to 100 tokens that situates each chunk in the whole document, then embeds the note together with the chunk. Anthropic reported that adding context cut failed retrievals by 49 percent, and by 67 percent when combined with reranking.

Before you start

A language model you can call once per chunk to write the context note, and a retrieval test set with questions that distinguish similar chunks.

Write the context note

For each chunk, ask the model for one or two sentences that name the document, the section, and what the chunk is about. Keep it to 50 to 100 tokens: long enough to disambiguate, short enough not to swamp the chunk itself.

Include the facts that make this chunk different from its neighbours, such as the product name, the policy name, or the date range. Generic notes add tokens without adding signal.

Prepend the note before embedding

Embed the note plus the chunk together, and store the chunk text unchanged for display. The model that answers should see the original chunk, not the note, so the answer quotes the real text.

Keep the note in the index as well as in the embedding input, so you can inspect what the retriever actually saw when a result looks wrong.

Measure failed retrievals, not vibes

Run the same test questions before and after adding context and count the failures: questions where the right chunk is not in the top 5. A real improvement shows as fewer failures, not as higher scores on easy questions.

If failures do not fall, the notes are probably too generic. Rewrite them to name the distinguishing facts and test again.

Practice check: fictional Maple Corner policies, 40 chunks

The table below is fictional practice material, worked out by hand from the stated failure counts. It is the expected result for the practice set, not a run of any retrieval system.

Test set Failures before Failures after Change
Refund vs warranty questions 9 of 20 4 of 20 5 fewer failures
Shipping questions 3 of 20 2 of 20 1 fewer failure
All 40 questions 12 of 40 6 of 40 Half as many failures

Context notes halve the failures on the full set, and most of the gain sits in the refund-versus-warranty questions, which is exactly where the chunks were ambiguous.

Expected result. UNTESTED: the failure counts in the practice table are worked out by hand, not measured by running a retriever. Run your own test questions before and after adding context and count the failures in the top 5.

Limits

  • Writing a note per chunk costs one model call per chunk at index time, which adds up on large corpora.
  • Context notes help retrieval; they do not fix a chunk that contains the wrong facts.

Sources

See all