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.
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.
A small passage of about 100–300 tokens, used only for embedding and matching.
The larger passage of about 1,000–2,000 tokens that the model reads.
Several children can match within one parent, so each parent appears once, at its best score.
Question
What exactly you must answer, and what to settle before you start.
2.1Question
Your support bot retrieves the right sentence but misses the conditions around it, and larger chunks make retrieval fuzzy. Fix both.
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.
- See why one size fails
An embedding averages its text. A 2,000-token chunk blurs many topics into one vector.
- Split twice
Cut documents into parents along headings, then cut each parent into children.
- Search the children
Embed and index children only, each pointing to its parent.
- Return the parents
Map hits to parents, keep each once at its best score, and rank them.
- 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.
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}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.
Q&A
What interviewers ask next. Answer out loud, then open.
4.1Interview questions
How do you pick sizes?
Sentence window or parent-child?
How does this interact with reranking?
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.