Sub topic 10
Small chunks for search, larger parents for context
This chapter assumes you know what an embedding is, how vector similarity search ranks results, and the basic retrieve-then-generate loop. It also assumes you have read the earlier chapters on chunk size, overlap, and recursive splitting.
The tension in chunk size
Chunk size pulls in two directions. Small chunks (50 to 150 tokens) give precise embeddings. Each one covers a single idea, so a query about that idea matches it strongly. But a small chunk often lacks the surrounding text the model needs to answer well.
Large chunks (500 to 1,000 tokens) carry plenty of context. Their embeddings blend several ideas, so they match any one query less sharply, and they spend more of the context budget on text that may be irrelevant.
Parent-child retrieval, also called small-to-big retrieval, avoids the trade-off by using two sizes. You search over small child chunks and then return the larger parent chunk each child came from. A parent can be a section, a page, or a fixed block of text.
How it works
- Split each document into parents of roughly 800 tokens.
- Split each parent into children of roughly 150 tokens.
- Embed and index only the children. Each child stores the ID of its parent.
- Keep the parents in a separate store, such as a dictionary or a database table, keyed by ID.
- At query time, search the children, look up their parent IDs, remove duplicates, and send the parents to the model.
A minimal implementation
This example reuses the chunk_words function from the fixed-size chapter to build both levels. The embedding step is left out so the code runs on its own. The index list is what you would embed and store in a vector database.
def chunk_words(text, size=200, overlap=40):
if overlap >= size:
raise ValueError("overlap must be smaller than size")
words = text.split()
step = size - overlap
chunks = []
for start in range(0, len(words), step):
chunks.append(" ".join(words[start:start + size]))
if start + size >= len(words):
break
return chunks
def build_parent_child(text, parent_size=200, child_size=50):
parents = {}
index = [] # one entry per child: what you would embed
for p_num, parent in enumerate(chunk_words(text, parent_size, 0)):
parent_id = f"p{p_num}"
parents[parent_id] = parent
for c_num, child in enumerate(chunk_words(parent, child_size, 10)):
index.append({"id": f"{parent_id}-c{c_num}",
"parent_id": parent_id,
"text": child})
return parents, index
def parents_for_hits(hit_ids, index, parents):
by_id = {entry["id"]: entry for entry in index}
seen, result = set(), []
for hit in hit_ids: # hits arrive in rank order
parent_id = by_id[hit]["parent_id"]
if parent_id not in seen:
seen.add(parent_id)
result.append(parents[parent_id])
return result
text = " ".join(f"word{i}" for i in range(450))
parents, index = build_parent_child(text)
print(len(parents), "parents,", len(index), "children")
top_hits = [index[0]["id"], index[1]["id"], index[7]["id"]]
print(len(parents_for_hits(top_hits, index, parents)), "parents returned")Three child hits come back as two parents, because the first two children share a parent. Deduplication keeps the output in rank order and avoids sending the same text twice.
Common errors
- Duplicate parents. Several top hits often share one parent. Without deduplication you waste context budget and may bias the model toward repeated text.
- Parents that are too large. If you retrieve five parents of 2,000 tokens each, you may exceed the context budget. Count tokens for the parents, not the children.
- Losing the link. If the parent ID is missing from the child's metadata, you cannot expand the hit. Store it at ingestion time.
- Stale parents. When a document changes, re-chunk both levels and replace the children and the parent together.
When it is worth the cost
Parent-child retrieval adds a second store and more ingestion logic. It helps most when answers need surrounding explanation, such as policies, manuals, and legal text. It helps less when each fact is self-contained, such as FAQ entries. Test it with recall@k and answer quality before adopting it.
Exercise
Your index holds 150-token children and 800-token parents. A query returns five child hits, and four of them come from the same parent. How many parents should you send to the model, and why?
Show answer
Two parents at most: the shared parent once, plus the parent of the fifth hit. Deduplicating by parent ID avoids sending the same 800 tokens four times, which saves context budget and avoids repeated text.