Contextual chunks and parent–child retrieval
Many retrieval failures start at ingestion. A chunk that says 'it weighs 1.9 kg' does not say what 'it' is. Give each chunk its context and retrieval improves without touching the search.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
Step 1 shows a chunk with and without its header, step 2 ranks plain chunks and contextual chunks side by side, step 3 shows parent–child retrieval: search small chunks, send the whole parent section. All RAG simulations share one knowledge base of 18 chunks from 11 documents of a fictional outdoor-gear shop, and three questions: one phrased differently from the documents, one that needs two documents, and one about the whole corpus.
Things to try
- Compare the two rankings for the fabric question.
- Notice which chunks gain from naming their document.
- Consider the cost: one pass over the corpus at indexing time, nothing at query time.
How it works in real systems
Anthropic's contextual retrieval prepends an LLM-written context to every chunk before embedding and BM25 indexing. LangChain's ParentDocumentRetriever and LlamaIndex's auto-merging retriever implement parent–child.
Code lab
The same idea in real code, step by step. Copy each block into a notebook or a file and run it in order.
Step 1 · Index the knowledge base
Install the libraries and index a small knowledge base with a sentence-embedding model. Normalised embeddings make the dot product equal to cosine similarity.
pip install sentence-transformers rank-bm25 numpy openai
import numpy as np
from sentence_transformers import SentenceTransformer
chunks = [
"Most items can be returned within 30 days of delivery for a full refund.",
"Damaged or defective items qualify for a full refund or a free replacement; send a photo of the damage to support first.",
"Refunds are issued within five business days after the returned item reaches our warehouse.",
"Parcels damaged in transit must be reported to the carrier within 48 hours of delivery.",
"The Ridge 2 tent weighs 1.9 kg and uses Aurora ripstop fabric for its fly sheet.",
"Aurora ripstop fabric is woven by Lumen Textiles at its mill in Porto, Portugal.",
"Customer complaints in Q3 were mostly about late deliveries during the September peak.",
]
emb = SentenceTransformer("all-MiniLM-L6-v2") # 384 dimensions, small and fast
E = emb.encode(chunks, normalize_embeddings=True)
def dense_search(q, k=3):
s = E @ emb.encode([q], normalize_embeddings=True)[0]
top = np.argsort(-s)[:k]
return [(int(i), float(s[i])) for i in top]
Step 2 · Write a context line for every chunk
A small helper to call a chat model (any OpenAI-compatible endpoint works, including a local server):
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY
def ask(prompt, model="gpt-4o-mini"):
r = client.chat.completions.create(model=model, messages=[{"role": "user", "content": prompt}], temperature=0)
return r.choices[0].message.content
docs = {"Returns policy": chunks[0:3], "Shipping": chunks[3:4], "Products": chunks[4:5], "Suppliers": chunks[5:6], "Q3 complaints": chunks[6:7]}
ctx_chunks = []
for title, cs in docs.items():
whole = " ".join(cs)
for c in cs:
line = ask(f"<document title='{title}'>{whole}</document>\nHere is a chunk from it:\n<chunk>{c}</chunk>\n"
"Write one short sentence situating this chunk within the document, to improve search retrieval. Answer with the sentence only.")
ctx_chunks.append(f"{title}. {line} {c}")
E_ctx = emb.encode(ctx_chunks, normalize_embeddings=True) # index these; show the original chunk to the model
Step 3 · Compare rankings, then retrieve parents
q = "Who makes the fabric used in the Ridge 2 tent?"
v = emb.encode([q], normalize_embeddings=True)[0]
print("plain ", np.argsort(-(E @ v))[:3]); print("contextual", np.argsort(-(E_ctx @ v))[:3])
Parent–child retrieval is the same idea from the other side: search small chunks, then send the whole parent section. In LangChain: ParentDocumentRetriever(vectorstore=..., docstore=..., child_splitter=..., parent_splitter=...).