RAG architecture and chunking

  • Intermediate
  • 30 min
  • Lesson 1 of 6 in Retrieval-augmented generation
  • Last verified

Open slides(25 slides)

Before you start

You will be able to

  • Describe the two phases of a RAG pipeline and say what happens at each stage.
  • Choose a chunking strategy (size, overlap, structure-awareness) for a corpus and justify it with trade-offs.
  • Explain which metadata a chunk needs and why permissions are enforced inside retrieval, not after it.
  • Describe how to pick a chunk size from evidence on a labelled question set instead of by habit.

Kavya has cut the refund policy into small pieces so each one fits the search. You open one at random. It reads: “…and so it is refunded within five days, for all items.”

Which refund? Which items? Kavya looks at it, files it, and moves on.

Later a customer asks about refund times. Will anyone ever find this piece, and if they do, will it help?

Concept

Retrieval-augmented generation (RAG)RAGShort for retrieval-augmented generation: putting retrieved content in the prompt so the answer is grounded in it.Open the glossary entry retrieves relevant pieces of text from an external store and puts them into the prompt, so the model answers from real, current or private documents instead of from its training data alone. The model’s weights do not change.

The pieces are called chunksChunkA piece of a document that is embedded and retrieved as one unit. Its size and boundaries strongly affect retrieval quality.Open the glossary entry. How you cut documents into chunks decides what the retriever can find later.

Why It Matters

A model has a training cutoff, no access to your private documents, and no way to cite where a claim came from. RAG addresses all three. It reduces hallucination, because the model has concrete text to answer from, but it does not remove it (see RAG evaluation and hallucination).

Chunking matters because retrieval can only return what ingestion produced. If the strip that holds the answer was cut badly, no later stage can reliably recover it. In practice, many “the model got it wrong” reports trace back to this step.

Mental Model

RAG is an open-book exam. You do not expect the model to have memorised everything. Right before it answers, you hand it the few pages that matter.

Chunking is how you decide what a “page” is. Too small and a page says “and so it departs” with no train named. Too big and the page holds a hundred topics, and its single summary vector matches none of them well.

Here is the same short passage cut three ways. The passage has six sentences, S1 to S6, and the third cut uses one sentence of overlap:

CutChunksWhat goes wrong or right
Too small: one sentence eachS1 · S2 · S3 · S4 · S5 · S6S4 says “it departs at 9” and no chunk names the train
Too big: everything togetherS1–S6One vector blurs every topic, and a prompt pays for all six sentences
Moderate with overlapS1–S3 · S3–S5 · S5–S6A fact that straddles S3 or S5 survives whole in at least one chunk

The pipeline in two phases

RAG has an ingestIngestionThe offline part of a RAG system: parse, chunk, embed and index documents. Safe ingestion is idempotent, incremental and records model and chunker versions.Open the glossary entry phase that runs ahead of time and a query phase that runs on every question.

Diagram: Ingest turns documents into embedded chunks in a vector store.

Query time

Ingest

Raw documents

Clean + parse

Chunk
(size + overlap)

Embed chunks

Vector store

User question

Embed query

Retrieve top-k
from vector store

Optional rerank

Construct prompt:
question + retrieved chunks

LLM generates answer

Diagram summary: Ingest turns documents into embedded chunks in a vector store. At query time the question is embedded, the top matching chunks are fetched (and optionally reranked), and they are placed in the prompt with the question for the model to answer.

Ingest, step by step:

  1. Parse and clean. Turn PDFs, HTML and wikis into clean text. Garbage here is garbage everywhere.
  2. Chunk. Split the text into pieces (the rest of this lesson).
  3. Embed. Turn each chunk into a vector (see Embeddings and similarity search).
  4. Store. Save vectors plus metadata in a vector database.

Query, step by step: embed the question with the same model, fetch the top-k nearest chunks, optionally rerank them, build a prompt from the question and the chunks, and generate.

Because ingest is separate from the model, updating knowledge means re-ingesting the changed document. No retraining is involved. That is the main reason RAG suits facts that change.

Chunking trade-offs

You cannot embed a 50-page document as one vector. One vector is one summary of everything in it, so it dilutes relevance and loses the specific detail a question asks about.

