Chunking Strategies
Why Chunking Matters
Chunking is the most impactful decision in RAG quality. Chunks that are too small lose context; chunks that are too large dilute relevance and waste context window space. Rule of thumb: chunks should be semantically self-contained — a reader should understand the chunk without needing surrounding context.
Fixed-Size Chunking
Split every N characters or tokens, with optional overlap. Pros: Simple, predictable, fast. Cons: Splits sentences and paragraphs mid-thought. Chunk size guidelines: • 128–256 tokens: High precision retrieval, less context per chunk • 512–1024 tokens: Better context, slightly less precise retrieval • Use 10–20% overlap between chunks to avoid losing information at boundaries
Recursive Character Splitting
Tries to split on natural boundaries in priority order: paragraphs → sentences → words → characters. The most practical approach for general documents.
Semantic Chunking
Embeds sentences and groups them by semantic similarity. Creates chunks where each chunk covers one topic. Higher quality but slower and more complex.
Chunking Methods Compared
For PDFs: use unstructured.io to extract text preserving structure, then apply RecursiveCharacterTextSplitter. For code: use language-aware splitting (split on functions/classes).
Finished reading? Mark it complete to earn your XP.