Foundations to Frontier
Tests
F. NLP and Large Language Models › F10 Retrieval-Augmented Generation

Chunking strategies

Before documents can be retrieved they must be split into chunks, and the split decides what the retriever can find and what the model gets to read. Chunks that are too small lose context, such as the name of the thing a sentence is about. Chunks that are too large dilute the match and fill the prompt with irrelevant text. This page covers fixed-size, sentence, recursive and structure-aware chunking, overlap, contextual headers and small-to-big retrieval. In the exercises you write fixed-window and recursive splitters and a header function, then measure retrieval over 100 synthetic documents in which most sentences say 'it' rather than the entity's name. Sentence chunks found the answer in the top 5 only 69% of the time and 32-token chunks 38%. 512-token chunks reached 99.7% but cost about 2250 prompt tokens per query. Adding the document title to each sentence chunk reached 100% at 78 tokens.

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.

Retrieval lab
Retrieval quality at 256 tokens: 0.92 (relative). Too small loses context; too large dilutes the match.
Why engineers care

Chunking is often the cheapest lever in a RAG system: changing how documents are split can matter more than changing the embedding model.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A chunk is…
  2. 2. Very small chunks risk…
  3. 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.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Overlap helps because…
  2. 2. A contextual header adds…
  3. 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)]
🐍 Exercise 1: splitters and headers
Write fixed-size windows with overlap, a recursive splitter and a contextual header.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Chunk size should be measured in…
  2. 2. Tables and procedures should be…
  3. 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.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Overlap of at least L − 1 guarantees…
  2. 2. A single embedding of a large chunk…
  3. 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.
🐍 Exercise 2 (research): how do chunk size, overlap and contextual headers trade retrieval accuracy against prompt cost?
Chunk synthetic documents with fixed windows, sentences, overlap and title headers; measure BM25 hit@5 and prompt tokens.

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

  1. Anthropic (2024). “Introducing contextual retrieval.” Engineering blog.
  2. LangChain documentation. “Text splitters.”
  3. LlamaIndex documentation. “Node parsers and hierarchical retrieval.”
  4. P. Lewis et al. (2020). “Retrieval-augmented generation for knowledge-intensive NLP tasks.” NeurIPS.