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.

    1. 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.
    2. 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 areGoodBad
    too bigSurrounding context comes along. Fewer chunksUnrelated text blurs the meaning. Longer text goes to the language model, which costs more
    too smallClear meaning that matches questions wellContext 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.

    MethodWhat it doesGoodWeak
    Fixed lengthCounts characters or tokens and cuts at even stepsSimple and fast. Even sizesCuts through words and sentences
    SentencesNever cuts a sentence. Packs neighboring sentences within the sizeSentences stay wholeThe rules for where a sentence ends differ by language
    ParagraphsNever cuts a paragraph (a block between blank lines). Packs paragraphs within the sizeFollows the blocks of meaning the writer madeLong paragraphs go over the size. Heading lines can get separated from their text
    Headings and structureFollows the document structure: headings, HTML tags, MarkdownOne chunk tends to hold one topic. Easy to attach the heading as extra informationSections vary a lot in length. Does not work on text with no structure
    SemanticCompares embeddings of neighboring sentences and cuts where the meaning changes a lotFinds where the topic changesNeeds 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.

    1. Step. 20 − 5 = 15.
    2. Starts. 0, 15, 30, 45. The chunk that starts at 45 reaches the end of the text (55), so we stop there.
    3. Chunks. Take 20 characters from each start. The last chunk is the 10 characters that are left.
      ChunkPositionsLengthText
      10 to 1920Cats sleep a lot. Th
      215 to 3420t. They nap all day.
      330 to 4920␣day. At night they␣
      445 to 5410they play.
    4. Overlaps. t.␣Th (15 to 19) is in chunks 1 and 2, ␣day. (30 to 34) in chunks 2 and 3, and they␣ (45 to 49) in chunks 3 and 4.
    5. Extra text. 20 + 20 + 20 + 10 = 70. That is 15 characters more than the 55 in the text. 70 ÷ 55 − 1 ≈ 27%.
    6. 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).

    InformationExampleUsed for
    Document name or addressshop-guide.mdShowing the source under the answer, with a link to the original
    Heading pathShop guide › RefundsShowing the source. If you put it in front of the chunk text before embedding, the chunk keeps what it is about
    Page number or positionpage 3, characters 663 to 734Jumping to that spot in the original. Fetching neighboring chunks
    Date or versionMarch 2026 versionFiltering out old versions and searching only the latest
    Who may see itstaff onlyFiltering 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

    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.

    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

    PropertyIn plain words
    One chunk = one vectorA chunk should hold one topic to be found by questions about that topic
    Bigger size means fewer chunks, and more mixed text in eachYou trade context for clarity
    Fixed length: if overlap ≥ L, every sentence of length ≤ L is whole in some chunkIf the overlap is longer than the answer, the answer survives a boundary
    The total grows to about size ÷ (size − overlap) timesOverlap of half the size almost doubles storage and computing
    Moving boundaries changes which sentences get cutA 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.

    1. Collect a few dozen questions you really expect, and for each one, write down the document and the spot that holds its answer.
    2. For each setup (method, size, overlap), cut and store the documents, then search with the questions.
    3. Count how often a chunk with the answer is in the top k results. How to measure this is on retrieval metrics.
    4. 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

    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 hereWords in books and docs
    Cutting documents into piecesChunking, text splitting
    PieceChunk, passage
    Most text one chunk can holdChunk size
    Text two neighboring chunks shareChunk overlap
    Trying the biggest boundary first to fit the sizeRecursive splitting
    Cutting where the meaning changesSemantic chunking
    Extra information attached to a chunkMetadata
    Narrowing the search by that informationMetadata filtering
    Search with small chunks, hand over big chunksParent-child retrieval, small-to-big
    A model that turns text into a list of numbersEmbedding model
    The most text a model takes at onceMax sequence length, context window
    The unit a model reads text inToken

    Sources

    ← RAGVector search →