Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 34/ 160
02. RAG

Parent-Child Chunking

Retrieve small child chunks for precision, then expand to parent sections for generation.

IntermediateSmall-to-Big · Parent Expand3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

Search with tiny snippets so matches are exact, but show the model the bigger section each snippet came from.

Think of it like…

Finding a sentence in a book using a precise index, then reading the whole paragraph or section around it before answering. The index points to the exact spot; the reader gets the context.

Where you’ve seen it

A compliance assistant indexed 400-token chunks and found answers were often incomplete: the matched chunk contained the rule but not the exceptions in the next paragraph. Switching to 150-token children linked to their ~800-token parent sections, retrieval precision stayed the same, while answer completeness (judged on 150 questions) rose from 68 to 84 percent. Prompts grew by about 40 percent in tokens, which they offset by returning four parents instead of eight chunks.

Words you’ll hear

Child chunk
Small unit that is embedded and searched.
Parent chunk
Larger surrounding unit (section or window) returned to the LLM.
Deduplication
Several children from one parent should yield that parent once.
Auto-merging
Return the parent only when enough of its children match; otherwise return the children.

Visual diagram

Loading interactive diagram

Theory & deep dive

How to build it

At ingestion, split each document into parents using structure (sections or headings; or fixed windows of 600 to 1,500 tokens), then split each parent into children (100 to 250 tokens, optionally with small overlap). Store parents in a key-value or document store by id, and embed only the children, each carrying metadata: parent_id, document id, section path and position. Metadata filters and permissions apply to children and are inherited from the parent.

At query time, retrieve the top k children (k = 10 to 30) with dense, sparse or hybrid search, optionally rerank them, map each child to its parent_id, deduplicate while preserving the best child's rank, and fetch the top parents (3 to 6) within a token budget. Variations: sentence-window retrieval returns the matched sentence plus N neighbours; auto-merging promotes to the parent only when several siblings match; three-level hierarchies (sentence, paragraph, section) allow adaptive expansion.

Mapping children to parents

Top children by score: c12 (parent P3), c13 (P3), c40 (P9), c55 (P3), c61 (P12). After deduplication: P3 (best child c12), P9, P12. Fetch the three parents, about 2,400 tokens, instead of five 150-token fragments with no surroundings.

Key takeaway: Embed children, store parents, dedupe by parent id, budget tokens.

Trade-offs and tuning

Pros: sharper matching than large chunks, more context than small chunks, no extra model calls, and easy citations (the child span shows exactly where the match was). Cons: larger prompts (cost and latency), possible noise if a parent is mostly irrelevant, more storage (parents and children), and added complexity in ingestion and updates (children and parents must stay consistent when a document changes).

Tune child size by retrieval precision and parent size by answer completeness: if answers miss exceptions or follow-on steps, enlarge parents or return adjacent parents; if prompts are too long or noisy, shrink parents or use auto-merging. Evaluate retrieval (recall@k at child level and parent level) and end-to-end answer quality and citation accuracy on labelled questions. Combine with reranking over parents when you want the LLM to see only the most relevant sections, and with contextual retrieval to make each child self-explanatory.

Auto-merging rule

If at least 3 of a parent's 6 children are in the top 20, return the parent; otherwise return only the matched children plus one neighbour each. This keeps prompts small for narrow questions and rich for broad ones.

Key takeaway: Tune child size for precision, parent size for completeness; watch prompt tokens.

How it works, step by step

Step 1 of 4
Design

1. Choose parent and child boundaries

Use sections as parents and 100–250 token children, with metadata linking child to parent.

parent ≈ 800 tokens · child ≈ 150 tokens
Key numbers at this step
Link
parent_id on each child

When to use it & trade-offs

What it is

Parent-child chunking (also called small-to-big retrieval) separates what you search from what you send to the LLM. Documents are split into small child chunks (about 100 to 250 tokens) that are embedded and matched precisely, and each child points to a larger parent (a section or a 600 to 1,500-token window). At query time you retrieve the best children, then replace them with their parents (deduplicated) before building the prompt. You get precise matching and rich context at the same time.

Why it matters: Chunk size forces a trade-off: small chunks match queries sharply but leave the LLM without surrounding context; large chunks give context but blur the embedding across several topics. Parent-child retrieval escapes the trade-off, and it's a simple, deterministic change that often raises answer quality without a new model. It's a standard pattern in LangChain (ParentDocumentRetriever) and LlamaIndex (auto-merging retriever).

When to reach for it

  • Policies, manuals and contracts where answers span paragraphs
  • Systems where precise citations matter
  • RAG with answers that need exceptions and follow-on steps

When to avoid or be careful

  • Sending every matching child's parent without deduplication
  • Parents so large they swamp the prompt
  • Updating parents without re-indexing children

Chunk-size strategies

Single medium chunks
Best for: Simple pipelines and homogeneous documents

Strengths: Easy; one index.

Watch out: Compromise between precision and context.

Parent-child (small-to-big)
Best for: Long structured documents needing precise matches and full context

Strengths: Precision and context together; clear citations.

Watch out: More storage, bigger prompts, consistency to maintain.

Sentence-window
Best for: Fine-grained matching with a little surrounding text

Strengths: Simple; adjustable window.

Watch out: Fixed window ignores document structure.

Interview signals & numbers to know

When this topic comes up

  • “The retrieved snippet is right but incomplete”
  • “Small chunks match better, large chunks answer better”
  • “What is small-to-big retrieval?”
  • Auto-merging retrieval
  • Sentence-window retrieval

Benchmarks & rules of thumb

100 – 250 -
child chunk tokens
600 – 1,500 -
parent chunk tokens
68% → 84% -
answer completeness gain in the example

Common mistakes & follow-ups

Common interview mistakes

  • Returning five children from the same section as five prompt entries instead of one parent.
  • Forgetting to carry ACL metadata from parents to children.
  • Changing a document and re-embedding the parent text while leaving old children in the index.

Likely interviewer follow-ups

  • How does parent-child retrieval resolve the chunk-size trade-off?
  • How do you deduplicate and budget parents?
  • When would you use auto-merging?
Interview tip: Use the phrase "search small, answer big" and explain the dedupe-and-budget step. Interviewers listen for the practical details.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Parent-Child Chunking with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Contextual Retrieval
Next topic
Document Parsing