Lesson 9.4core3 h 20 min⚡ live demo

Chunking and ingestion

The unglamorous stage that decides retrieval quality

Everything downstream reads the text you extract and the pieces you cut it into. Parsing errors, chunks that split a table from its header, or passages that lose the name of the product they describe cause more RAG failures than any choice of model. This lesson covers converting messy sources to clean structured text, the chunking strategies and their trade-offs, small-to-big retrieval, Anthropic's contextual retrieval, and how to make ingestion incremental and measurable.

You'll be able to

  • Convert PDFs, HTML and code into structured text that preserves headings and tables, and recognize common parsing failures
  • Explain why chunking is necessary and the trade-off between chunk size, embedding quality and context
  • Implement token-based, recursive and structure-aware chunkers and know when semantic or LLM-based chunking is worth its cost
  • Apply small-to-big retrieval and contextual retrieval to recover lost context
  • Design metadata, deduplication and incremental re-indexing for a living corpus

Key ideas

  1. Parsing quality sets the ceiling: convert every source to structured Markdown, keep page numbers and originals, and inspect outputs by eye.
  2. Chunking exists because embeddings dilute over long text, context is finite, and the chunk is the unit of evaluation and citation.
  3. Small chunks match precisely but lose context; large chunks keep context but blur the embedding — typical sweet spots are 200–800 tokens with 10–20% overlap, decided by measurement.
Show 4 more key ideas
  1. Five strategies in increasing cost: fixed-size by tokens, recursive, structure-aware, semantic (embedding drops), LLM-based propositions.
  2. Small-to-big (sentence windows, parent–child, multi-representation) decouples what you match on from what the model reads.
  3. Contextual retrieval prepends an LLM-written 50–100 token context to each chunk before embedding and BM25 indexing; Anthropic reported 49% fewer top-20 retrieval failures, 67% with reranking.
  4. Carry rich metadata (source, section path, page, timestamps, permissions, model version), deduplicate, and make ingestion incremental with content hashes.

Suggested path through this lesson