Context and retrieval

Chunking Lab

Retrieval works on chunks, not whole documents. Chunk too large and retrieval brings irrelevant text; too small and a chunk loses the context that makes it meaningful. The lab runs nine chunking methods on your text and shows the boundaries, sizes, overlap, and which chunks a sample question retrieves.

What Is Chunking in RAG?0:48 · beginner

The trade-offsBeginner

Smaller chunks give more precise matches but carry less context. Overlap repeats text at boundaries so a fact split across two chunks still appears whole in one, at the cost of more tokens to embed and store.

Larger chunksSmaller chunks
More context per hitMore precise hits
Fewer embeddingsMore embeddings
More irrelevant text in the promptFacts can be split across chunks

Structure-aware chunkingIntermediate

Markdown-aware chunking splits at headings and keeps the heading path with each chunk; code-aware chunking keeps functions and classes whole. Structure-aware methods usually retrieve better than fixed sizes on structured documents.

Semantic chunkingIntermediate

Semantic chunkers start a new chunk where the topic changes, usually detected by comparing sentence embeddings. The lab’s version compares shared words instead (it calls no embedding model), which shows the idea but misses synonyms.

Hierarchical and contextual chunksAdvanced

Parent-child chunking retrieves small chunks but sends their larger parent section to the model. Contextual chunking prepends a short description of where the chunk came from (document title, section) before embedding, which helps retrieval of chunks that make little sense alone.

Key takeaways

  • Chunk by structure when the document has it.
  • Overlap trades tokens for recall.
  • Test chunking with real questions.
Use the chunks in the RAG simulator