4.4.13 · D4Alignment, Prompting & RAG

Exercises — Chunking strategies for retrieval

2,548 words12 min readBack to topic

The only formulas you need, restated so this page stands alone:

Relevant background: Retrieval-Augmented Generation (RAG), Text Embeddings and Cosine Similarity, Vector Databases and ANN Search, Context Window Limits of LLMs, Tokenization, Metadata Filtering in Retrieval.


Level 1 — Recognition

Recall Solution

Answer: the chunk step. A retriever can only return the units you indexed, so if a unit is badly sized (too big = blurry vector, too small = no context) no downstream stage can recover the lost precision or context. Chunking sits at the very front, so its quality is an upper bound on everything after it.

Recall Solution

(a) → chunk too big — one vector must average many ideas, giving semantic blurring. (b) → chunk too small — the chunk lost its surrounding context, so the embedding is ambiguous.

Recall Solution

Fixed-size → Recursive / structure-aware → Semantic → Document-structure → Parent-document (small-to-big).


Level 2 — Application

Recall Solution

With no overlap the stride equals , so use . It divides evenly, so no rounding was needed — but we still write the ceiling out of habit, because most real numbers won't divide cleanly.

Recall Solution

Overlap changes the stride to . Use the overlap formula: Why more chunks than Q1? Each chunk now advances only 360 new tokens instead of 400, so you need more of them to cover the same document. Overlap costs storage — that's the price of protecting facts near boundaries.

Recall Solution

Use : So is the minimum; a safe practical choice is . Why and not ? Two neighbouring chunks share tokens; a window of length up to that straddles the boundary is fully inside at least one chunk. Setting gives .

Recall Solution

Use . Small, but over thousands of documents that's many lost facts — which is exactly why overlap exists.


Level 3 — Analysis

Recall Solution

. . Factor . This matches : halve , double the vectors. Cost: twice the storage and slower nearest-neighbour search in the vector DB. Benefit: finer resolution — each vector represents one tighter idea, improving precision.

Figure — Chunking strategies for retrieval
Recall Solution

The fact has span , so it needs .

  • : the shared band is only 1 token wide; the 4-token fact cannot fit entirely inside either chunk → split, not retrievable intact.
  • : the shared band is 3 tokens; any window of length straddling the boundary fits fully in one chunk → the fact survives in the right-hand chunk (or left, depending on position). Look at the green band in the figure widening from 1 to 3 — that widening is literally the guarantee.
Recall Solution

Fixed-size cuts land at arbitrary token positions — mid-sentence, mid-table-row, or mid-code-block — producing chunks that are syntactically broken and semantically blurry. Recursive/structure-aware chunking splits on the biggest natural separator first (\n\n, then sentences), and document-structure chunking makes one chunk per section and attaches the heading path as metadata. Both keep each chunk a coherent idea, so embeddings are sharper and retrieval is more precise.

Recall Solution

Benefit: boundaries follow meaning (a genuine topic shift), not an arbitrary token count, so chunks are maximally coherent. Cost: you must embed every sentence to compute those distances — many extra embedding API calls, so it is slower and more expensive to build the index. See Text Embeddings and Cosine Similarity for the distance measure.


Level 4 — Synthesis

Recall Solution
  • (a) Strategy: Parent-document / small-to-big, layered on document-structure chunking (clauses are natural units).
  • (b) Index unit: a small precise piece — e.g. the single obligation sentence "Either party may terminate with 30 days' written notice." Small chunk → sharp embedding → nails the nearest-neighbour match.
  • (c) Return unit: the whole parent clause §7.3 — enough context for the LLM to answer and handle follow-ups without a second query.
  • (d) Metadata: the clause/section path (e.g. section = "7.3 Termination"), enabling metadata filtering to restrict search to relevant sections. Why this design: it decouples the retrieval unit (tiny, precise) from the generation unit (large, contextual) — the defining advantage of small-to-big.
Recall Solution

(i) Protect the span: need . (ii) cap: , so any is allowed. A clean choice satisfying both is (or the tidy ). Take : stride . Why these steps: the span constraint sets the floor on ; the rule sets the ceiling; picking anything in between and plugging into the stride formula gives the index size.


Level 5 — Mastery

Recall Solution

Let the chunks start at token positions . After chunks the last chunk ends at position: (The first chunk contributes its full length ; every one of the remaining chunks advances the coverage by one stride .) We need this to reach or pass the final token : Isolate : . Add : Since must be a whole number and we need the smallest such , take the ceiling:

Recall Solution

(a) : — the no-overlap formula falls straight out. ✓ Consistent. (b) : the whole document fits in one chunk. Plug : . For the numerator , so the ratio is and the ceiling is . ✓ Exactly one chunk — correct. (c) : the stride , so . Interpretation: chunks barely advance, so you need infinitely many to cover the document — the mathematical warning against runaway overlap.

Recall Solution

True token length: tokens. Teammate expected (treating as characters): chunks. Reality (model chunks by tokens, tokens): chunks. The mismatch is a error. Lesson: always chunk on the same tokenizer the model uses — see Tokenization — because "size" means tokens, and chars/token holds only for English (code and other languages differ wildly). This also interacts with Context Window Limits of LLMs: mis-sized chunks can silently overflow the window.

Recall Solution

Need . The binding condition is (ceiling only ever rounds up, so satisfying the un-ceiled inequality with a whole-token boundary is enough here). Solve for : Smallest allowed chunk size: tokens. Check: . ✓ If you tried , — over budget. So is exactly on the edge.

Recall Feynman check: say the whole ladder in one breath

Chunking sets the quality ceiling (L1). You size the index with and protect facts with (L2). Smaller chunks trade storage for precision, and structure beats blind cuts (L3). Real pipelines decouple a tiny precise index unit from a large contextual return unit (L4). And the formula behaves sensibly at every edge — no overlap, tiny docs, runaway overlap, token-vs-char, and hard budgets — as long as you never drop the ceiling (L5).