ChoiceBenefitCost
Small chunksPrecise matchesEach chunk may lose the context that makes it meaningful
Large chunksMore context per hitThe embedding blurs several topics; more irrelevant tokens in the prompt
Overlap between neighboursA fact at a boundary survives in at least one chunkMore chunks to store and embed; near-duplicate hits

Common splitting strategies:

  • Fixed size (by tokens or characters): simplest, ignores meaning.
  • Sentence or paragraph based: respects natural units.
  • Semantic: split where the topic changes. Better boundaries, but costs extra computation at ingest.
  • Structure-aware: follow headings, sections, lists and tables. Usually the best default for documents that have structure.

There is no universal best size. Illustrative starting points that teams try are a few hundred tokens with 10 to 20 percent overlap. Treat them as a first experiment, not an answer (the last section shows how to decide).

Cutting a passage into chunks

Text summary

This tool cuts one short, made-up passage of 83 words (a lending guide for an imaginary library) into chunks of words. You set the chunk size and the overlap, and overlap is always smaller than the size. The question is “How many times can a book be renewed?”, and the answer is one sentence: “Books can be renewed twice, and each renewal adds fourteen days to the loan.” The tool lists the chunks and marks each one that holds the whole answer sentence, or only part of it. At a chunk size of 20 words with no overlap, the cut falls inside the answer sentence, so no chunk holds all of it and the tool warns you. With an overlap of 8 words, the second chunk holds the whole sentence. More overlap means more words stored, because the repeated words are kept in both chunks. Chunks are counted in words here; real systems usually count tokens or characters.

The interactive version needs JavaScript. The text summary above says what it shows.

Chunk by structure, not by character count

Splitting every N characters can cut a sentence, a clause or a table in half. Respect the document’s own boundaries instead.

Headings and sections. Split on headings, then on paragraphs, and only fall back to a size limit inside a long section. Store the heading path (for example “Refunds > Digital goods”) with each chunk.

Tables are the hard case. If you split a table row by row, a row such as “Gold | 14 days | 5%” has lost its column names. A reader, and an embedding, cannot tell what 14 or 5% mean. Options:

  • Keep a small table as one chunk.
  • For a large table, repeat the caption and header row in every chunk.
  • Or turn each row into a sentence (“Gold plan: refund window 14 days, fee 5%”) before embedding.

What the embedder sees depends on how you chunk a row. The second and third options keep the meaning:

StrategyText that gets embedded
Row split naivelyGold | 14 days | 5%
Caption and header repeatedRefund plans. Plan | Window | Fee. Gold | 14 days | 5%
Row turned into a sentenceGold plan: refund window 14 days, fee 5%.

Parsing quality comes first: a multi-column PDF read in the wrong order produces nonsense no chunker can fix. See Document AI for layout-aware parsing.

Give chunks their context back

An isolated chunk often says “it”, “the company” or “this quarter” without naming what. Three common fixes:

  • Cheap prefix. Before embedding, prepend the document title and section heading to each chunk. It costs nothing at query time and often fixes “which document is this about?” confusion.
  • LLM-written context note. A model reads the whole document and writes a short note situating each chunk in it (for example, which filing and which quarter a revenue sentence belongs to). Anthropic calls this contextual retrievalContextual retrievalA chunking technique where a model writes a short note placing each chunk in its document, and the note is added to the chunk before embedding and BM25 indexing. It costs one extra model call per chunk at ingest.Open the glossary entry: the note is prepended to the chunk and used for both the embeddings and the BM25 index (source above; read it for their setup and results). The cost is an extra model call per chunk at ingest.
  • Small-to-big (parent-child)Parent-child retrievalMatching on small chunks and then returning their larger parent section. It gives precise matching with more context, at the cost of bigger prompts.Open the glossary entry. Embed small chunks for precise matching, then hand the model the larger parent section. You pay with larger prompts and need to de-duplicate parents that several children point to.
Diagram: A bare chunk such as “It rose 3% in the second quarter” does not say what rose.

Raw chunk:
'It rose 3% in the second quarter.'

Add document title
and section heading

'Acme annual report, Revenue:
It rose 3% in the second quarter.'

Embed this version

Index

Small-to-big: match on the small chunk

Hand the larger parent
section to the model

Diagram summary: A bare chunk such as “It rose 3% in the second quarter” does not say what rose. Prefixing the document title and section heading before embedding restores that. With small-to-big, the small chunk is used to find the match, and its larger parent section is what the model reads. The report title is an invented example.

