AI ENGINEERING · RETRIEVAL AND RAG IN DEPTH · INDEXING · 21 OF 35

Chunking Strategies for RAG: Size, Overlap, Structure

Why documents are split before embedding, fixed-size chunks with overlap and a worked count, recursive and structure-aware splitting for markdown, HTML, tables and code, semantic chunking, choosing a chunk size, metadata and title prefixes on every chunk, parent-child retrieval, re-embedding when documents change, and the mistakes that quietly break retrieval.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
1 / 17

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • One idea per chunk
  • split on structure
  • ~10-20% overlap
  • metadata + title on every chunk
  • match small, return big

Notes

Why split

You might think, just embed each document. But an embedding model turns one piece of text into one vector. Squash a forty page manual into one vector and you get an average of fifty topics, close to everything and sharp about nothing. Embedding models also limit their input, often to a few hundred or a few thousand tokens. And at answer time you want to hand the model five passages, not five files.

Why not embed whole files
  • one vector per piece of text
  • whole manual → one vector: an average of 50 topics, matches nothing sharply
  • embedding models cap input (~512 to ~8k tokens)
  • the LLM prompt has a budget: send 5 passages, not 5 files

The idea

So we split. A chunk is a passage that can answer a question on its own, and it is the unit everything else works on. It is what gets embedded, what gets matched against the question, and what gets pasted into the prompt. The pipeline: parse the source into clean text that keeps its structure, split, attach metadata, embed, store. A good chunk holds one idea and still makes sense when read out of context.

The chunk is the unit
  • chunk = a passage that can
  • answer a question on its own
  • it is what gets embedded, what gets matched, what gets pasted in the prompt
  • a good chunk holds one idea
  • and makes sense out of context

Fixed size + overlap

The simplest method is fixed size. Pick a size in tokens, say five hundred and twelve, and cut. On its own, that slices sentences in half at every border. So you add overlap, here sixty four tokens. Each new chunk starts sixty four tokens before the previous one ended, so the step between starts, called the stride, is four hundred and forty eight. A short sentence that straddles a border now appears whole in at least one chunk. It is fast and predictable, but it ignores meaning entirely.

Fixed size with overlap
size    = 512 tokens
overlap = 64 tokens
stride  = size - overlap = 448

chunk 1: tokens    0 -  511
chunk 2: tokens  448 -  959
chunk 3: tokens  896 - 1407

tokens 448-511 sit in chunks 1 and 2:
a short sentence cut at a border
still appears whole in one chunk

Worked: count chunks

Let us count one. A two thousand token document, chunks of five hundred and twelve, overlap of sixty four, so a stride of four forty eight. Chunks start at token zero, four forty eight, eight ninety six, thirteen forty four and seventeen ninety two. The last one holds only two hundred and eight tokens. Five chunks. The rule is length minus overlap, divided by the stride, rounded up. And overlap is not free: you embed about thirteen percent more tokens than the document has.

Worked: 2,000-token document
size 512, overlap 64, stride 448

1:    0 -  511   512 tokens
2:  448 -  959   512
3:  896 - 1407   512
4: 1344 - 1855   512
5: 1792 - 1999   208  (short tail)

chunks = ceil((2000 - 64) / 448) = 5
embedded: 2,256 tokens (~13% extra)

Recursive splitting

Recursive splitting is the sensible default. Give it a list of separators from biggest to smallest: headings, then blank lines between paragraphs, then line breaks, then sentence ends, and finally spaces. It splits on the biggest one first. Any piece still over the size limit is split again with the next separator down. Then small neighbouring pieces are merged back together until they approach the limit. It follows the writer's own boundaries and cuts mid sentence only as a last resort.

