Split Documents into Chunks
Before a question-answering system can search your documents, it has to decide what a search result is. It is almost never a whole document: the documents are cut into smaller passages called chunks, and the search ranks those. Three choices shape them: where the cuts fall, how long each chunk is, and what each chunk remembers about its origin. Together they decide whether the passage that answers a question can be found at all, and whether the answer can later point to where it came from.
Why search returns pieces, not documents
Picture the manual of a washing machine. It covers installation, the wash programs, cleaning the filter and what each error code means. Someone asks: "How do I clean the filter?" Only one of those sections answers it. The rest of the manual is about other things.
If the search could only return whole documents, it would hand back the entire manual. Three things go wrong with that. First, matching gets blurry. A search compares the question with a summary of each candidate's meaning. A whole manual's meaning is a mix of everything in it, so the filter section is diluted by pages about installation and error codes. A passage about one topic is compared on that topic alone, so it matches a question about that topic much more exactly. Second, every passage the system finds is later sent to the language model inside its prompt, and every word sent costs money and waiting time. Third, an answer should say where it came from, and "the manual" is a weak source compared with "the manual, section Cleaning the filter".
- Installing the machine
- Wash programs
- Cleaning the filter
- Error codes
- washing machine manual, cut into chunks
So the documents are split once, ahead of time, and each chunk becomes a separate search candidate. A good chunk passes a simple test: a person who reads it on its own, without the pages around it, understands what it says. If a person can make sense of it, so can the search and the model that answers from it.
There is also a hard limit behind the soft reasons. The model that turns a text into its searchable form, the embedding model, reads only a fixed number of tokens (the word pieces a model counts in) and cuts off the rest. Text past that limit never reaches the search at all, so a long document has to be split anyway.
True or false?
With a model that can read a very long prompt, splitting documents gives no benefit.
False. A smaller passage still matches a question more exactly, costs less to send and can be cited by its section. Long context changes none of that.
A chunk that makes sense to a person reading it alone is also a good unit for search.
True. A chunk that holds a complete idea carries the meaning the search compares against, and gives the model something it can answer from.
Cutting where the author already did
The simplest way to split a text is by length: every 1,000 characters, cut. It is easy to write, and it ignores everything the text says about itself. A cut every 1,000 characters lands wherever the count runs out: in the middle of a sentence, between a step and its warning, or across a heading so that the end of one section and the start of the next share a chunk.
Most documents already mark where one idea ends and the next begins. The author grouped sentences into paragraphs and paragraphs into sections, and gave each section a heading. In Markdown those marks are plain to see, and a splitter can cut exactly there:
# Washer manual
## Installing the machine
Place the machine on a level floor and remove the transport bolts.
## Cleaning the filter
Clean the filter once a month, or when water stays in the drum.
Open the small flap at the bottom front, place a shallow tray
under it and turn the filter slowly to the left.
## Error codes
E21 means the water is not draining. Clean the filter first.Splitting at the headings gives one chunk per section, and every chunk starts where a topic starts and stops where it stops. The heading itself says what the chunk is about. When a section is still too long for one chunk, the next mark down is the paragraph: paragraphs are separated by blank lines, and cutting between them keeps each sentence whole. Only when a single paragraph is too long does a splitter fall back to cutting between sentences, and lines or single words come last. So each step down uses a smaller natural boundary, and the splitter always cuts at the largest boundary that still makes the pieces fit.
- washer-manual.md
- Installing the machine
- Cleaning the filter
- Error codes
- chunk 1 whole section
- chunk 2 paragraph 1
- chunk 3 paragraph 2
- chunk 4 whole section
- washer-manual.md to Installing the machine
- washer-manual.md to Cleaning the filter
- washer-manual.md to Error codes
- Installing the machine to chunk 1
- Cleaning the filter to chunk 2
- Cleaning the filter to chunk 3
- Error codes to chunk 4
The tree assumes the filter section was too long for one chunk, so it was cut between its two paragraphs; short sections stay whole.
Finding headings looks trivial, but a few rules of Markdown matter:
- A heading is a line that starts with one to six
#characters followed by a space. The number of#is its level:##is a section under a#title. - A word like
#hashtaghas no space after the#, so it is ordinary text. - Lines inside a code block are never headings, even when they start with
#, as a comment in a shell script does. A splitter that cut at every line starting with#would slice a code example in two. - A blank line that contains only spaces still separates two paragraphs. A splitter that looks for two newlines in a row should therefore ignore trailing spaces first.
True or false?
Every line that begins with a # character is a Markdown heading.
False. A heading needs one to six # characters followed by a space, and lines inside a code block never count. A shell comment or #hashtag is not a heading.
A section that is too long can be split further at its blank lines, between paragraphs.
True. Paragraphs are the next boundary the author left. Cutting there keeps every sentence whole and every chunk on one idea.
How big a chunk should be
Even when the cuts follow the structure, a splitter needs a limit on how long a chunk may grow. That limit is the chunk size, and choosing it is a trade between two ways to fail.
A chunk that is too small no longer holds a whole answer. If the filter instructions are split into one sentence per chunk, the search may find "turn the filter slowly to the left" and miss the sentence before it, which says to put a tray underneath first. A tiny chunk can also lose what it is about: "Do this once a month" means nothing without the sentence that says what "this" is.
A chunk that is too large drifts across several topics. Its meaning becomes an average of all of them, so it matches each single question less well, and the one sentence that matters is diluted by the ones around it. Large chunks also cost more and take longer to send to the model. Past a certain length they lose text outright, because the embedding model cuts off everything past its input limit.
| Chunk size | What one chunk holds | What can go wrong |
|---|---|---|
| one sentence | one step of the instructions | the answer is spread over chunks; a chunk may not say what it is about |
| one section or paragraph | how to clean the filter, start to finish | little: the chunk answers one kind of question whole |
| several sections | filter, error codes and warranty together | blurred matching, more cost per question, text past the model limit is lost |
So the goal is a chunk large enough to hold an answer whole and small enough to stay about one thing. No single number is right for every collection: it depends on how long the sections of these documents are and on the embedding model. A common rule from practice is a few hundred tokens per chunk.
The embedding model's input limit sets the ceiling. That limit is counted in tokens, not in characters or words, and for many small embedding models it is a few hundred tokens. For a sense of scale, 512 tokens are roughly 300 to 400 English words. A chunk size measured in characters is only a rough stand-in for a token count, so it is worth checking that the longest chunks still fit.
Splitting along structure makes the choice easier. When the documents have sections of a sensible length, most chunks are simply one section each, and the size limit only steps in for the few sections that are too long.
True or false?
The embedding model reads every word of a chunk, however long the chunk is.
False. It reads up to its maximum input length in tokens and cuts off the rest. Words past the limit have no effect on the search.
Making every chunk bigger can make search results worse, even though each chunk then holds more.
True. A chunk that covers several topics matches each one less exactly, and the relevant sentence is diluted by unrelated ones.
Overlap: repeating text across a boundary
Wherever a splitter cuts by length, a cut can land inside an answer. Say the answer to a question is two sentences long, sentences 3 and 4 of a passage, and the splitter takes three sentences per chunk. The first chunk ends after sentence 3 and the second begins with sentence 4, so neither chunk holds the whole answer. Each half matches the question only partly, and the model receives at best half of what it needs.
Overlap is the usual remedy: each chunk starts a little before the previous one ended, so the text around every boundary appears in two neighbouring chunks. With an overlap of one sentence, the second chunk becomes sentences 3 to 5, and the answer sits whole inside it.
- Without overlap
- chunk 1
- chunk 2
- the cut splits the answer
- sentence 1
- sentence 2
- sentence 3
- sentence 4
- sentence 5
- sentence 6
- the answer: sentences 3 and 4
- With one sentence of overlap
- chunk 2 holds the whole answer
- chunk 3
Overlap has a price. The repeated text is stored, turned into vectors and searched twice, and when two neighbouring chunks are both found, the model receives the same sentences twice. The share is often large: one hosted vector store, OpenAI's, splits files by default into chunks of 800 tokens with 400 tokens of overlap, so half of every chunk is a copy of its neighbour.
Overlap only helps where a boundary can fall inside an idea, and that is exactly what splitting along structure avoids. A cut at a heading falls where the author closed one topic and opened another, so there is no half-answer on either side to repeat. Repeating the end of the error codes section at the start of the warranty section would add unrelated text to both chunks. Overlap earns its place when a long section has to be cut by length after all, or when the documents have no headings or paragraphs to follow.
True or false?
Every chunking scheme needs some overlap, or answers will be lost at the boundaries.
False. Overlap protects against cuts that land inside an idea. When cuts fall at headings, there is nothing to protect, and overlap only adds repeated text.
With overlap, the same sentence can appear in two different chunks.
True. That repetition is the whole mechanism: the text around a boundary is copied into both neighbours, so one of them holds it whole.
A chunk remembers where it came from
Once a document is cut into pieces, a piece no longer knows which document it belonged to. Take a chunk that reads "Clean the filter once a month, or when water stays in the drum." Which machine? Which manual? A reader shown that sentence as the basis of an answer has no way to check it, and a system that wants to cite it has nothing to cite.
So every chunk is stored as a small record, not as bare text: its text, the document it came from and the heading of the section it sits under. When sections have subsections of their own, the heading can be kept as the whole path down to the subsection, such as Cleaning > The filter, because a heading like "The filter" alone could belong to several sections. A splitter that cuts at headings already knows the heading, and the path above it, at the moment it cuts, so recording it costs nothing.
- chunk 2
- text Clean the filter once a month...
- document washer-manual.md
- heading Cleaning the filter
That source does two jobs later on. When a search returns the chunk, the source can be shown beside it, so a person sees at once which document and which section the passage came from and can open it to check. And when an answer is written from several chunks, each statement can be tied back to the document and heading of the chunk it came from.
The heading can also help the chunk make sense on its own. A chunk that starts with "Do this once a month" is about nothing in particular, while the same text with "Cleaning the filter" above it is clearly about the filter. Many splitters therefore keep the heading in the chunk's text as well as in its source, so the search sees it too.
True or false?
Storing only the text of each chunk is enough, since the text can always be searched for in the original documents later.
False. The same sentence can appear in several documents, and searching the originals again is extra work. Recording the document and heading when the chunk is made keeps the link exact.
A splitter that cuts at headings knows each chunk's heading at the moment it creates the chunk.
True. It has just read that heading to decide where to cut, so storing it with the chunk is free.