LESSON 43 Β· CHUNKING

Choosing Chunk Size

Learn how to choose a chunk size that gives your RAG system useful context without creating noisy, expensive, or hard-to-retrieve chunks.

30 min readβ€’Intermediateβ€’Generative AI

By the end of this lesson, you will be able to:

  • Explain why there is no single perfect chunk size for every RAG system.
  • Choose a starting chunk size using document type, model context, and retrieval needs.
  • Understand the relationship between chunk size and overlap.
  • Compare small and large chunks using Python experiments.
  • Evaluate retrieval quality instead of choosing a size by habit.
  • Use metadata and document structure alongside chunk size decisions.
1

What Is Chunk Size?

Chunk size is the amount of content placed into one chunk before that chunk is embedded and stored for retrieval. Depending on the chunker, size can be measured in characters, words, tokens, sentences, or another unit.

Example: 500 tokens per chunk β†’ approximately 500 tokens of source content in each chunk.

The important question is not β€œWhat number should I always use?” It is β€œHow much context does one retrieved chunk need to answer the questions users actually ask?”

Key idea: chunk size is a retrieval design decision. The best value depends on the content, query patterns, embedding model, generator context window, and evaluation results.
2

Why Chunk Size Matters

TOO SMALL

Context can disappear

A chunk may contain only one detail while the explanation, definition, or condition needed to interpret it lives in another chunk.

TOO LARGE

Retrieval gets noisy

A large chunk may contain the answer plus many unrelated paragraphs, making ranking and generation less focused.

A useful chunk size sits between these extremes: enough context to preserve meaning, but small enough for precise retrieval.

3

Small vs Large Chunks

SMALL

Precise retrieval

Smaller chunks often make it easier to retrieve a narrow fact or sentence.

MEDIUM

Balanced context

Medium chunks often provide a practical balance between precision and enough surrounding explanation.

LARGE

More context

Larger chunks can preserve complete sections, but may reduce retrieval precision and consume more context.

DECISION

Measure it

Use representative questions and compare retrieval quality rather than assuming one size is best.

4

Tokens Are Often a Better Unit

Words and characters are convenient, but tokens are often more useful when working with language models because model context limits are expressed in tokens.

</> Python
text = "Chunk size controls how much context each retrieved unit contains."

words = text.split()
print("Words:", len(words))
print("Approximate chunk target:", 12, "words")

This is only a simple illustration. Production systems should use a tokenizer appropriate for the embedding or generation model when token limits matter.

5

Start With the Document Structure

Chunk size should not be chosen independently of the document. A technical manual, FAQ, legal policy, and product catalog have different natural units of meaning.

TECHNICAL DOC

Sections and subsections

Preserve headings, explanations, code context, and related steps when possible.

FAQ

Question + answer

Keep each question with its answer instead of splitting the pair into unrelated chunks.

Rule: a structurally meaningful chunk is usually more useful than a chunk that only satisfies a numeric length target.
6

Chunk Size and Overlap

Chunk size and overlap work together. If the chunk size is 500 tokens and the overlap is 50 tokens, the next chunk starts 450 tokens after the previous start.

Step size = chunk size βˆ’ overlap

Increasing overlap can protect information near boundaries, but it also increases repeated content, storage, embedding work, and potentially retrieval redundancy.

7

Python: Compare Candidate Sizes

A simple experiment can show how many chunks different sizes create for the same document.

</> Python
def chunk_words(words, size):
    return [words[i:i + size] for i in range(0, len(words), size)]

text = ("RAG retrieves relevant context before generation. "
        "Chunk size affects retrieval precision and context. "
        "Good evaluation uses realistic user questions.")
words = text.split()

for size in [5, 10, 15]:
    chunks = chunk_words(words, size)
    print(size, "words ->", len(chunks), "chunks")

This does not prove which size is best. It gives you candidates to test with embeddings and real retrieval questions.

8

Chunk Size and Retrieval Precision

Suppose a user asks, β€œWhat is the refund window for damaged products?” A tiny chunk containing only β€œ30 days” may be hard to interpret. A huge chunk containing an entire returns policy may include many unrelated rules.

QUERY

Ask a realistic question

Use the same questions real users are likely to ask.

RETRIEVE

Compare top results

Check whether the relevant chunk appears near the top.

INSPECT