Recursive splitting
  • try to split on, in order: 1 headings (#, ##) 2 blank lines (paragraphs) 3 line breaks 4 sentence ends (. ? !) 5 spaces, then characters
  • piece still over the max? split it with the next one then merge small neighbours back up toward the max

Structure-aware

Better still, use the document's real structure, which is why the parser matters. In markdown, split on headings and remember the heading path above each piece. In HTML, use heading and section tags, and throw away navigation, footers and cookie banners. Keep a table whole if it fits. Otherwise split it into groups of rows and repeat the header row on every chunk. Split code by function or class, never in the middle of one. And for PDFs, fix multi column layouts and strip page headers before splitting anything.

Structure-aware chunking
  • markdown: split on headings, keep the heading path
  • HTML: use h1-h3 and sections, drop nav, footer, cookie bar
  • tables: whole table, or row groups with the header row repeated on every chunk
  • code: split by function or class, keep file + signature
  • PDF: fix columns, strip page headers and numbers first

Semantic chunking

Semantic chunking splits on meaning instead of characters. Embed every sentence, then measure the cosine similarity between each sentence and the next. Neighbours on the same topic score high, say zero point eight two and zero point seven nine. Where the topic changes, the score drops, here to zero point three one. Cut there. The chunks follow the ideas in the text. The catch is cost, one embedding per sentence at ingest, and chunk sizes you cannot predict. Measure it against recursive splitting before you commit.

Semantic chunking
embed each sentence, compare
each one with the next:

  s1 → s2   0.82
  s2 → s3   0.79
  s3 → s4   0.31   ← topic shift
  s4 → s5   0.85

cut where similarity drops below
a threshold or a low percentile:
  [s1 s2 s3]  [s4 s5]

Chunk size

Size is the big trade off. Too small, say fifty tokens, and a chunk matches sharply but cannot answer anything. A chunk saying it rose four percent is useless without knowing what rose. Too large, say two thousand tokens, and the vector blurs many topics, so the right chunk ranks lower, and every chunk you retrieve fills the prompt with text that does not help. Most teams start between two hundred and fifty and a thousand tokens with ten to twenty percent overlap, then tune.

Chunk size trade-offs
  • too small (~50-100 tokens) sharp match, but too little context: "it rose 4%" - what?
  • too large (~2,000+ tokens) vector blurs many topics, prompt fills with filler
  • common start: ~256-1,024 tokens, ~10-20% overlap
  • then tune on real questions from your own users

Metadata

Every chunk needs metadata, stored next to its vector. The source, so you can cite and link it. The title and section path, so a reader knows where it came from. The date it was last updated, so you can prefer fresh text. Access rights, so the retriever filters out chunks this user may not read before anything reaches the model. And a hash of the text, which you will need when documents change.

One chunk record
id:      leave-policy#carry-over/1
text:    "Unused days carry over..."
source:  hr/leave-policy.pdf
title:   Leave policy
section: Annual leave > Carry-over
page:    4
updated: 2026-03-02
acl:     [all-staff]
hash:    9f3c...
vector:  [0.12, -0.04, ...]

Add the context

A chunk cut from the middle of a document often loses its subject. Unused days carry over, up to five, until the thirty first of March. Days of what, from which policy? The fix is cheap. Before embedding, prepend the document title and section path to the chunk text. Now the vector knows this is about annual leave. A stronger version asks a small model to write one or two sentences of context for each chunk. It costs a model call per chunk at ingest, but can noticeably cut missed retrievals.

Give each chunk its context
  • raw chunk: "Unused days carry over, up to 5, until 31 March."
  • embedded as: "Leave policy > Annual leave > Carry-over. Unused days carry over, up to 5, ..."
  • stronger: a small model writes
  • 1-2 lines of context per chunk

Parent-child

You can escape the size trade off with parent child retrieval. Split each section into small child chunks, around a hundred to two hundred tokens, and embed only those. Each child remembers its parent, the full section it came from. At query time, search the children, because small chunks match precisely. Then collect their parent ids, remove duplicates, and send the parents to the model, because larger passages carry the context needed to answer. Match small, return big.

Parent-child retrieval
  • index small children (~100-200 tokens): precise matching
  • each child points to a parent (its section, ~1,000 tokens)
  • search children, collect
  • parent ids, remove duplicates, send the parents to the LLM
  • match small, return big

When docs change

Documents change, and the index must follow. When a file is edited, parse and chunk it again. Give each chunk a stable id built from the document, the section and its position, and store a hash of its text. Same hash, keep the old vector. New hash, embed it and update the row. An id that disappeared, delete it, or the model will quote a retired policy. Changing chunk settings or the embedding model is different. Vectors from different models cannot be compared, so build a fresh index alongside, then switch.

When documents change
1 re-parse and re-chunk the file
2 stable id: doc + section + n
3 hash each chunk text
4 same hash  → keep the vector
  new hash   → embed + upsert
  id is gone → delete it
5 file deleted → delete all chunks

new chunk settings or a new
embedding model: build a fresh
index in parallel, then switch

Common mistakes

A few mistakes come up again and again. Splitting a table mid row, so one chunk has half a row and no header, and the model reads a price against the wrong product. Dropping headings, so chunks lose their subject. Leaving boilerplate like footers and menus, so dozens of chunks share the same text and the top results become near copies. Indexing several versions of the same file. Tiny leftover tail chunks. And using one chunk size for slide decks, contracts and code alike.

Common mistakes
  • table split mid-row: half a row, no header, wrong answer
  • headings dropped: chunks with no subject
  • boilerplate in every chunk: footers, legal text, menus; top results become copies
  • duplicate files (v1, v1-final)
  • tiny 20-token tail chunks
  • one size for every doc type

Numbers worth knowing

Some numbers worth knowing. Chunks of roughly two hundred and fifty to a thousand tokens, with ten to twenty percent overlap. Embedding models accept about five hundred to eight thousand tokens per input. A page of prose is five hundred to eight hundred tokens. Prompts usually get three to ten chunks. Ten thousand documents of two thousand tokens make about fifty thousand chunks. At a thousand and twenty four dimensions in thirty two bit floats, each vector is four kilobytes, about two hundred megabytes in all.

Numbers worth knowing
chunk size        ~256 - 1,024 tokens
overlap           ~10 - 20%
embedding input   ~512 - 8k tokens max
page of prose     ~500 - 800 tokens
chunks per prompt ~3 - 10

10k docs x 2k tokens ≈ 50k chunks
1,024-dim float32 = 4 KB per vector
50k vectors ≈ 200 MB before index

Say it like this

How to say it. We parse documents with their structure intact, split recursively on headings, then paragraphs, then sentences, at about five hundred tokens with sixty four tokens of overlap, and keep tables whole with their headers. Every chunk carries its source, section, date and access list, and is embedded with its heading path prepended. Content hashes drive incremental re-embedding when files change. And we chose the size by measuring retrieval on real questions, not by guessing.

In the interview
  • "We parse with structure, split on headings, then paragraphs, ~512 tokens with 64 overlap, tables kept whole. Each chunk has source, section, date and ACL, and is embedded with its heading path. Hashes drive incremental re-embedding. Size was tuned on real questions."

Interview questions

Q1Why do RAG systems split documents into chunks before embedding?

An embedding model produces one vector per input, so a whole document becomes a blurry average that matches no specific question well. Embedding models also cap input length, and the LLM prompt has a budget, so you want to retrieve and send focused passages rather than entire files.

Q2How many chunks does a 2,000-token document produce with 512-token chunks and 64 tokens of overlap?

The stride is 512 minus 64, which is 448. Chunks start at 0, 448, 896, 1344 and 1792, so there are five, the last holding 208 tokens. In general it is ceil((N minus overlap) / stride), and here about 13 percent extra tokens get embedded.

Q3What is recursive character or text splitting?

It splits on the largest natural separator first (headings, then paragraphs, lines, sentences, words) and recurses with smaller separators only for pieces still over the size limit, then merges small neighbours. It respects document structure and rarely cuts mid-sentence.

Q4How do you choose a chunk size?

Balance precision against context: small chunks match sharply but may lack the information to answer, large ones dilute the vector and waste prompt tokens. Start around 256 to 1,024 tokens with 10 to 20 percent overlap, then pick the size that maximises recall on a set of real questions.

Q5How should tables and code be chunked?

Keep a table whole if it fits, otherwise split it into row groups and repeat the header row in every chunk so values keep their column meaning. Split code on function or class boundaries and keep the file path and signature with each chunk.

Q6What metadata should every chunk store?

A stable id, source location, document title, section path, last-updated date, access-control list and a content hash. These enable citations, freshness preferences, permission filtering at query time and incremental updates.

Q7What is parent-child (small-to-big) retrieval?

Index small child chunks for precise matching, but store a link to the larger parent section each came from. At query time, search the children, deduplicate their parents and send the parents to the model, which gets precise matching and enough context.

Q8How do you keep a chunk index up to date as documents change?

Re-chunk changed files, give chunks stable ids and content hashes, re-embed only chunks whose hash changed, and delete chunks that disappeared or whose source was deleted. If you change chunk settings or the embedding model, rebuild a new index in parallel and switch reads, since vectors from different models are not comparable.