RAG & LLMs / 2. TEXT SPLITTING
Stage 2: Text Splitting
Chunking strategy directly impacts retrieval quality
EXPLANATION
Embedding models and LLMs have context limits. You must split documents into chunks. But naive splitting breaks semantic meaning — a sentence cut in half loses context. LangChain's RecursiveCharacterTextSplitter is the gold standard. It tries to split on paragraph breaks → sentence ends → words, in that order — preserving natural boundaries. Key params: • chunk_size → max chars per chunk (typically 500–1000) • chunk_overlap → shared chars between adjacent chunks (prevents losing context at split boundaries)
DATA FLOW
FULL DOCUMENT (10,000 chars)
────────────────────────────────────────────────────────────
[ chunk 1 (800) ][ chunk 2 (800) ]
[overlap: 150]
[ chunk 3 (800) ]
Overlap ensures no sentence is cut off without context.
RecursiveCharacterTextSplitter tries splits in this order:
\n\n → \n → ". " → " " → ""CODE