LESSON 38 Β· CHUNKING

Fixed-size Chunking

Fixed-size chunking splits text into pieces using a chosen size, such as a number of characters or tokens. It is simple, predictable, and useful as a baseline for RAG systems.

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

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

  • Explain how fixed-size chunking works.
  • Distinguish character-based and token-based chunk sizes.
  • Understand why chunk overlap is often used with fixed-size chunks.
  • Implement simple fixed-size chunking with Python.
  • Understand the trade-off between chunk size, context, precision, and retrieval cost.
  • Choose and evaluate a practical starting size instead of assuming one size fits every dataset.
1

What Is Fixed-size Chunking?

Fixed-size chunking divides text into pieces according to a predetermined size. The size might be measured in characters, words, or tokens.

For example, if you choose 500 characters, the chunker repeatedly takes up to 500 characters from the source text until the document is consumed. The method does not first try to understand whether a sentence or section has finished.

DocumentLarge source text
β†’
Fixed sizeExample: 500 characters
β†’
ChunksChunk 1, 2, 3…
β†’
EmbedVector per chunk
Key idea:Fixed-size chunking gives you a predictable maximum size, but it does not automatically create semantically meaningful boundaries.
2

How It Works

The basic algorithm is straightforward: choose a size, start at the beginning of the text, take that many units, move forward, and repeat.

INPUT

One long document

β€œGenerative AI uses models to understand and generate content. Retrieval systems can add external knowledge…”

β†’
OUTPUT

Fixed windows

Chunk 1 = first window, Chunk 2 = next window, Chunk 3 = next window, and so on.

If the document has 2,400 characters and the chosen size is 600 characters, a simple non-overlapping implementation produces up to four chunks.

3

Character-based Chunking

Character-based chunking is the easiest version to understand. The chunk size is a number of characters.

</> Python
text = "Chunking splits long documents into smaller pieces for retrieval."
chunk_size = 20

chunks = [
    text[i:i + chunk_size]
    for i in range(0, len(text), chunk_size)
]

for number, chunk in enumerate(chunks, start=1):
    print(f"Chunk {number}: {chunk}")
What happens:the slice moves forward by 20 characters each time. The final chunk can be shorter than the requested size.
4

Character Size Does Not Mean Token Size

A character count and a token count are different measurements. Language models process text as tokens, while a character-based chunker counts characters.

CHARACTERS500 charactersA raw text-length limit. The number of tokens can vary.
WORDS100 wordsA word-based limit. Words are not the same as model tokens.
TOKENS300 tokensA token-based limit that is closer to model context accounting.
IMPORTANTUnits matterAlways document which unit your chunking system uses.
Practical point: a fixed 500-character chunk does not guarantee a fixed number of tokens. Token counts change with language, punctuation, formatting, and the tokenizer.
5

Token-based Fixed-size Chunking

Instead of counting characters, a system can tokenize the text and create chunks containing a maximum number of tokens. This is useful when you need tighter control over model context limits.

TextOriginal document
β†’
TokenizerConvert text to tokens
β†’
Fixed token windowExample: 300 tokens
β†’
ChunksToken ranges
Why tokens can help:your chunking limit can be expressed in the same basic unit used by the language model's context window, although exact tokenizer behavior depends on the model.
6

Python: Fixed-size Words

Here is a simple word-based version. It is useful for learning the algorithm, but production systems should consider the tokenizer and document structure.

</> Python
text = "Python makes fixed size chunking easy to understand."
words = text.split()
chunk_size = 4

chunks = [
    " ".join(words[i:i + chunk_size])
    for i in range(0, len(words), chunk_size)
]

for chunk in chunks:
    print(chunk)

This version groups four words at a time. Notice that it can still cut across a sentence or logical section.

7

The Problem With Hard Boundaries

A fixed window can end in the middle of a sentence, list item, code example, or explanation.

HARD CUT

Chunk 1: β€œThe refund is available within 30 days if the product is returned…”

Chunk 2: β€œβ€¦unused and in its original packaging.”

BETTER CONTEXT

A later chunking strategy can keep the complete condition together instead of splitting the idea at an arbitrary position.

Fixed-size chunking is therefore simple rather than universally intelligent. It can be a strong baseline, but it should be tested against your actual documents and questions.

8

Why Use Overlap?

Overlap repeats some content between neighboring chunks. The goal is to reduce the chance that an important idea is split exactly at a chunk boundary.

No overlapChunk 1: 1–500
Chunk 2: 501–1000
100-unit overlapChunk 1: 1–500
Chunk 2: 401–900
Large overlapMore repeated context, but more stored and processed text.
Trade-off:Overlap can improve continuity, but excessive overlap creates duplicate content and increases indexing and retrieval work.
9

Python: Fixed Size With Overlap

A simple character-based implementation can move forward by chunk_size - overlap instead of the full chunk size.

</> Python
text = "Fixed size chunking can use overlap to preserve nearby context."
chunk_size = 30
overlap = 8
step = chunk_size - overlap

chunks = [
    text[i:i + chunk_size]
    for i in range(0, len(text), step)
]

for number, chunk in enumerate(chunks, start=1):
    print(f"Chunk {number}: {chunk}")
Important:overlap must be smaller than the chunk size. In a production implementation, also handle empty text and validate the parameters.
10

Choosing a Chunk Size

There is no universally correct fixed size. A useful starting point depends on the document type, question style, embedding model, reranker, context window, and retrieval evaluation.

