RAGEngineering Hub
Chapter 4 · concept 3 of 6 · show all
  1. 1What RAG is, and why
    1. 1.1Why models need outside knowledge, and what hallucination means
    2. 1.2What RAG is: anatomy of a request
    3. 1.3RAG vs fine tuning vs long context and prompt caching
    4. 1.4Use cases and the quality, latency and cost triangle
    5. Chapter overview
  2. 2Tokens, cost and your first RAG
    1. 2.1Tokens and context windows for RAG
    2. 2.2The RAG lifecycle and your first RAG in miniature
    3. Chapter overview
  3. 3Getting documents in
    1. 3.1Content sources and file formats
    2. 3.2Parsing, layout and OCR
    3. 3.3Tables, images and mixed content
    4. 3.4Cleaning, normalization and quality gates
    5. 3.5Metadata, document identity and versioning
    6. 3.6Updates, deletes, reindexing and ingestion pipelines
    7. Chapter overview
  4. 4Chunking
    1. 4.1Why we chunk, and the main chunking strategies
    2. 4.2Structure aware chunking and tables
    3. 4.3Chunk size, overlap and token based sizing
    4. 4.4Contextual chunking and late chunking
    5. 4.5Parent child and multi representation indexing
    6. 4.6Chunk metadata, provenance and the chunking debugging playbook
    7. Chapter overview
  5. 5Embeddings
    1. 5.1Embeddings and similarity from first principles
    2. 5.2What embeddings cannot do
    3. 5.3Choosing an embedding model: size, cost, language
    4. 5.4Embedding model migration and index rebuilds
    5. Chapter overview
  6. 6Vector stores and scores
    1. 6.1Vector databases, ANN indexes and choosing a store
    2. 6.2Score calibration and thresholds
    3. 6.3Late interaction and multivector retrieval
    4. Chapter overview
  7. 7Finding the right chunks
    1. 7.1Query understanding, rewriting and expansion
    2. 7.2Dense retrieval and keyword search with BM25
    3. 7.3Hybrid retrieval and rank fusion
    4. 7.4topK, no good match, and metadata filtering
    5. 7.5Routing, diversity, deduplication and caching
    6. 7.6Reranking: two stage, cross encoder and LLM rerankers
    7. Chapter overview
  8. 8When retrieval fails
    1. 8.1Retrieval failure analysis playbook
    2. Chapter overview
  9. 9Building the answer
    1. 9.1Context selection, packing and citations
    2. 9.2Grounding prompts and abstention
    3. 9.3Lost in the middle and context management
    4. 9.4Conflicts, staleness and freshness rules
    5. 9.5Hallucination checks and answer verification
    6. 9.6Conversational RAG, answer UX and RAG APIs
    7. Chapter overview
  10. 10Measuring
    1. 10.1Why measure, and what to measure
    2. 10.2Building a golden dataset
    3. 10.3Retrieval metrics: precision, recall, MRR, nDCG
    4. 10.4Faithfulness and answer correctness
    5. 10.5LLM as judge, and judging the judge
    6. 10.6Offline and online evals, regression tests and slices
    7. Chapter overview
  11. 11Safety and operations
    1. 11.1Prompt injection and poisoned documents
    2. 11.2Access control, privacy and governance
    3. 11.3Index lifecycle operations
    4. 11.4Observability and tracing
    5. 11.5Latency, cost, caching and model routing
    6. 11.6Reliability, degraded modes and launch readiness
    7. Chapter overview
  12. 12Scale and advanced RAG
    1. 12.1Scaling, multi tenancy and enterprise architecture
    2. 12.2Choosing an architecture: naive, advanced, agentic, graph
    3. 12.3Agentic RAG and RAG as an MCP tool
    4. 12.4GraphRAG, structured data and multimodal RAG
    5. Chapter overview

Chunk size, overlap and token based sizing

How big each piece of a document should be, why you measure it in tokens, and how to find the right size with a number instead of a guess.

A real question

Why does my RAG answer only half right, even though the answer is in the document?

Often because the answer was cut in half when the document was split into pieces. Pick the piece size by measuring, not by habit.

The problem

Why this matters

Pick one size, say 1,000 characters, and use it for every document. Bigger overlap is always safer.
The belief

Pick one size, say 1,000 characters, and use it for every document. Bigger overlap is always safer.

Size pieces in tokens, keep one idea per piece, and test sizes on questions with known answers. On our documents, 512 tokens beat 128, and overlap made results worse.
What really happens

Size pieces in tokens, keep one idea per piece, and test sizes on questions with known answers. On our documents, 512 tokens beat 128, and overlap made results worse.

Why not the simpler way

The first thing most people try is one fixed size for everything, often 1,000 characters, because a tutorial used it. It works in a demo. On real documents it cuts tables in half and splits the one sentence that answers the question across two chunks, so neither chunk is a good match.

The idea

The idea in plain words

A chunk is the piece of text the search can return. Too small, and a chunk holds half an answer. Too big, and the answer is buried among unrelated text, so it ranks lower and costs more tokens in the prompt.

Think of index cards for a talk. One word per card is useless; the whole speech on one card is unreadable. You want one idea per card.

How it works

