Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 23/ 160
02. RAG

Chunking Strategies

Fixed, recursive, semantic, and structure-aware splits that balance recall and context quality.

IntermediateChunk Size · Overlap3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

Cut long documents into pieces that each hold one complete idea, so the right piece can be found and shown to the model.

Think of it like…

Cutting a textbook into index cards for revision: cards too small (a single sentence) lose the point; cards too big (a whole chapter) are hard to match to a question and crowd your desk. The best cards each hold one complete idea with its heading.

Where you’ve seen it

A policy assistant with 600-token fixed chunks scored recall@5 of 71 percent on 200 test questions because answers often straddled chunk boundaries or lost the section title. Switching to heading-aware splitting (each section split recursively to about 400 tokens, 50-token overlap, with the document title and section path prepended) lifted recall@5 to 86 percent with the same embedding model and index.

Words you’ll hear

Chunk size
Target length per chunk, in tokens (not characters); commonly 200–800.
Overlap
Tokens repeated between adjacent chunks so boundary-spanning sentences survive.
Recursive splitter
Tries large separators first (sections, paragraphs), falling back to smaller ones.
Structure-aware chunking
Respects headings, lists, tables and code blocks instead of cutting blindly.

Visual diagram

Loading interactive diagram

Theory & deep dive

Choosing a splitting strategy

Fixed-size chunking is simple and predictable but ignores meaning, so it cuts mid-sentence and mid-table. Recursive splitting (for example LangChain's RecursiveCharacterTextSplitter) tries separators in order, such as double newline, newline, sentence end, then space, keeping natural units together up to the size limit. Structure-aware chunking uses the document's own markup (Markdown or HTML headings, PDF sections, code functions, Q&A pairs) and is usually the best first choice for manuals, policies and docs. Semantic chunking embeds sentences and starts a new chunk where similarity to the previous sentences drops; it can improve coherence but costs extra embeddings and doesn't always beat structure-aware splitting.

Start around 300 to 500 tokens with 10 to 15 percent overlap and tune on your questions: factual lookups favour smaller chunks, explanations and procedures favour larger ones or parent-child retrieval. Keep tables, lists and code blocks intact; for tables, include the header row with each chunk of rows or convert rows to sentences.

A recursive splitter configuration

RecursiveCharacterTextSplitter.from_tiktoken_encoder(chunk_size=400, chunk_overlap=50, separators=["\n## ", "\n\n", "\n", ". ", " "]) splits on Markdown sections first, then paragraphs, sentences and words, measuring size in tokens.

Key takeaway: Prefer structure-aware or recursive splitting, sized in tokens, tuned on your questions.

Context, metadata and evaluation

A chunk read alone often loses what it's about ("The limit is 5,000" - of what?). Prepend context: document title, section path and date, either inside the embedded text or as metadata shown to the LLM. Related techniques include contextual retrieval (an LLM writes a one-sentence situating blurb per chunk), parent-child retrieval (match small chunks, return their parent section) and sentence-window retrieval. Store source URL, page, section and chunk position so you can cite and so neighbouring chunks can be fetched.

Evaluate chunking empirically. Build 100 to 300 questions with known source passages, vary chunk size, overlap and strategy, and compare recall@k and downstream answer quality, along with index size and cost (smaller chunks mean more vectors). Also read samples of the actual chunks: bad parsing (page headers repeated in every chunk, broken tables, hyphenated words) is a more common cause of poor retrieval than the splitter. Re-chunk when documents or models change, and version the chunking config.

What gets embedded

"Employee Handbook › Leave Policy › Sick leave (updated 2026-04-01): Employees may take up to 12 paid sick days per year..." Including the title path means the chunk still matches a question about sick leave even if the paragraph never repeats the phrase.

Key takeaway: Carry context with each chunk, inspect real chunks, and tune against recall@k.

How it works, step by step

Step 1 of 4
Ingestion

1. Parse cleanly first

Extract text with structure (headings, tables) and remove repeated headers, footers and boilerplate.

no "Page 3 of 40" in every chunk
Key numbers at this step
Garbage in
garbage retrieved

When to use it & trade-offs

What it is

Chunking splits documents into pieces small enough to embed and retrieve, yet large enough to make sense alone. Strategies: fixed-size (every N tokens, with overlap), recursive (split on paragraph, then sentence, then word boundaries until pieces fit), structure-aware (split by headings, sections, table rows or code functions) and semantic (cut where meaning shifts, found by embedding similarity). Chunk size is a trade-off: small chunks retrieve precisely but lose context; large chunks keep context but dilute the match and waste prompt space.

Why it matters: Chunking is the most underrated lever in RAG. A good embedding model and LLM can't recover an answer that was split across two chunks, or buried in a 2,000-token block about five topics. Teams often gain more recall from fixing chunking than from changing models, and chunking decisions are expensive to revisit because they require re-embedding the corpus.

When to reach for it

  • RAG over handbooks, tickets, wikis and contracts
  • Parent-child or contextual retrieval pipelines
  • Citation-ready retrieval with page and section metadata

When to avoid or be careful

  • Splitting by characters with no regard to tokens or structure
  • Chunks with no title or section context
  • Changing chunking without re-evaluating recall

Chunking strategies

Fixed-size with overlap
Best for: Quick baselines and unstructured text

Strengths: Simple, predictable sizes.

Watch out: Cuts mid-thought; ignores structure.

Recursive / structure-aware
Best for: Docs, policies, manuals, code

Strengths: Keeps coherent units; strong default.

Watch out: Needs reliable structure in parsed text.

Semantic chunking
Best for: Long, unstructured prose with topic shifts

Strengths: Chunks follow meaning.

Watch out: Extra embedding cost; uneven chunk sizes.

Interview signals & numbers to know

When this topic comes up

  • “The answer is in the doc but retrieval misses it”
  • “What chunk size should we use?”
  • “Tables come back garbled”
  • Overlap and boundaries
  • Parent-child retrieval

Benchmarks & rules of thumb

300 – 500 tokens -
common starting chunk size
10 – 15% -
typical overlap
71% → 86% -
recall@5 gain in the example

Common mistakes & follow-ups

Common interview mistakes

  • Setting chunk size in characters and exceeding the embedding model's token limit on code or non-English text.
  • Splitting a table so that rows lose their header and become meaningless.
  • Tuning chunk size on three hand-picked questions instead of a labelled set.

Likely interviewer follow-ups

  • How would you choose a chunk size?
  • Why add overlap, and what does it cost?
  • How do you chunk tables or code?
Interview tip: Say you'd tune chunking against recall@k on a labelled question set. Measured chunking choices stand out from folklore numbers.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Chunking Strategies with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Cosine Similarity
Next topic
Vector Databases