Recursive Character Splitting
You'll learn how to chunk text intelligently by trying multiple separators in a prioritized order, preserving natural document structure.
When you're building a RAG system, you need to split documents into chunks small enough to fit in your context window. But not all splits are equal. Recursive character splitting is a smarter approach than fixed-size chunking because it respects the natural boundaries of text.
The Core Idea
Instead of blindly cutting text every N characters, recursive character splitting tries to split at meaningful boundaries using a hierarchy of separators:
- First, try paragraphs (split on
\n\n) - If chunks are still too big, try sentences (split on
.or!or?) - If still too large, fall back to individual characters
Think of it like breaking a chocolate bar: you first try to snap it along the pre-scored lines (paragraphs), then along smaller divisions (sentences), and only break individual pieces (characters) as a last resort.
How It Works
The algorithm checks each chunk size after splitting. If a chunk exceeds your target size, it applies the next separator in the list recursively. This preserves document structure while ensuring chunks stay within your size limits.
For example, a long paragraph gets split into sentences first, keeping related ideas together. Only if a single sentence is too long does it split mid-sentence at the character level.
This approach maintains semantic coherence—your chunks are more likely to contain complete thoughts, which leads to better retrieval quality and more useful context for your LLM.
Key Takeaway: Recursive character splitting chunks text by respecting natural boundaries (paragraphs → sentences → characters), preserving semantic meaning better than fixed-size splitting.