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.
- 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.
- 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.
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.
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.
- 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.
- 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.
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.
- 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.
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.
- 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.
- 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.
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.
- 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.
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.
- "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."