Metadata and permissions

Each chunk should carry fields you can filter on, saved at ingest time:

  • source (document id or URL), so you can cite it;
  • section or heading path;
  • date or version, so you can prefer current text and drop superseded text;
  • language, document type;
  • access control (tenant, group, or user permissions).

Access control is the field people forget, and it is the one with legal consequences. If a user may not read a document, its chunks must never reach the prompt for that user.

Enforce this inside the retrieval query (a filter or a per-tenant namespace). Do not retrieve first and filter afterwards:

  • Post-filteringPost-filteringApplying a metadata filter after the nearest-neighbour search. Filtered-out candidates leave fewer than k results. The alternative is filtering inside the search; see metadata filter.Open the glossary entry a top-k can leave you with zero results, because the best matches were the ones removed.
  • Scores, summaries, logs and caches built from unfiltered results can leak the existence or content of restricted documents.
  • Asking the model to “not mention” restricted text is not a control. The text is already in the prompt.

Also scope any cache by identity, so one user’s cached answer is never served to another.

Diagram: Post-filtering fetches the top results from everything, then removes forbidden ones, which can leave nothing and still lets restricted text...

Filter inside the retrieval query

Apply the user's tenant and group filter

Retrieve top-k from allowed chunks only

Full k results, nothing restricted ever fetched

Post-filtering: unsafe and lossy

Retrieve top-k from everything

Remove chunks the user cannot read

Few or zero results left

Diagram summary: Post-filtering fetches the top results from everything, then removes forbidden ones, which can leave nothing and still lets restricted text touch scores, logs and caches. Filtering inside the query means only chunks the user may read are ever candidates.

Choosing chunk size with evidence

Folklore (“512 is standard”) is how teams end up with a bad default for years. The corpus and the questions decide.

  1. Build a small labelled set. Collect realistic questions and mark which passage answers each. Start with a few dozen (an illustrative size; see RAG evaluation in depth for building a proper one).
  2. Change one thing. Index the corpus at two or three chunk sizes, with the same embedder and the same overlap.
  3. Measure retrieval. For each question, check whether a chunk containing the answer appears in the top-k. The fraction of questions where it does is recall@k. Also look at where in the list it appears.
  4. Check the prompt cost. Bigger chunks mean more tokens per query. A small recall gain may not justify doubling the context.
  5. Check the end-to-end answer on a handful of examples. Retrieval can improve while the answer worsens if large chunks bring in noise.
Diagram: Chunk size is chosen by experiment: the same labelled questions run against indexes built at a few sizes, with recall, prompt cost and the...

Labelled questions
with answer passages

Index at 2 or 3 chunk sizes
same embedder, same overlap

Measure recall@k
and answer rank

Check tokens per query

Spot-check final answers

Pick the smallest size that
meets the recall target

Diagram summary: Chunk size is chosen by experiment: the same labelled questions run against indexes built at a few sizes, with recall, prompt cost and the end-to-end answers compared before a size is picked.

Different content often wants different settings: short FAQ entries need almost no splitting, while long contracts need clause-aware chunks with their section titles attached.

Real-World Application

This is the architecture behind internal-document Q&A and support-over-knowledge-base products. In production, the ingest side is a pipeline with its own schedule, monitoring and re-index plan, because changing the chunker or embedder means re-processing the whole corpus.

Interview Perspective

Provenance: common industry pattern; no company-specific claim.

Expect “walk me through a RAG pipeline”, “how do you choose chunk size”, “how do you handle tables”, and “what happens if retrieval returns nothing relevant”. A strong answer names the trade-off, then says how you would measure it. A common mistake is conflating RAG with fine-tuning, or answering chunk size with a number and no method.

Practice Questions

Provenance: generated practice.

  • Basic: What are the two phases of a RAG pipeline, and which one runs per question?
  • Intermediate: Why does chunk overlap help, and what does it cost?
  • Advanced: Your corpus is long legal contracts plus short FAQ entries. Would you use one chunking setup for both?
  • Product company: Retrieval returns three moderately relevant chunks and none answers the question. What should the system do?

Activity

