AI Engineer Roadmap All lessons Interactive Quiz GitHub

Parent Child Chunking

Intermediate · 8 min read · Updated · By Saurabh Pathak

In short: Parent-child chunking searches over small "child" chunks for precise matches, then gives the model the larger "parent" chunk each child came from, for full context.

Play the interactive lesson: Parent-child chunkingRead on GitHub
On this page
  1. Explain it like I'm new
  2. Watch it
  3. How it works
  4. Code example
  5. When to use it / when not to
  6. Common mistakes
  7. Recap
  8. Interview questions
  9. Go deeper

Level: Intermediate · Reading time: 8 min · Phase: 1 — RAG Foundations

Before you start: Chunking, Recursive Chunking and Metadata. You should know what a chunk is and that a chunk can carry extra facts such as an ID.

Explain it like I'm new

Think of a library index card. The card is tiny: "Egg boiling times — page 112". It is easy to find the right card. But you do not read the card to cook; you open the book to page 112 and read the whole section. The card is the child. The page is the parent.

Chunking has a built-in conflict (see Chunking): small chunks are best for finding, large chunks are best for understanding. Parent-child chunking stops choosing. It stores both sizes and links them.

At query time you search children, collect their parent IDs, remove duplicates, and send the parents to the LLM. In LangChain this is called the ParentDocumentRetriever. LlamaIndex has a related idea called auto-merging retrieval.

Words to know:

Watch it

Parent-child chunking: Search small pieces, answer with the big piece around them.
Parent-child chunking: Search small pieces, answer with the big piece around them.

It plays on a loop. Read the caption in the box, one step at a time. Want to go at your own pace? Step through it with Previous and Next.

How it works

  1. Split each document into parent chunks (by section or with a large recursive split).
  2. Split each parent into child chunks.
  3. Give each child a parent_id. Embed children and store them in the vector database.
  4. Store parents in a simple key-value store (parent_id → text).
  5. At query time: search children (top-k), map them to parent IDs, deduplicate, fetch parents.
  6. Send parents (not children) to the LLM.
flowchart LR
  P1[Parent p0: full section] --> C1[child p0-c0]
  P1 --> C2[child p0-c1]
  P2[Parent p1] --> C3[child p1-c0]
  Q[Question] -->|vector search| C2
  C2 -->|parent_id = p0| P1
  P1 -->|send to LLM| L[LLM]

Worked example: 3 children are retrieved with parent IDs p4, p4, p9. After deduplication you send 2 parents, p4 and p9. If parents are 800 tokens each, the prompt carries about 1,600 tokens of context.

Worked example

The repo demo splits the sample text into three parents (p0, p1, p2), each cut into two children. Real output:

p0-c0    -> parent p0: 'Docker images are built in layers. '
p0-c1    -> parent p0: 'Each instruction in a Dockerfile adds a layer.\n'
p1-c0    -> parent p1: 'Multi-stage builds use several FROM lines. '
p1-c1    -> parent p1: 'Only the final stage ends up in the shipped image, so the image stays small.\n'
p2-c0    -> parent p2: 'Refresh tokens should live in an httpOnly cookie. '
p2-c1    -> parent p2: 'Rotate refresh tokens on every use.'

Now ask "How do I keep the image small?". Trace it (we reason through the search by hand here, it is not demo output):

  1. Search compares the question with the six children only.
  2. Words like "image" and "small" point to p1-c1, "...the image stays small", as the best match.
  3. Its parent_id is p1, so we fetch parent p1, the whole paragraph, including the sentence about multi-stage builds.
  4. If a second child from p1 also matched, we would still send p1 once, thanks to deduplication.

The small child made the match precise. The parent gives the LLM the context around it.

Code example

From code/rag_chunkers.py, building on recursive_chunks:

def parent_child_chunks(text, parent_size=400, child_size=100):
    parents, children = {}, []
    for p_idx, parent in enumerate(recursive_chunks(text, parent_size)):
        pid = f"p{p_idx}"
        parents[pid] = parent
        for c_idx, child in enumerate(recursive_chunks(parent, child_size)):
            children.append({"id": f"{pid}-c{c_idx}", "parent_id": pid, "text": child})
    return parents, children

def retrieve_parents(question, children, parents, search, k=3):
    hits = search(question, [c["text"] for c in children], k)   # indexes of best children
    seen, out = set(), []
    for i in hits:
        pid = children[i]["parent_id"]
        if pid not in seen:
            seen.add(pid); out.append(parents[pid])
    return out

search can be any retriever, for example BM25 from code/rag_bm25.py.

In real projects (LangChain, with any vector store and embeddings you already use):

from langchain_classic.retrievers import ParentDocumentRetriever   # LangChain 1.x; older: langchain.retrievers
from langchain_core.stores import InMemoryStore
from langchain_text_splitters import RecursiveCharacterTextSplitter

retriever = ParentDocumentRetriever(
    vectorstore=vectorstore,                      # holds child embeddings
    docstore=InMemoryStore(),                     # holds parents
    child_splitter=RecursiveCharacterTextSplitter(chunk_size=200),
    parent_splitter=RecursiveCharacterTextSplitter(chunk_size=1000),
)
retriever.add_documents(docs)
results = retriever.invoke("Where should refresh tokens be stored?")

Import paths move between LangChain versions; check the docs for your version.

When to use it / when not to

Common mistakes

Recap

If you remember only three things:

Interview questions

1. What problem does parent-child chunking solve? <details><summary>Answer</summary> The trade-off between precise retrieval (small chunks) and enough context for generation (large chunks). It searches small children and returns their large parents. </details>

2. Which chunks are embedded? <details><summary>Answer</summary> Usually only the children. Parents are stored by ID in a document store and fetched after the search. </details>

3. What is a risk of this approach? <details><summary>Answer</summary> Prompt size can grow quickly, and duplicate or irrelevant parent text can add noise. You need deduplication and a cap on the number or size of parents. </details>

Go deeper

Prev: Semantic Chunking · Next: Sliding Window Chunking · Back to phase: Phase 1