TL;DR
Document chunking for RAG is splitting scraped or loaded documents into smaller passages before embedding them, so retrieval returns focused, relevant context instead of whole pages. Good chunking respects structure (headings, paragraphs), sizes chunks to the embedding model, and often overlaps them so meaning is not cut mid-thought.
Why Chunk at All
In a RAG system you embed content, store the vectors, and retrieve the closest matches to a query. If your unit is a whole page, retrieval returns a page when the answer is one paragraph, which dilutes relevance and wastes the model's context window. Chunking breaks documents into passage-sized units so retrieval returns exactly the relevant part. Chunking is the step that makes retrieval precise.
What Good Chunking Considers
- Size. Chunks must fit the embedding model's window and leave room in the LLM's context for several retrieved chunks. A few hundred tokens is a common target.
- Structure boundaries. Split on natural boundaries (headings, paragraphs, list items) rather than a fixed character count, so a chunk is a coherent thought, not a fragment cut mid-sentence.
- Overlap. Overlapping consecutive chunks by a small margin keeps context that straddles a boundary from being lost.
- Metadata. Attaching the source URL, title, and section to each chunk lets the system cite and filter.
# structure-aware chunking beats blind character splitting
chunks = split_by_headings(markdown_doc, target_tokens=400, overlap=50)
for c in chunks:
c.metadata = {"url": doc.url, "section": c.heading}Why Clean Input Matters First
Chunking amplifies whatever it is given. Chunk a page full of nav and boilerplate and you get chunks full of nav and boilerplate, which retrieve for the wrong queries and pollute results. Chunking well starts with clean, structure-preserving markdown, which is why the scraping step and the chunking step are two halves of the same ingestion job.
Key Takeaways
- Chunking splits documents into passages so retrieval returns focused context.
- Size to the embedding model, split on structure boundaries, and overlap slightly.
- Clean, structured input is a prerequisite; chunking amplifies noise if the source is dirty.
How ScrapeGraphAI Handles This
ScrapeGraphAI produces the clean, structure-preserving markdown that chunks well: scrape removes boilerplate and keeps headings and code intact, so your chunker splits coherent content rather than page chrome.