Do, notice, apply.

  1. Do. Take a three-paragraph document you have (a policy, a README, a manual page). Cut it two ways: into single sentences, and into one chunk per paragraph. Write three questions a reader might ask about it.
  2. Notice. For each question, which cut keeps the information needed to answer it? Which cut makes a chunk that could not be understood alone (a “…and so it departs” strip)?
  3. Apply. Now take a document that contains a table. Decide how you would chunk it so a row still makes sense, and write the chunk text for one row.

Homework

On paper, design the ingestion pipeline for a company’s internal HR policy documents. State the inputs and parsing choices, the chunking approach and why, the metadata fields (including who may read what), and how you would test your chunk size.

Quick Revision

  • RAG retrieves chunks and puts them in the prompt. Weights do not change.
  • Ingest runs ahead of time. Query runs on every question.
  • Small chunks are precise but context-poor. Large chunks are context-rich but blurry. Overlap protects boundaries at some cost.
  • Split by structure, keep table headers with rows, and restore context with a prefix or a parent.
  • Store source, section, date and permission metadata. Enforce permissions in the query.
  • Choose chunk size by measuring recall@k on a labelled set.

Check yourself

  • Checkpoint (answer aloud, no notes): Design chunking and retrieval for 10,000 PDFs with tables and headings. Justify chunk size, overlap and metadata, and say how you would choose empirically.
  • Re-test (nearby variant): Answer quality dropped after raising chunk size from 256 to 1024. Give two mechanisms and say how you would tell whether retrieval or generation is at fault.
  • Misconception to avoid: picking a chunk size by folklore, embedding a whole document as one vector, or ignoring tables.

Resources & Further Reading

Checked sources to go deeper. The essential ones come first in each group.

Official documentation

  • Retrieve & Re-Rank (opens in a new tab)

    Sentence Transformers2026Documentation

    Shows the retrieve-then-rerank pipeline with a bi-encoder and a cross-encoder. It previews the query-side stages that the next two lessons cover.

Research papers

Hands-on

More for this module: Retrieval-augmented generation resources

Practice

Test what you just read. Quizzes and cards are grouped by module: Retrieval-augmented generation.

Check yourself9 questions on what this lesson set out to teach

Checking answers needs JavaScript. You can still read every question and option below.

0 of 9 answered

Keyboard: arrow keys move between options. Space or Enter selects the focused option.

ObjectiveDescribe the two phases of a RAG pipeline and say what happens at each stage.

  1. Question 1 of 9A support team updates its refund policy document today. In a RAG system, how do answers come to reflect the change?

    Choose one answer.

  2. Question 2 of 9Which stages belong to the query phase of a RAG pipeline, as opposed to the ingest phase?

    Choose all that apply.

ObjectiveChoose a chunking strategy (size, overlap, structure-awareness) for a corpus and justify it with trade-offs.

  1. Question 3 of 9Answer quality got worse after a team raised its chunk size from 256 to 1024 tokens. Which two mechanisms could explain it?

    Choose all that apply.

  2. Question 4 of 9A pricing table is split into one chunk per row. A user asks 'What is the refund window on the Gold plan?' and retrieval keeps returning the wrong rows. What is the most likely cause and fix?

    Choose one answer.

  3. Question 5 of 9Kavya's policy piece reads '...and so it is refunded within five days, for all items.' It never names the policy or the product, so a search for 'refund time for shoes' misses it. What change at ingest helps most?

    Choose one answer.

ObjectiveExplain which metadata a chunk needs and why permissions are enforced inside retrieval, not after it.

  1. Question 6 of 9A multi-tenant assistant retrieves the top 10 chunks for a query, then removes chunks the user may not read. Users sometimes get 'no answer' even though a permitted document answers the question. Why, and what is the fix?

    Choose one answer.

  2. Question 7 of 9Users complain the assistant quotes a superseded 2023 policy even though the 2025 version is indexed. Which chunk metadata would let you fix this at retrieval time?

    Choose one answer.

ObjectiveDescribe how to pick a chunk size from evidence on a labelled question set instead of by habit.

  1. Question 8 of 9Your team must pick between chunk sizes 200, 400 and 800 tokens for a new corpus. Which approach is most defensible?

    Choose one answer.

  2. Question 9 of 9In a chunk-size experiment, recall@5 rose from 0.70 to 0.78 when chunks doubled, but each prompt now carries twice as many tokens. What should you do before adopting the larger size?

    Choose one answer.

Build with this lesson