Day 114 ยท Tearing the book into useful pages

Chunking Strategies

You will be able to
  • 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
Today's ~120 minutes
Spaced-rep: due cards + redraw the six RAG stages from memory (D113)10 min
ELI5 + tech read; chunk-strategies visualizer20 min
Guided: three tearings + the table rescue40 min
Practice: chunk your own repo docs20 min
Project: chunking decision card20 min
Quiz + flashcards10 min

Builds on: Day 113 โ€” RAG architecture ยท Day 96 โ€” Tokenization

The analogy

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.

Why this matters on the job

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.

Watch it happen

Tearing the book into useful pages โ€” three chunkings, three fates

step 1 / 5
the document
ยง3 Employee leave: 16 weeksโ€ฆ
ยง3.2 Contractors: leave accrues
at 1 day per month worked,
up to 12 weeks maximum.
ยง4 Expense policy: receiptsโ€ฆ

One 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

guided 1

One document, three tearings

25 min
  1. Create chunk_lab.py with the starter code: one realistic markdown policy doc and three chunkers โ€” fixed (with overlap), recursive, structure-aware.
  2. 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?
  3. Now run the retrieval comparison at the bottom: the same three queries against each strategy's index (reusing Day 113's cosine retriever).
  4. 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.
  5. Note how the structure-aware chunks carry their heading path in the text โ€” and how that alone lifts their scores on section-flavored queries.
๐Ÿ python โ€” editable, runs in your browser
Ctrl/โŒ˜+Enter runs ยท Tab indents ยท numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)
guided 2

Break a table, then save it

15 min
  1. 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).
  2. 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.)
  3. 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.
  4. 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 min

Take 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.

Ship before you stop

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.

Rubric โ€” check what you completed (0/5)

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.
Knowledge check

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

articlePinecone Learning Center โ€” chunking strategies โ†—20 mindocsLangChain docs โ€” text splitters โ†—15 mindocsLlamaIndex โ€” node parsing & ingestion โ†—15 min
If you have a third hour
  • 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.
Done means
  • 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
How this connects

โ† 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