SMALL

Focused

Can improve precision, but related information may be split across multiple chunks.

MEDIUM

Balanced

Often a practical baseline because it keeps more context without making every result huge.

LARGE

Context-rich

Preserves more surrounding information, but can add unrelated text to retrieved context.

Ask
  • How long are the useful answers?
  • Are questions usually about one paragraph or a whole section?
  • How much context can the generator handle?
Then test
  • Several chunk sizes
  • Different overlap values
  • Real user questions
11

Fixed-size Chunking in RAG

In a RAG pipeline, fixed-size chunks are usually created during ingestion. Each chunk can then be embedded, stored, and retrieved later.

1. LoadRead source documents
β†’
2. ChunkFixed-size windows
β†’
3. EmbedVector per chunk
β†’
4. StoreVectors + metadata
User questionβ€œHow long is the warranty?”
β†’
Similarity searchRank chunk vectors
β†’
LLMAnswer from retrieved context

If the warranty sentence is split between two chunks, retrieval may need to return both pieces for the model to see the complete answer. Overlap can reduce this boundary problem.

12

Metadata for Fixed-size Chunks

Chunking should not discard the identity of the original source. Store useful metadata with every chunk so retrieval results can be traced back to the document.

SOURCE IDdoc-1024Identifies the original document.
CHUNK IDdoc-1024-07Identifies the particular chunk.
POSITIONstart: 3000Useful for locating the source text.
TITLEWarranty PolicyUseful for filtering, display, and citations.
Why it matters: a vector match tells you which chunk is similar. Metadata helps the application know where that chunk came from and how to present the source.
13

When Fixed-size Chunking Works Well

EXAMPLE 1Large plain-text collections

When documents have relatively consistent formatting, fixed windows can provide a simple and repeatable baseline.

EXAMPLE 2First RAG prototype

It is easy to implement and gives you something measurable before adding more sophisticated chunking logic.

EXAMPLE 3Uniform content

Content such as repeated records or consistently structured descriptions may tolerate fixed boundaries well.

It becomes less attractive when document structure carries important meaning, such as headings, tables, procedures, or tightly connected sections.

14

Common Mistakes

1. Treating one size as universally optimal

Different datasets and question patterns can need different chunk sizes.

2. Using a huge overlap

Too much repetition increases storage and processing without necessarily improving retrieval.

3. Ignoring units

β€œ500” is meaningless unless you know whether it means characters, words, or tokens.

4. Ignoring hard boundaries

A fixed window can split important ideas, headings, lists, and examples.

5. Forgetting metadata

Without source information, displaying citations and tracing results becomes harder.

6. Never measuring retrieval

Visual inspection is not enough. Test real questions and retrieved chunks.

15

How to Evaluate Fixed-size Chunking

Compare several configurations instead of guessing. For example, test multiple chunk sizes with and without overlap against a representative question set.

RELEVANCEDid the right chunk appear?

Check whether top results actually discuss the requested topic.

COMPLETENESSIs the answer intact?

Check whether needed information was split across chunks.

NOISEHow much extra text?

Check whether retrieved chunks contain too much unrelated context.

Configuration A500 characters + 0 overlap
Configuration B500 characters + 100 overlap
Configuration C800 characters + 100 overlap
DecisionChoose from measured retrieval quality, not a universal rule.
16

Fixed-size vs Other Chunking Methods

FIXED-SIZE

Predictable windows

Simple and consistent, but boundaries may cut through meaning.

VS
STRUCTURE-AWARE

Meaningful boundaries

Methods such as sentence, recursive, or semantic chunking try to preserve document meaning more carefully.

Fixed-size chunking is valuable because it is easy to understand, implement, benchmark, and improve. Later lessons will explore alternatives that make boundary decisions more context-aware.

PRACTICE

Build a Fixed-size Chunker

Take a paragraph of documentation and design two configurations.

  1. Create a simple fixed-size chunker using Python.
  2. Test one configuration without overlap.
  3. Test a second configuration with a small overlap.
  4. Ask five questions about the source document and inspect the retrieved chunks.
  5. Record which configuration gives the most relevant and complete context.
Challenge: explain one case where a larger chunk is better and one case where a smaller chunk is better. Your explanation should refer to context and retrieval precision.

Quick Quiz

1. What does fixed-size chunking do?

A. Splits text using a predetermined size
B. Summarizes every document
C. Removes all metadata

2. Which is a possible unit for fixed-size chunking?

A. Characters
B. Tokens
C. Both A and B

3. Why is overlap used?

A. To preserve some neighboring context
B. To guarantee perfect retrieval
C. To eliminate embeddings

4. What is a major weakness of fixed boundaries?

A. They can split an important idea
B. They cannot create vectors
C. They always require a GPU

30-Second Recap

1. FixedSplit text using a predetermined size.
2. UnitsUse characters, words, or tokens and document the unit.
3. OverlapRepeat nearby content to reduce boundary loss.
4. Trade-offsBalance context, precision, storage, and retrieval cost.
5. EvaluateTest configurations with real questions.

Key Takeaways

  • Fixed-size chunking creates predictable text windows using a chosen size.
  • The size can be measured in characters, words, or tokens; these units are not interchangeable.
  • Fixed boundaries can split meaningful content, so overlap can help preserve nearby context.
  • Chunk size and overlap affect retrieval quality, context size, storage, and processing cost.
  • Use fixed-size chunking as a practical baseline, then compare it with other strategies using real retrieval tests.