Parent-Child (Small-to-Big) Chunking

Retrieve with small precise chunks and answer with their larger parent sections.

Last Updated: 27, Nov 2026

Search Tutorials

--Ads--

Module 4 · Lesson 7 of 10 · Intermediate

Parent-Child (Small-to-Big) Chunking

Retrieve with small precise chunks and answer with their larger parent sections.

The chunk size trade-off says small chunks retrieve precisely but lack context, while large chunks have context but retrieve poorly. Parent-child chunking (also called small-to-big retrieval) avoids choosing: it searches with small chunks and answers with large ones.

The idea

  1. Split each document into large parent chunks, such as whole sections of 1,500 to 3,000 tokens.
  2. Split each parent into small child chunks of 100 to 300 tokens. Each child stores the ID of its parent.
  3. Embed and index only the children.
  4. At query time, search the children, then look up their parents and send the parents to the LLM.
The query matches child chunk 3 inside a parent section; the whole parent section is sent to the LLM.
Figure 1. Search over small child chunks, then send the parent section to the LLM.

Implementation

parent_child.py
import numpy as np
from chunkers import recursive_split          # from the fixed-size chunking lesson
from rag_helpers import ask, embed

class ParentChildIndex:
    def __init__(self, parent_chars: int = 4000, child_chars: int = 600):
        self.parent_chars, self.child_chars = parent_chars, child_chars
        self.parents: dict[str, dict] = {}        # parent_id -> {"text", "source"}
        self.children: list[dict] = []            # {"text", "parent_id"}
        self.vectors = None

    def add_document(self, doc_id: str, text: str) -> None:
        for p, parent_text in enumerate(recursive_split(text, self.parent_chars)):
            parent_id = f"{doc_id}#p{p}"
            self.parents[parent_id] = {"text": parent_text, "source": doc_id}
            for child_text in recursive_split(parent_text, self.child_chars):
                self.children.append({"text": child_text, "parent_id": parent_id})

    def build(self) -> None:
        self.vectors = embed([c["text"] for c in self.children])

    def search(self, query: str, k_children: int = 8, max_parents: int = 3) -> list[dict]:
        scores = self.vectors @ embed(query, query=True)[0]
        results, seen = [], set()
        for i in np.argsort(-scores)[:k_children]:
            parent_id = self.children[i]["parent_id"]
            if parent_id in seen:                 # several children may share one parent
                continue
            seen.add(parent_id)
            results.append({"parent_id": parent_id, "score": float(scores[i]), **self.parents[parent_id]})
            if len(results) == max_parents:
                break
        return results
from pathlib import Path

index = ParentChildIndex()
for path in Path("docs").glob("*.txt"):
    index.add_document(path.name, path.read_text(encoding="utf-8"))
index.build()

for hit in index.search("Can I carry unused leave into next year?"):
    print(hit["parent_id"], round(hit["score"], 3), len(hit["text"]), "chars")

Why deduplicate parents

When several children of the same parent match, a naive implementation sends the same parent several times. Collecting unique parent IDs in rank order, as above, keeps the context window free for other relevant sections. A strong signal that one parent is highly relevant is that many of its children appear in the top results.

Variant: sentence-window retrieval

Instead of fixed parents, you can index single sentences and, at query time, return each matched sentence together with its N neighbours on both sides. This works well for dense text such as legal or scientific documents, where the exact matching sentence matters most.

def sentence_window(sentences: list[str], hit_index: int, window: int = 3) -> str:
    start, end = max(0, hit_index - window), min(len(sentences), hit_index + window + 1)
    return " ".join(sentences[start:end])

Variant: return the whole document

For collections of short documents (FAQ answers, product pages, tickets), the "parent" can simply be the whole document. Search over chunks, then send the full documents of the top hits.

Storing parents in production

Children go into the vector database. Parents do not need to be embedded, so store them in any key-value store or document database (PostgreSQL, Redis, MongoDB, or even files) keyed by parent_id. Frameworks call this a docstore: LangChain's ParentDocumentRetriever and LlamaIndex's AutoMergingRetriever implement the pattern.

Watch the token budget

Parents are big. Three parents of 3,000 tokens is already 9,000 tokens per question. Limit the number of parents, or fall back to sending only the matching children when the parents would exceed your budget.

Key takeaways

  • Index small child chunks for precise matching; send their larger parents for context.
  • Deduplicate parents and keep them in rank order of their best child.
  • Sentence-window and whole-document retrieval are variants of the same idea.
  • Store parents in a simple key-value store; only children need embeddings.