Chunking Strategies
- Implement fixed-size, recursive, and structure-aware chunking on the same document
- Explain the retrieval-precision vs context-completeness trade-off that chunk size controls
- Choose overlap and size defaults for prose, tables, and code, and justify them
- Attach metadata (source, section, position) at chunk time and say why it must happen there
| Spaced-rep: due cards + redraw the six RAG stages from memory (D113) | 10 min |
| ELI5 + tech read; chunk-strategies visualizer | 20 min |
| Guided: three tearings + the table rescue | 40 min |
| Practice: chunk your own repo docs | 20 min |
| Project: chunking decision card | 20 min |
| Quiz + flashcards | 10 min |
Builds on: Day 113 โ RAG architecture ยท Day 96 โ Tokenization
You're building the index for the open-book exam, and the book won't fit on index cards whole โ you have to tear it into pages first. Tear carelessly and every page ends mid-sentence: the card that says "the limit is" is useless without the card that says "500 USD." Tear too small and each card is a confetti scrap with no context. Tear too big and each card is half the book โ it matches every search vaguely and the student has to skim the whole thing anyway.
Chunking is the tearing strategy. The dumbest tear is "every 200 words, cut" (fixed-size). Smarter is "prefer to cut at paragraph breaks, then sentences, only then mid-sentence" (recursive). Smartest is "cut where the AUTHOR cut โ at headings and sections โ and write the chapter name on every scrap" (structure-aware). The strategy you pick decides what CAN ever be retrieved: retrieval returns chunks, not documents, so a fact split across two chunks is a fact your system half-knows forever.
Chunking is the most underrated dial in RAG โ practitioner postmortems routinely trace "the bot can't find things" to chunks that severed answers from their context, and no amount of embedding-model upgrades repairs a bad tear. It is also cheap leverage: re-chunking is a one-line config change plus re-index, versus weeks of model work. In your capstone (Day 119) and in any customer engagement, "show me your chunks for this failing query" is the first debugging move an experienced FDE makes โ you are learning that move today.
Tearing the book into useful pages โ three chunkings, three fates
step 1 / 5One HR document, one question coming later: "How much parental leave do contractors get?" How we tear the doc into chunks decides whether retrieval can ever answer it.
Guided practice
One document, three tearings
25 min- Create
chunk_lab.pywith the starter code: one realistic markdown policy doc and three chunkers โ fixed (with overlap), recursive, structure-aware. - Run it. For each strategy, read the printed chunks and count: how many chunks cut a sentence in half? How many separated a number from the rule it belongs to?
- Now run the retrieval comparison at the bottom: the same three queries against each strategy's index (reusing Day 113's cosine retriever).
- Query "what is the travel booking limit" should expose the fixed chunker: the phrase "500 USD" and the words "travel" or "pre-approval" land in different chunks. Verify which strategies return a chunk containing the COMPLETE answer.
- Note how the structure-aware chunks carry their heading path in the text โ and how that alone lifts their scores on section-flavored queries.
Break a table, then save it
15 min- Add this markdown table to the doc, under a new "## Per-diem rates" section: Region | Breakfast | Dinner โ three data rows (US 20/50, EU 15/45, APAC 12/40, as pipe-separated lines).
- Re-run the fixed chunker and find where the table got cut. Ask: if retrieval returns only the chunk with the "APAC" row, can a model know 12 means breakfast USD? (No โ the header is in another chunk.)
- Implement the fix: detect table lines (they contain " | ") and emit the table as ONE atomic chunk, header included. If a table exceeds the size limit, split by row-groups but prepend the header row to every group.
- Re-run retrieval for "APAC dinner per diem" and confirm the atomic-table strategy returns a self-sufficient chunk.
On your own
Chunk your own repo docs
20 minTake a real README or docs file from one of your own projects (Day 21 or Day 42 work). Run all three chunkers on it. Deliverable: a 10-line written verdict โ which strategy wins for THIS document and why; the worst single chunk each strategy produced (paste it); and your recommended size/overlap numbers with one sentence of justification each.
Constraints: judge chunks by asking "could a model answer a question from this chunk alone?" Hints: code fences are the tables of READMEs โ check whether any chunker cut one in half; if your doc has headings, structure-aware should win, so explain any surprise.
The chunking decision card
Create chunking_notes.md: a decision card you will reuse at capstone ingest time (Day 119). Contents: (1) a table of the three families with one-line mechanism, best-for, and failure mode; (2) your defaults โ size in tokens, overlap %, separator priority โ for prose, markdown docs, tables, and code, each with a one-sentence why; (3) the "torn answer" example from today's lab pasted verbatim as a cautionary exhibit; (4) three rules that must happen at chunk time (metadata stamping, table atomicity, heading-path prefixing). Commit it.
Common mistakes & misconceptions
- Chunking by characters while budgeting prompts by tokens. Measure chunk size in tokens (Day 96) โ a 1000-character code chunk can be 400 tokens; emoji-heavy text even more.
- Assuming bigger chunks are safer. Big chunks blur the embedding (many topics โ one vector), tanking retrieval precision. Retrieve-small, deliver-big is the real fix (Day 117).
- Using overlap as a substitute for good boundaries. Overlap patches cut wounds; recursive/structure-aware splitting avoids making them.
- Splitting tables from their headers or code mid-function. Keep them atomic, or replicate the header into every fragment.
- Deferring metadata to "later." After chunking, you cannot recover which section a chunk came from โ stamp source, heading path, and position at chunk time.
- Re-chunking without re-embedding and re-indexing everything. Chunks and their vectors must stay in lockstep; a partial re-index silently mixes two chunking generations.
Q1. Retrieval keeps returning a chunk that reads "...requires written manager" โ the amount is in the previous chunk. What is the cleanest fix?
Q2. What is the core trade-off that chunk size controls?
Q3. Why must metadata be attached at chunk time rather than later?
Go deeper โ curated resources
- Semantic chunking โ Embed sentences, cut where adjacent-sentence cosine similarity drops below a threshold. Elegant, ~10โ50x the embedding cost at ingest; try it when structure-aware boundaries still feel wrong.
- All three chunkers run; severed-answer failure demonstrated and explained
- Table made atomic and verified retrievable as a self-sufficient chunk
- Own-docs verdict written with recommended defaults
- Decision card committed
- Quiz โฅ 2/3
โ Back: Day 113 showed chunking as one of six stages โ today you learned it decides what can ever be retrieved. Day 96's tokenization is why sizes are measured in tokens, not characters.
Forward โ: Day 115 embeds these chunks for real; Day 117's parent-child retrieval resolves today's size trade-off outright. On Day 119 your capstone ingest pipeline applies today's decision card, and Day 136 measures chunking quality with context-precision metrics.
Unlocks: D116 Hybrid Search