Skip to content

How to Choose Chunk Size for RAG

Summary

The quality of RAG heavily depends on how documents are sliced. Too small fragments lose context, while too large ones blur the search. Here's how to choose the right chunk size for your data.

Why Chunk Size Matters

The found fragment is what the model sees before responding. If it's too short, the model answers based on a snippet. If too long, it mixes several topics, leading to irrelevant search results.

flowchart LR
    A["Small chunk"] -->|"loses context"| X["Incomplete response"]
    B["Large chunk"] -->|"multiple topics"| Y["Inaccurate search"]

Factors Influencing the Choice

1. Document Type

Type Recommended Size
FAQ, short answers 100–300 tokens
Articles, documentation 400–800 tokens
Books, long reports 800–1500 tokens

2. Embedding Model

The embedding model has a maximum input text length. The chunk should not exceed this limit.

3. Nature of Questions

  • For narrow questions ("what is the price?") — smaller chunks;
  • For broad questions ("what is the essence of the approach?") — larger chunks.

Chunk Overlap: How Much Overlap

Chunk overlap refers to the number of characters overlapping between adjacent fragments. Typically, 10–20% of the chunk size:

from langchain_text_splitters import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(
    chunk_size=800,
    chunk_overlap=100,  # 12.5% of the size
)

Overlap is needed to ensure that a thought, split by a boundary, is not lost.

How to Test

Don't guess, measure. Collect 20–30 real questions and check how well the found fragments answer them.

# Simplified test loop
for question in test_questions:
    docs = retriever.invoke(question)
    # Evaluate: does the answer contain the found fragments?
    print(question, "->", len(docs), "fragments")

Metrics are Recall and precision of the search. Increase the size until recall stops improving.

Step-by-Step Plan

  1. Start with 500 tokens and overlap 50–100.
  2. Gather test questions.
  3. Run the search and evaluate recall.
  4. Try 300 and 800 — compare.
  5. Stop at the best balance of quality and cost.

Common Mistakes

  • Ignoring document structure — slicing by characters instead of headings and paragraphs.
  • Too large overlap — increases duplication and storage costs.
  • One size fits all — different types require different slicing.

See Also