Read the context

Check whether the retrieved text contains enough information to answer correctly.

REPEAT

Test multiple sizes

Compare candidate sizes across many questions, not one example.

9

Chunk Size and Context Window

Your generator may support a large context window, but that does not mean every retrieved chunk should be large. Retrieval quality still matters.

If you retrieve several large chunks, their combined size can quickly consume the available context. Smaller, focused chunks can sometimes let the system retrieve more distinct evidence.

Think in budgets: query + instructions + retrieved context + conversation history + generated answer all compete for model context.
10

Chunk Size by Use Case

FACT LOOKUP

Often smaller

FAQ answers, product attributes, and short policies may benefit from focused chunks.

CONCEPT EXPLANATION

Often larger

Educational or technical material may need surrounding explanation, examples, and conditions.

CODE

Preserve structure

Keep a function with the relevant explanation and avoid splitting code arbitrarily when possible.

LEGAL / POLICY

Respect sections

Keep definitions, conditions, exceptions, and headings together when they form one rule.

11

A Practical Starting Strategy

There is no universal number, but you can use a controlled process instead of guessing.

STEP 1

Understand the content

Identify natural units such as sections, paragraphs, questions, or procedures.

STEP 2

Choose a few candidates

Test several reasonable sizes instead of tuning one value blindly.

STEP 3

Add appropriate overlap

Protect boundaries only as much as the content requires.

STEP 4

Evaluate retrieval

Measure whether the right evidence is retrieved for realistic questions.

12

How to Evaluate Chunk Size

Build a small evaluation set with questions and expected evidence. Compare candidate chunk sizes using metrics such as retrieval hit rate, ranking position, answer correctness, and context usefulness.

</> Python
results = {
    250: [1, 1, 0, 1, 1],
    500: [1, 1, 1, 1, 1],
    800: [1, 0, 1, 1, 0],
}

for size, hits in results.items():
    hit_rate = sum(hits) / len(hits)
    print(f"{size} tokens: {hit_rate:.0%} hit rate")

The example is intentionally simple. Real evaluation should use a larger, representative test set and inspect whether retrieved context actually supports the final answer.

13

Common Mistakes

  • Using one size everywhere: different document types may need different strategies.
  • Optimizing only for chunk count: fewer chunks does not automatically mean better retrieval.
  • Ignoring structure: splitting headings, definitions, or question-answer pairs can damage meaning.
  • Using excessive overlap: repeated content can increase cost and retrieval noise.
  • Testing one query: a single successful retrieval is not enough evidence.
  • Ignoring the generator: retrieved context must fit the overall context budget.
14

Chunk Size Is a System-Level Decision

Chunk size interacts with chunking strategy, overlap, embedding model, vector index, retrieval method, reranking, metadata filters, and the language model that generates the final answer.

Better mindset: do not ask β€œWhat is the best chunk size?” Ask β€œWhich chunking configuration gives my users the best retrieval and answer quality at an acceptable cost?”
15

Practice: Test Three Chunk Sizes

Take a short document and create chunks using three candidate sizes. Use five realistic questions and record whether the correct evidence appears in the top retrieved results.

TASK

Compare retrieval quality

Try small, medium, and large chunks. Keep the embedding and retrieval method the same, then compare the results.

Quick Quiz

1. Why can very small chunks hurt RAG?

They may retrieve a detail without the surrounding context needed to interpret it correctly.

2. Why can very large chunks hurt retrieval?

They can contain too much unrelated content, reducing precision and consuming more context.

3. What should determine chunk size?

Document structure, user questions, retrieval quality, model context, and system cost should all influence the decision.

4. Is there one universally correct chunk size?

No. The right size is workload-dependent and should be validated with evaluation data.

30-Second Recap

1. Balance

Enough context, without unnecessary noise.

2. Structure

Respect natural document boundaries.

3. Overlap

Use it to protect important boundaries.

4. Evaluate

Choose using realistic retrieval tests.

Key Takeaways

  • Chunk size directly affects retrieval precision and available context.
  • Small chunks can improve focus but may lose surrounding meaning.
  • Large chunks preserve context but can introduce noise and higher cost.
  • Tokens are often a useful unit when model context limits matter.
  • Document structure and overlap should be considered together with size.
  • The best chunk size is the one validated by your workload and evaluation set.