Chunking: cutting documents into pieces
Before RAG can find answers in your documents, the documents have to be cut into pieces. Where and how much you cut decides whether the answer to a question stays whole in one piece or gets split across two. Try cutting the text below yourself.
Short answer. Chunking is cutting a long document into pieces that are a good size for search. If a piece is too big, unrelated text gets mixed in and it costs more. If it is too small, the surrounding context is lost. So we cut at sentence and paragraph boundaries, and let neighboring pieces overlap a little.
Color band and number = a chunk. Neighboring chunks have different colors. Stripes = overlap, text that is in two chunks. Thick underline = the answer to the chosen question.
Test with questions
List of chunks
Try other text
Your text is cut only inside this browser. It is not sent anywhere and not saved. Up to 5,000 characters are used.
This is a small, simplified model. Real chunking measures size in tokens. Here we count characters (spaces and line breaks included) because they are easier to see. There is no search here. "Whole" means the answer is all inside one chunk, not that the chunk would actually be found. The sample text is a guide for a made-up shop.
Definitions
The text below explains these by cutting a made-up notice by hand. These are the textbook definitions. Length is measured in characters or tokens.
- chunking
- Cutting a document into pieces that become the units for retrieval. It is also called text splitting. One piece is called a chunk.
- chunk size
- The upper limit on the length of one chunk.
- chunk overlap
- The length of the part that two neighboring chunks share. When cutting at a fixed length, the next chunk starts (chunk size − overlap) after the start of the chunk before it.
- recursive splitting
- A method that first splits at large boundaries (paragraphs) and splits again at smaller boundaries (sentences, words) only the pieces that are longer than the chunk size.
- semantic chunking
- A method that compares the embeddings of neighboring sentences and cuts where the meaning changes a lot.
- metadata
- Information stored with each chunk, such as which document and which place in it the chunk came from and who may see it. It is used to show sources and to filter search results.
1. Why cut at all
When RAG gets a question, it searches stored text for the parts closest to the question and passes them to a language model. The unit it searches is a chunk. Cutting documents into chunks is called chunking. There are two reasons to cut.
- Length has a limit. An embedding model turns text into a list of numbers (a vector), and it can only take so much text at once. Text past the limit may simply be cut off and ignored. The language model that writes the answer also has a limit on how much it reads at once (the context window), and longer input is slower and costs more.
- One chunk is one vector. If you turn a long text into a single vector, the meanings of many topics get blended together, and the vector ends up only halfway close to every question. A chunk should hold one topic, so that a question about that topic comes out close to it. Think of a book with one index word for the whole book, compared with index words for every page.
But tiny chunks lose context. A chunk that only says "We are closed on Mondays." does not say who is closed. And if an answer sentence is split in half across two chunks, neither chunk has the whole answer.
| If chunks are | Good | Bad |
|---|---|---|
| too big | Surrounding context comes along. Fewer chunks | Unrelated text blurs the meaning. Longer text goes to the language model, which costs more |
| too small | Clear meaning that matches questions well | Context is cut off. Answers get split across chunks. Many chunks |
2. Ways to cut
The methods differ in where they put boundaries. The four buttons in the experiment are the first four methods below.
| Method | What it does | Good | Weak |
|---|---|---|---|
| Fixed length | Counts characters or tokens and cuts at even steps | Simple and fast. Even sizes | Cuts through words and sentences |
| Sentences | Never cuts a sentence. Packs neighboring sentences within the size | Sentences stay whole | The rules for where a sentence ends differ by language |
| Paragraphs | Never cuts a paragraph (a block between blank lines). Packs paragraphs within the size | Follows the blocks of meaning the writer made | Long paragraphs go over the size. Heading lines can get separated from their text |
| Headings and structure | Follows the document structure: headings, HTML tags, Markdown | One chunk tends to hold one topic. Easy to attach the heading as extra information | Sections vary a lot in length. Does not work on text with no structure |
| Semantic | Compares embeddings of neighboring sentences and cuts where the meaning changes a lot | Finds where the topic changes | Needs an embedding for every sentence, so it costs more. Not in this experiment |
Real tools mix these. A common way is to try the biggest boundary first: split into paragraphs, split only the paragraphs that are too long into sentences, and if a sentence is still too long, split it into words.
Try this. ① The starting setup (fixed length, size 100, overlap 0) gives 11 chunks. Q1 falls across chunks 2 and 3, and Q2 across chunks 4 and 5, so both are "Cut". ② Keep overlap at 0 and press "Pack sentences". You get 15 chunks, Q1 to Q3 are all "Whole", and the extra text is 0%. ③ In "Paragraphs", chunk 1 is 68 characters and holds only the two heading lines, and chunk 2 is 171 characters, longer than the size, because a paragraph is never cut. ④ "Headings" gives 6 chunks. The shortest is 145 characters and the longest is 258.
3. Size and overlap
Chunk size is the most text one chunk can hold. Overlap is how much text two neighboring chunks share. Cutting by fixed length goes like this.
step = size − overlap
start of chunk k = k × step (k = 0, 1, 2, …)
chunk k = characters [start, start + size) of the text (the last chunk goes to the end)
extra text = (sum of chunk lengths ÷ text length − 1) × 100%
With overlap, a sentence on a boundary goes in twice: at the end of one chunk and at the start of the next. If it is cut in one, it gets a chance to stay whole in the other. With fixed length, if the overlap is at least L characters, any sentence of L characters or less is always whole in some chunk. Wherever the sentence starts, the chunk that holds that start reaches past it by at least the overlap.
Overlap has a price. The step gets shorter, so you get more chunks, and you compute and store more embeddings. With size s and overlap o, the total grows to about s ÷ (s − o) times the text. If the overlap is half the size, that is almost double. And if two chunks with the same sentence are both found, the language model gets the same text twice.
When you pack sentences, overlap is not counted in characters. Sentences at the end of the previous chunk whose total length fits in the overlap are copied whole to the start of the next chunk. If a single sentence is longer than the overlap, there is no overlap.
Try this. ① With fixed length and size 100, raise the overlap to 30. Q1 to Q3 all become "Whole". In return, the chunks go from 11 to 16, and the extra text is 41%. ② At overlap 20, Q1 is whole but Q2 is still cut. Its answer is 79 characters, longer than the overlap, so when a boundary falls inside it, neither side holds all of it. ③ At overlap 35, Q2 is cut again, even though it was whole at 30. Changing the overlap also moves the boundaries. More overlap is not always better.
4. A worked example: size 20, overlap 5
The text below is 55 characters, counting spaces. Positions start at 0. In the table, ␣ is a space.
Cats sleep a lot. They nap all day. At night they play.
- Step.
20 − 5 = 15. - Starts. 0, 15, 30, 45. The chunk that starts at 45 reaches the end of the text (55), so we stop there.
- Chunks. Take 20 characters from each start. The last chunk is the 10 characters that are left.
Chunk Positions Length Text 1 0 to 19 20 Cats sleep a lot. Th2 15 to 34 20 t. They nap all day.3 30 to 49 20 ␣day. At night they␣4 45 to 54 10 they play. - Overlaps.
t.␣Th(15 to 19) is in chunks 1 and 2,␣day.(30 to 34) in chunks 2 and 3, andthey␣(45 to 49) in chunks 3 and 4. - Extra text.
20 + 20 + 20 + 10 = 70. That is 15 characters more than the 55 in the text.70 ÷ 55 − 1 ≈ 27%. - Cut sentences. The second sentence, "They nap all day." (positions 18 to 34), is whole in chunk 2. The third, "At night they play." (36 to 54), starts in chunk 3 and ends in chunk 4, so no chunk has all of it. It is 19 characters, longer than the overlap of 5.
If you cut the same text with "Pack sentences" (size 20, overlap 5), each sentence becomes one chunk: 18, 18 and 19 characters (counting the space after each sentence). Any two sentences together are over 20. Every sentence is longer than the overlap of 5, so nothing overlaps.
Try this. Press "Worked example (size 20, overlap 5)" in the experiment. You get the same 4 chunks as the table, with 27% extra text. Then press "Pack sentences": 3 chunks, 0% extra.
5. Size is measured in tokens, not characters
Models read text not as characters but as pieces called tokens. Both the input limit of a model and what you pay are counted in tokens. So real chunking counts tokens with the tokenizer of the model you will use.
Characters and tokens do not line up evenly. The same number of characters can be a very different number of tokens, depending on the language and the tokenizer. A chunk you sized at "500 characters" may fit under one model's limit and go over another's. A chunk longer than the embedding model's limit becomes a vector with its end cut off, so it may never be found by what was in that end part.
6. Information to attach to each chunk
A chunk is text torn out of a document. If you do not know where it came from, you cannot show a source, and you do not know who is allowed to see it. So each chunk is stored with extra information (metadata).
| Information | Example | Used for |
|---|---|---|
| Document name or address | shop-guide.md | Showing the source under the answer, with a link to the original |
| Heading path | Shop guide › Refunds | Showing the source. If you put it in front of the chunk text before embedding, the chunk keeps what it is about |
| Page number or position | page 3, characters 663 to 734 | Jumping to that spot in the original. Fetching neighboring chunks |
| Date or version | March 2026 version | Filtering out old versions and searching only the latest |
| Who may see it | staff only | Filtering by permission before the search |
There are two main uses. Filtering: you add conditions to the search, like "only chunks this person may see, from the latest version". Filtering by permission matters most. If a chunk the user may not see reaches the language model, it can leak into the answer. Showing sources: if the answer shows which part of which document it came from, the reader can open the original and check.
7. Things you should not cut
- Tables. Cut in the middle, and the numbers in the lower chunk have no column names. You cannot tell if "3,000" is a price or a count. Keep a table together. If it is too big, split it every few rows and repeat the header row in each chunk.
- Code. Cut a function in half, and neither half makes sense. Cut by function or block.
- Lists. If "There is no refund in these three cases:" is separated from its items, a chunk with only the items does not say what they are about. Keep a list with the sentence before it.
The rules in this experiment (blank lines, # heading lines) cannot see tables or code. Real tools first read the document format (HTML, Markdown, PDF) to find its structure, then cut.
8. When cutting alone is not enough
The answer to Q4, "I return an electric bike after 5 minutes. How much do I get back?", needs two sentences: the rule in "Refunds" (within 10 minutes, a full refund) and the exception in "Electric bikes" (always keep 2 dollars). They are in different paragraphs. If only the chunk with the rule is found, the language model will wrongly answer "a full refund".
No way of cutting solves this well. A huge chunk can hold both sentences, but for other questions that chunk drags in lots of unrelated text. So you solve it outside the cutting too.
- Fetch several chunks (the top k), not just one.
- Attach the heading path to each chunk, so the "Electric bikes" chunk shows that it is about renting and refunds.
- When writing documents, put an exception right next to its rule. It is easier for people to read, too.
Search with small chunks, hand over big chunks (parent-child). Small chunks with a clear meaning are better for finding. Big chunks with more context are better for answering. So you use both. Cut the document into big chunks (parents, for example one section each), then cut each parent into small chunks (children, a few sentences each). Search with the children, and give the language model the parent of each child that was found. Each child just needs its parent's id as metadata. This helps when context gets cut off, but for an answer spread over two distant sections, like Q4, both parents still have to be found.
Try this. Pick Q4. In the starting setup, its first sentence is itself cut across chunks 7 and 8. In "Pack sentences" (size 100, overlap 0), it is "Scattered" across chunks 10 and 15, and in "Headings" across chunks 4 and 6. With fixed length, a size of 550 puts both sentences in chunk 2 of 2, but at 400 they are scattered again, across chunks 2 and 3. It depends on luck: where the boundaries happen to fall.
9. Key properties
| Property | In plain words |
|---|---|
| One chunk = one vector | A chunk should hold one topic to be found by questions about that topic |
| Bigger size means fewer chunks, and more mixed text in each | You trade context for clarity |
| Fixed length: if overlap ≥ L, every sentence of length ≤ L is whole in some chunk | If the overlap is longer than the answer, the answer survives a boundary |
The total grows to about size ÷ (size − overlap) times | Overlap of half the size almost doubles storage and computing |
| Moving boundaries changes which sentences get cut | A setting that saves one question can cut another. Measure with many questions |
10. There is no right size: measure with your own questions
The best method and size depend on the kind of text (short questions and answers, long contracts, code), the kind of questions, and the embedding model. Do not take a number you saw somewhere as the answer.
- Collect a few dozen questions you really expect, and for each one, write down the document and the spot that holds its answer.
- For each setup (method, size, overlap), cut and store the documents, then search with the questions.
- Count how often a chunk with the answer is in the top k results. How to measure this is on retrieval metrics.
- Pick the best setup, and measure again when the documents change.
"Test with questions" on this page is a tiny version of the first step. It does not search. It only checks whether the answer is whole inside one chunk. Changing the setup means embedding every chunk again, so store a version number for the setup you used along with the chunks.
Where it is used
- Chatbots that answer from documents (RAG). Company rules, product manuals and help pages are cut into chunks and stored. The chunks closest to a question are found and used in the answer.
- Summarizing long text. Text too long for a model to read at once is summarized chunk by chunk, and then the summaries are combined.
- Search engines. Even keyword search can show more exactly where a match is if long documents are indexed by page or section.
- Translation and proofreading. Text longer than the input limit has to be fed in parts. Cutting in the middle of a sentence makes the translation awkward, so cut at sentence and paragraph boundaries.
Frequently asked questions
What is chunking?
It is cutting a long document into pieces that are a good size for search. In RAG, each piece is turned into a vector and stored. When a question comes in, the pieces closest to it are found and passed to the language model.
What chunk size should I use?
There is no size that is right for everyone. It depends on the kind of text, the kind of questions and the embedding model. Write down real questions and where their answers are, try several sizes, and measure how often the chunk with the answer is found.
Do I need overlap?
When you cut by fixed length, overlap helps save sentences that fall on a boundary. In return, you get more chunks and more storage, and the same text can show up twice in the search results. If you cut at sentence or paragraph boundaries, you need less overlap.
Should I measure size in characters or in tokens?
In tokens. The input limit of a model and the price are both counted in tokens. The same number of characters can be a very different number of tokens, depending on the language and the tokenizer.
Is semantic chunking always better?
No. It has to compute an embedding for every sentence, so it costs more, and at least one study found that it was not consistently better than fixed-size chunking. Measure with your own documents and questions before you choose.
Words
| Words used here | Words in books and docs |
|---|---|
| Cutting documents into pieces | Chunking, text splitting |
| Piece | Chunk, passage |
| Most text one chunk can hold | Chunk size |
| Text two neighboring chunks share | Chunk overlap |
| Trying the biggest boundary first to fit the size | Recursive splitting |
| Cutting where the meaning changes | Semantic chunking |
| Extra information attached to a chunk | Metadata |
| Narrowing the search by that information | Metadata filtering |
| Search with small chunks, hand over big chunks | Parent-child retrieval, small-to-big |
| A model that turns text into a list of numbers | Embedding model |
| The most text a model takes at once | Max sequence length, context window |
| The unit a model reads text in | Token |
Sources
- Lewis et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. The paper that introduced RAG under this name.
- Karpukhin et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. It cut Wikipedia articles into equal-length pieces (passages) that do not overlap, and used them as the unit of search.
- Chen et al. (2023). Dense X Retrieval: What Retrieval Granularity Should We Use? A study showing that the unit you search by (passage, sentence, or smaller units of fact) changes the quality of search and answers a lot.
- Qu, Tu, Bao (2024). Is Semantic Chunking Worth the Computational Cost? A study that found the computing cost of semantic chunking was not paid back by consistent gains.
- Liu et al. (2023). Lost in the Middle: How Language Models Use Long Contexts. A study showing that information in the middle of a long input is used less well. A cost of handing over many big chunks.
- Sentence Transformers docs: Input Sequence Length. Each embedding model has a maximum input length, and longer text is cut down to its first part.