AI Hints
Home›RAG Tutorial›Smarter Splitting
RAG · Lesson 21 of 120

Choosing a Chunk Size

Not too big, not too small.

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

There's no perfect chunk size. Small chunks match precisely, but may lack context. Big chunks carry more context, but blur meaning, and fill the prompt. Let's see how size changes our index.

Choosing a Chunk Size
From the video: Choosing a Chunk Size
  • Small: precise
  • Big: more context
  • Find the balance

Trying three sizes

We load all fourteen pages, and split them with three different chunk sizes. For each size, we print how many chunks we get, and their average length. Run it. Smaller sizes give many more, shorter chunks.

chunk_sizes.pypython
from pathlib import Path
from langchain_text_splitters import RecursiveCharacterTextSplitter

texts = [p.read_text() for p in sorted(Path("data/rag/northwind").glob("*.md"))]

for size in [100, 300, 1000]:
    splitter = RecursiveCharacterTextSplitter(chunk_size=size, chunk_overlap=0)
    chunks = [c for t in texts for c in splitter.split_text(t)]
    print(f"size {size:4}: {len(chunks):3} chunks, average {sum(map(len, chunks)) // len(chunks)} characters")
Output
size  100: 111 chunks, average 62 characters
size  300:  44 chunks, average 158 characters
size 1000:  14 chunks, average 500 characters

Rules of thumb

For short help pages like ours, two hundred to five hundred characters works well. For long reports, try larger chunks. The real answer comes from testing, which we'll do in the evaluation section, by measuring which size finds the right answers most often.

keyvalue
Help pages200–500 chars
Long reportslarger
Tableskeep whole
Decide bymeasuring

Summary

Chunk size is a trade off between precision and context. Start around three hundred characters, then measure. Next, metadata on chunks.

Key takeaways
  • Small: precise
  • Big: context
  • Measure to decide
Next up: Metadata on Chunks — where it came from