Calculate the number of chunks produced when splitting a document for RAG retrieval.
RAG chunking splits a document into overlapping windows so that context near chunk boundaries isn't lost: num_chunks = ceil(document_length / (chunk_size − overlap_tokens)), where overlap_tokens = chunk_size × overlap%. Larger chunks capture more context per retrieval but reduce precision (more irrelevant text mixed in) and risk exceeding the embedding model's context window; smaller chunks improve retrieval precision but multiply storage and embedding cost, and overlap adds redundancy that trades extra storage for continuity across chunk boundaries.
num_chunks = ceil(doc_length / (chunk_size - chunk_size * overlap_percent / 100))
300-800 tokens is a common starting range for general text RAG — small enough for precise retrieval, large enough to retain coherent context; the right value depends on your content type (dense technical text often benefits from smaller chunks, narrative text from larger ones).
Without overlap, a sentence or idea split exactly at a chunk boundary can lose context in both resulting chunks; overlap ensures each chunk contains a 'tail' of the preceding chunk's content so boundary-spanning information isn't lost.
The embedding API will typically truncate the input to fit its context window, silently discarding the excess text — always keep chunk size safely below the embedding model's maximum context window.
Higher overlap percentages increase the number of chunks needed to cover a document (since each chunk contributes less new content), which directly increases both embedding cost and vector storage — see the Redundancy result for exactly how much duplicate content this adds.