Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Cutting a manual into index cards
Imagine copying a manual onto index cards so you can look things up later. Cards with a single sentence such as ‘It must be torqued to 5 Nm’ are useless on their own: what is ‘it’? Cards with whole chapters answer every question but take forever to read. Good chunking finds pieces that make sense alone and are small enough to be precise, often by writing the chapter title on every card. The demo shows how chunk size and overlap change what a query can find.
Chunking is often the cheapest lever in a RAG system: changing how documents are split can matter more than changing the embedding model.
- 1. A chunk is…
- 2. Very small chunks risk…
- 3. Very large chunks risk…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Fixed-size: windows of N tokens, optionally overlapping by M tokens so content cut at a boundary appears whole in some chunk.
- Sentence or paragraph: split at natural boundaries; chunks are coherent but vary in length.
- Recursive character splitting: try paragraph breaks first, then lines, then sentences, then words, until every piece fits the size limit (LangChain’s default).
- Structure-aware: follow headings, sections, tables and code blocks; keep a table or a procedure step together.
- Contextual headers: prepend the document title and section path, or an LLM-written one-line context (Anthropic’s contextual retrieval, 2024), so each chunk stands alone.
- Small-to-big (parent document) retrieval: match on small precise chunks, then hand the model their parent section.
- Semantic chunking: split where the embedding of consecutive sentences changes sharply.
- Typical sizes: 200–800 tokens with 10–20% overlap; tune on your own questions.
- 1. Overlap helps because…
- 2. A contextual header adds…
- 3. Small-to-big retrieval returns…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Splitting documents in practice
Exercise 1.
- Write fixed-size windows with overlap.
- Write a recursive splitter that tries coarse separators first.
- Prepend a title and section header to a chunk.
Practice.
- LangChain RecursiveCharacterTextSplitter and MarkdownHeaderTextSplitter; LlamaIndex node parsers (sentence, semantic, hierarchical).
- Count chunk size in tokens of the embedding model, and stay under its maximum input length.
- Keep tables, code blocks and numbered procedures intact; parse PDFs carefully (layout, headers and footers).
- Store metadata with each chunk: document, section, page, version; it enables filtering and citations.
- Evaluate a few chunking settings on real questions with recall@k (F10-5) before committing.
from langchain_text_splitters import MarkdownHeaderTextSplitter, RecursiveCharacterTextSplitter
md = MarkdownHeaderTextSplitter([("#", "h1"), ("##", "h2")]).split_text(manual_markdown)
rec = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
chunks = []
for sec in md:
head = " > ".join(sec.metadata.values())
chunks += [f"{head}\n{c}" for c in rec.split_text(sec.page_content)]- 1. Chunk size should be measured in…
- 2. Tables and procedures should be…
- 3. Chunk metadata enables…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Boundary splitting. With fixed chunks of C tokens and no overlap, a span of L tokens at a random position straddles a boundary with probability (L − 1)/C. Overlap of at least L − 1 tokens guarantees that every span of length L lies whole in some chunk, at the cost of storing about C/(C − overlap) times as many tokens.
Dilution. For lexical or dense retrieval, a chunk’s score reflects its whole content. A large chunk containing the answer plus much unrelated text scores lower than a focused chunk would; BM25 normalises by length, and a single embedding averages over everything in the chunk.
Missing context. Pronouns, abbreviations and section-scoped facts make a small chunk ambiguous. The fix is not a bigger chunk but restoring the context: a header with the title and section, or an LLM-written summary line, makes the small chunk self-contained while keeping it precise and cheap.
- 1. Overlap of at least L − 1 guarantees…
- 2. A single embedding of a large chunk…
- 3. The fix for ambiguous small chunks is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How do chunk size, overlap and contextual headers trade retrieval accuracy against prompt cost?
History.
- LangChain’s recursive splitter (2022) made size-and-separator chunking the default in practice.
- LlamaIndex popularised hierarchical and small-to-big retrieval (2023).
- Anthropic, “Introducing contextual retrieval” (2024): prepending an LLM-written context to each chunk reduced retrieval failures substantially.
Exercise 2. BM25 retrieval over chunked synthetic documents.
- Corpus: 100 documents, one entity each, 150 sentences of 8–20 tokens; each sentence states one of 60 attributes. The entity’s name appears in only 30% of sentences; the rest say ‘it’.
- Queries: 600 of the form entity name + attribute. A hit means a top-5 chunk fully contains a sentence stating that attribute for that entity.
| chunking | hit@5 | prompt tokens per query |
|---|---|---|
| fixed 32 tokens | 0.383 | 160 |
| sentence | 0.692 | 65 |
| fixed 128 | 0.900 | 630 |
| fixed 128, overlap 64 | 0.937 | 638 |
| fixed 128 + title | 0.943 | 634 |
| fixed 512 | 0.997 | 2245 |
| fixed 512 + title | 0.997 | 2260 |
| sentence + title | 1.000 | 78 |
- Small chunks lost the entity: a sentence saying ‘it’ cannot match the entity’s name, and 32-token windows also cut sentences in half.
- Big chunks found everything but cost 29 times the tokens of titled sentence chunks.
- Overlap added about 4 points at 128 tokens by keeping cut sentences whole.
- A title header on sentence chunks gave perfect retrieval at 78 tokens.
- Seeds 1 and 2: sentence 0.685 and 0.672, fixed 128 0.927 and 0.910, sentence + title 1.0 on both.
- Caveat: here the header contains exactly the missing entity name, so its success is built in. Real documents have harder context (sections, scopes, abbreviations), and LLM-written context lines help less than this ideal header.
Chunking in AI, robotics and aerospace
- Maintenance manuals: procedures, warnings and torque tables must stay together; section paths such as ‘Landing gear > Retraction > Inspection’ make chunks self-contained.
- Standards and requirements: each requirement with its identifier and parent section is a natural chunk.
- Logs and telemetry notes: chunk by event or time window, with mission and subsystem in the header.
References
- Anthropic (2024). “Introducing contextual retrieval.” Engineering blog.
- LangChain documentation. “Text splitters.”
- LlamaIndex documentation. “Node parsers and hierarchical retrieval.”
- P. Lewis et al. (2020). “Retrieval-augmented generation for knowledge-intensive NLP tasks.” NeurIPS.