Step by step

  1. Every manual is split into pieces called chunks. Where the cuts land decides what search can find later.
    1

    Cut the document into cards

    Every manual is split into pieces called chunks. Where the cuts land decides what search can find later.

  2. Search compares the question with every chunk and returns the best few. It can only return a card, never half of two.
    2

    The question picks the closest card

    Search compares the question with every chunk and returns the best few. It can only return a card, never half of two.

  3. If the card holds the whole answer, the reply is right and can cite it. If the answer was cut, the model sees only part of it.
    3

    The model answers from that card

    If the card holds the whole answer, the reply is right and can cite it. If the answer was cut, the model sees only part of it.

In detail

Measure chunks in tokens, not characters or words. The embedding model has a token limit, and the prompt budget is in tokens. On our documents a word is 1.39 tokens on average (Part 1), but code, numbers and other languages break that rule.

Overlap repeats the last few tokens of one chunk at the start of the next, so a sentence on a boundary appears whole in at least one chunk. It also adds near-duplicate chunks that can crowd the top results.

The code

Try it in Python

import tiktoken
enc = tiktoken.get_encoding("o200k_base")

def fixed_chunks(text, size=512, overlap=0):
    ids = enc.encode(text)
    step = size - overlap
    return [enc.decode(ids[i:i + size]) for i in range(0, len(ids), step)]

See it

What we measured

Measured · evidence recall@3

25 Polder documents, 47 golden questions with a known answer passage, bge-small-en-v1.5 embeddings, vector search only. A hit counts only when a top 3 chunk holds the whole answer, which favours bigger chunks: a 128 token chunk often holds only part of it.

Fixed 128 tokens, no overlapbaseline0.766
Fixed 512 tokens, no overlap0.7660.894
Fixed 512 with 15% overlap0.8940.851
Recursive 512 tokens0.8940.936

Measured 2026-11-04 with corpus/scripts/precompute.mts. Overlap did not help here: the near-duplicates pushed better chunks out of the top 3. Chunks are sized with the o200k tokenizer, but the embedder counts its own WordPiece tokens and drops anything past 512. One FiberBox chunk was 520 of them, so the evidence for g-02 and g-07 was never embedded. Every chunk over the limit is now split again with the embedder's own count (corpus/scripts/precompute.mts); 1024 sizes are left as they are, so their rows include the cost of truncation.

Try it on a real document

A customer asks: “Should I reset the router when the power light is solid red?”

Cut the manual every

FiberBox manual · 5. Restart and reset

Release the button. The router restarts with factory settings. The default Wi-Fi name and password are printed on the sticker under the router. After a reset, the router downloads its login profile automatically, which can take up to ten minutes. Do(chunk boundary)not reset a router that shows a solid red power light. A reset cannot fix a hardware fault and may make it harder for support to read the fault log.

↑ the cut lands between “Do” and “not”

What search returns (top 3)

  1. 1.chunk #13 · 0.868not reset a router that shows a solid red power light. A reset cannot fix…–
  2. 2.chunk #3 · 0.747. If still red, call support | Fibre | Solid green | …–
  3. 3.chunk #2 · 0.7427.3.0 are no longer supported and should be updated first…–

The best match starts in the middle of the sentence. The word “Do” was left behind in the previous chunk, which is all about how to reset. No chunk in the top 3 holds the whole warning.

Choosing

Use it when, avoid it when

Use it when
  • Most prose: start at 256 to 512 tokens and measure
  • Long policy or manual sections with clear headings: split by structure first
Avoid it when
  • Tables and code: a fixed window breaks rows and functions; split by structure
  • Very short documents: one chunk per document is often best

What it costs · Smaller chunks mean more vectors to store and more chunks needed to cover one answer; bigger chunks mean more prompt tokens per answer.

In production

Pick the size with a golden set: questions with a known answer passage. Count how often a top 3 chunk contains the whole answer. That single number turns a style debate into a measurement.

Watch out

Common mistakes

  • More overlap is always safer. It adds duplicates that crowd out better chunks, as the numbers above show.
  • The model limit is the right size. An 8,000 token embedding limit says what fits, not what retrieves well.

Check yourself

Remember this

Remember these

  • Measure chunk size in tokens, the unit models count in.
  • Too small splits answers; too large buries them and costs more.
  • Test sizes with questions whose answer passage you know: here 128 tokens found 36 of 47, 512 found 43.
  • Overlap is not free: near-duplicate chunks pushed better ones out of the top 3.

Say it in an interview

  1. 01Chunk size decides what the search can return, so it caps answer quality.
  2. 02I size chunks with the embedding model's own tokenizer and check none pass its limit; a mismatch here once cut off the end of one chunk.
  3. 03I pick the size with a golden set and evidence recall, not by habit.
  4. 04On my corpus, 512 tokens beat 128 by 15 points; overlap did not help.
  5. 05Tables and code get structure-aware splitting instead of fixed windows.
Try it yourself: Chunk LabChange the chunk strategy and size, and see how often the answer survives in one piece.

Quick quiz

Three questions

Question 1

Q1

Why measure chunk size in tokens instead of characters?

Question 2

Q2

What does a golden question need for evidence recall?

Question 3

Q3 · from RAG-03

A tokenizer turns text into what?

Saved on this device. No account needed.