AI Hints
Home›RAG Tutorial›Cleaning and Chunking
RAG · Lesson 18 of 120

Fixed-Size Chunks

Size and overlap.

⏱ 2 min read▶ Video at 17:39🎓 Free
Watch this lesson in the RAG courseWatch on YouTube ↗Full RAG course

The simplest way to chunk is by size. Take a fixed number of characters, then the next, and so on. An overlap repeats a little text between neighbours, so an idea cut at the edge appears in both.

Fixed-Size Chunks
From the video: Fixed-Size Chunks
  • Fixed number of characters
  • Then the next
  • With a little overlap

Chunking by characters

Chunk steps through the text, taking size characters each time, and moving forward by size minus overlap. We split the returns page into chunks of one hundred and twenty characters, with twenty overlapping. Run it. It works, but look at the edges. Words and sentences are cut in half.

fixed_chunks.pypython
from pathlib import Path

def chunk(text, size, overlap):
    step = size - overlap
    return [text[i:i + size] for i in range(0, len(text), step)]

text = Path("data/rag/northwind/returns.md").read_text()
pieces = chunk(text, size=120, overlap=20)
print(len(pieces), "chunks")
for p in pieces[:3]:
    print(repr(p))
Output
7 chunks
'# Returns and refunds\n\n## Change of mind\nYou can return any unused item within 30
days of delivery for a full refund.\nBi'
'or a full refund.\nBikes must be unridden, with the original packaging and all
reflectors and manuals.\nWe charge a £25 co'
'.\nWe charge a £25 collection fee to pick up a returned bike. Parts can be posted back
at your own cost.\n\n## Faulty items'

Size and overlap

Chunk size controls how much text each piece holds. Overlap helps when an idea crosses a boundary, but too much overlap creates near duplicates. A common start is a few hundred characters, with ten to twenty percent overlap.

keyvalue
Sizehow much per chunk
Overlaprepeat at edges
Too much overlapduplicates
Start with300 chars, 15%

Summary

Fixed size chunking is simple, but it cuts words and sentences. Next, recursive splitting, which respects paragraphs.

Key takeaways
  • size
  • overlap
  • Cuts mid-word
Next up: Recursive Splitting — paragraphs first