Reference · How-to · ~8 min
How to chunk documents for RAG
Chunking makes or breaks RAG. Goal: each chunk is **small enough to retrieve** but **big enough to answer**.
Chunking makes or breaks RAG. Goal: each chunk is **small enough to retrieve** but **big enough to answer**.
Steps
1. **Clean text** — remove headers/footers, fix broken PDF lines
2. **Pick chunk size** — start ~300–800 tokens (~1–3 paragraphs)
3. **Add overlap** — 10–20% overlap so sentences split across chunks still work
4. **Keep metadata** — source file, page, section title on each chunk
5. **Embed + store** — vector DB or in-memory for demos
6. **Test 10 real questions** — if wrong passages retrieve, adjust size/overlap
Starting defaults
| Doc type | Chunk size | Overlap |
|----------|------------|---------|
| FAQ | whole Q+A | n/a |
| Manual | 500 tokens | 50 tokens |
| Code | function or class | small |
| Legal | paragraph + heading | 15% |
Copy-paste pseudo-code
def chunk(text, size=500, overlap=50):
words = text.split()
chunks = []
i = 0
while i < len(words):
chunk = " ".join(words[i : i + size])
chunks.append(chunk)
i += size - overlap
return chunks