Parent-Child Hierarchical Context Retrieval

Embed small chunks for precise matching, and send large parents to the model for enough context.

Part 1 of 5

Basics

The whole idea first, then the terms the rest of this page uses.

1.1Whole idea

Small chunks match queries precisely but lack context. Large chunks hold context but match vaguely. Parent-child retrieval uses both: search small children, then give the model their larger parents.

Why it matters

Embed small chunks for precise matching, and send large parents to the model for enough context.

1.2Key concepts

One idea per card, in plain words.

01Child chunk

A small passage of about 100–300 tokens, used only for embedding and matching.

02Parent chunk

The larger passage of about 1,000–2,000 tokens that the model reads.

03Deduplication

Several children can match within one parent, so each parent appears once, at its best score.

Part 2 of 5

Question

What exactly you must answer, and what to settle before you start.

2.1Question

Interviewer

Your support bot retrieves the right sentence but misses the conditions around it, and larger chunks make retrieval fuzzy. Fix both.

Part 3 of 5

Detailed answer

How it works, and how to build or explain it.

3.1Step by step

The mechanism, in the order you would explain it.

  1. See why one size fails

    An embedding averages its text. A 2,000-token chunk blurs many topics into one vector.

  2. Split twice

    Cut documents into parents along headings, then cut each parent into children.

  3. Search the children

    Embed and index children only, each pointing to its parent.

  4. Return the parents

    Map hits to parents, keep each once at its best score, and rank them.

  5. Respect the budget

    Parents cost tokens. Five parents of 2,000 tokens take 10,000 from your context window.

3.2Code

Working code for the core of it.

Search small, read large

Small chunks match queries precisely, and large ones give the model enough context. This keeps both.

parent-child.tsTypeScriptCopy
1export interface Chunk {2  id: string;3  parentId: string;4  text: string;5}6 7/** Splits a document into large parents, and each parent into small children to embed. */8export function splitDocument(docId: string, words: string[], parentSize: number, childSize: number) {9  const parents = new Map<string, string>();10  const children: Chunk[] = [];11  for (let p = 0; p * parentSize < words.length; p++) {12    const parentWords = words.slice(p * parentSize, (p + 1) * parentSize);13    const parentId = `${docId}#${p}`;14    parents.set(parentId, parentWords.join(" "));15    for (let c = 0; c * childSize < parentWords.length; c++) {16      children.push({ id: `${parentId}.${c}`, parentId, text: parentWords.slice(c * childSize, (c + 1) * childSize).join(" ") });17    }18  }19  return { parents, children };20}21 22/** Search hits children; the model reads parents. Each parent appears once, at its best score. */23export function toParents(hits: { child: Chunk; score: number }[], parents: Map<string, string>, limit: number) {24  const best = new Map<string, number>();25  for (const { child, score } of hits) best.set(child.parentId, Math.max(best.get(child.parentId) ?? -Infinity, score));26  return [...best]27    .sort((a, b) => b[1] - a[1])28    .slice(0, limit)29    .map(([id, score]) => ({ id, score, text: parents.get(id) ?? "" }));30}
30 linesUTF-8TypeScript
children
Only these get embedded and searched.
best.set(child.parentId, Math.max(...))
Several children of one parent collapse into one result, at its best score.

Tested by npm run test:modules.

Part 4 of 5

Q&A

What interviewers ask next. Answer out loud, then open.

4.1Interview questions

How do you pick sizes?
Test on your eval set. Start with 200-token children and 1,500-token parents, and split on document structure when you can.
Sentence window or parent-child?
A sentence window returns neighbouring sentences around a hit. Parent-child returns a fixed section. Windows suit flowing text; parents suit documents with clear sections.
How does this interact with reranking?
Rerank parents, not children. The reranker should judge what the model will read.
Part 5 of 5

Extra depth

Edge cases, counterpoints and what to read next.

5.1Edge cases and counterpoints

Includes the strongest argument against this approach.

Show detailsHide details
Tables and code
Keep them whole. A table split across children loses its meaning.
Counterpoint
Short, uniform documents such as FAQs gain little. One chunk per answer already works.