Walk Through a RAG Pipeline End to End
How a RAG pipeline works from splitting documents to a sourced answer, how the two searches fit together, and how to test it, for your AI interview.
In short
A RAG pipeline has two halves. First, documents are cut into chunks, embedded and stored in a search index. Then, for each question, the system searches that index, keeps the best chunks, and puts them in the prompt so the model answers from them.
12 min readBeginnerVery common · estimateFacts checked 26 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
A RAG pipeline has two halves. First, ahead of time, I cut the documents into chunks, label them, turn each chunk into an embedding, and save them in a search index. Then, for each question, I search that index by exact words and by meaning, merge the results, rerank the best chunks, and put them in the prompt. The model answers from those chunks. I test the search and the answer separately, so I know which half broke.
RAG stands for retrieval-augmented generation. A language model only knows what it learned in training. It has never read your supplier contracts or this month's policy changes, so it either guesses or admits it doesn't know. A RAG pipeline finds the right pieces of your documents for each question and places them in the prompt, so the model answers from text you supplied.
The first half runs ahead of time. Each document is cut into chunks, usually a few hundred tokens each. A token is a small piece of text, often part of a word. Each chunk gets labels, such as the contract name and clause, and an embedding model turns it into a list of numbers that captures its meaning. The chunks, numbers and labels go into a search index, and a keyword index is built over the same chunks. Every later question searches both.
The second half runs for each question. The question becomes the same kind of numbers, and the system searches twice: by meaning, and by exact words using BM25, which is strong on identifiers such as clause 14.2. The two lists merge, a reranker keeps the best candidates, and the top few go into the prompt. The model writes the answer from those chunks.
I test retrieval and the answer separately. Retrieval asks whether the right chunk reached the prompt, which needs questions with known source passages. Answer quality asks whether the model used those chunks correctly. RAG is not always needed, though. Sometimes the simplest solution is the best.
What is a RAG pipeline, in plain words?
A language model answers from what it learned in training. It has never read your contracts, this month's policy changes or your product manuals, so it either guesses or says it doesn't know. A RAG pipeline fixes that. It finds the right pieces of your documents for each question and places them in the prompt, so the model answers from text you supplied.
Picture a new claims handler in her first week. She doesn't memorise the whole policy library. For each customer she pulls the right page, reads it and answers. A RAG pipeline does that job for a model. If a legal team has 40,000 supplier contracts, a question about one liability cap should bring back the few clauses that answer it. A 2020 paper by Patrick Lewis and 11 other authors explored a recipe for retrieval-augmented generation, and the name comes from its title.
Key words
- Chunk
- A piece of a document, cut so that it holds one idea, usually a few hundred tokens.
- Token
- A small piece of text the model reads, often part of a word.
- Embedding
- A list of numbers that stands for the meaning of a piece of text. Similar meanings get similar numbers.
- Prompt
- The text the model reads before it writes, made of your instructions, the question and the chunks.
- Reranker
- A second model that scores the candidate chunks against the question and keeps the best.
How is the knowledge base prepared?
This half runs once for each document, and again whenever a document changes. Microsoft's architecture guide describes it in four steps: chunking, enriching, embedding and persisting.
Indexing: done once, ahead of time
Your documents
40,000 supplier contracts
Cut into chunks
a few hundred tokens each
Add labels
contract name, clause, date
Embedding model
turns each chunk into numbers
Search index
chunks, vectors, words and labels saved
- 1The whole document is cut into pieces that each hold one idea.
- 2Each piece gets a label, such as the contract name and its clause.
- 3The embedding model turns each labelled piece into a list of numbers.
- 4The pieces, their numbers and labels are saved in the search index.
Chunk size is a trade-off. Bigger chunks carry more context, but they blur the one idea a search should match. Smaller chunks match precisely, but they can lose the sentence that explains them. Anthropic's guide says chunks are usually no more than a few hundred tokens.
Labels matter as much as the text. Microsoft's guide describes enrichment as adding metadata fields that the pipeline creates from the content. A contract's client, date and status let the search filter to active contracts before it ranks by meaning.
What happens when someone asks a question?
This half runs on every question. Microsoft's guide describes the orchestrator, the part that picks the search and packages the results. It takes the top results from the search, packages them with the question, and sends that prompt to the language model.
- 1
Embed the question
Turn the question into the same kind of numbers as the chunks, using the same embedding model.
- 2
Search twice
Run a meaning search and an exact-word search (BM25, a classic score for matching exact words). Both run on the same index, and each returns its own ranked list.
- 3
Merge the lists
Combine both lists by rank, since the two scores are on different scales, and remove duplicates, so each chunk appears once.
- 4
Rerank
A reranker scores the merged chunks against the question and keeps the best few.
- 5
Build the prompt
Put the chosen chunks, with their source names, next to the question and the rules.
- 6
Generate with sources
The model writes the answer from those chunks, and the prompt names each chunk's source.
Answering one question
The question
asks about clause 14.2
Search twice
Exact-word search
BM25 finds 14.2 word for word
Meaning search
finds the same idea, new words
Merge the lists
each chunk counted once
Rerank
keeps the best few chunks
Model writes the answer
answers from the chunks it got
- 1The question's words go to the exact-word search.
- 2The question's meaning goes to the meaning search.
- 3Chunks with the exact words come back as a ranked list.
- 4Chunks with a similar meaning come back as another list.
- 5The two lists merge into one, with duplicates removed.
- 6The best chunks go into the prompt, and the model answers from them.
“If your retrieved information is irrelevant, your generation could be grounded but off-topic or incorrect.”
Google Cloud
Worked example, with made-up numbers. A legal team has 40,000 supplier contracts, and a paralegal asks what the liability cap under clause 14.2 is. Meaning search alone can return clauses about liability caps and miss the one that cites 14.2. Anthropic notes that embedding models can miss exact matches, and BM25 is particularly effective for queries with unique identifiers or technical terms.
- Meaning search only
Clause 14.2 is missed
The search finds liability clauses and misses the one with the number, so the bot answers from the wrong clause.
- Meaning plus exact words
The right clause comes first
The exact-word search catches 14.2, the meaning search catches the wording variants, and the reranker puts the clause on top.
Here is the idea in plain Python. Counting shared words is a crude stand-in for BM25, but it shows why exact terms matter: the chunk with clause 14.2 scores highest.
# A tiny stand-in for keyword search: count the words a chunk shares with the question.
CHUNKS = {
"c1": "Supplier liability is capped at 2 million pounds under clause 14.2.",
"c2": "Payment terms are 30 days from the invoice date.",
"c3": "Liability for data breaches is uncapped under clause 9.1.",
}
def words(text):
return {w.strip("?.").lower() for w in text.split()}
def score(question, chunk):
return len(words(question) & words(chunk))
question = "What is the liability cap under clause 14.2?"
ranked = sorted(CHUNKS, key=lambda k: score(question, CHUNKS[k]), reverse=True)
for key in ranked[:2]:
print(key, score(question, CHUNKS[key]), CHUNKS[key])Output
c1 5 Supplier liability is capped at 2 million pounds under clause 14.2. c3 4 Liability for data breaches is uncapped under clause 9.1.
Anthropic's own test is a useful reference for the numbers. It fetched the top 150 candidates and passed the top 20 to the model. Reranking is a common filter. It reduces cost and latency, because the model reads less, although the extra step itself adds a small amount of latency. Your data needs its own numbers, so tune them with a test set.
Where does a RAG pipeline fail, and how do you check it?
Microsoft's guide is clear about the limit of this fixed sequence. It says some scenarios exceed what a fixed pipeline can handle, and standard RAG suits one search against one index.
“Standard RAG works well for queries that map to a single search against a single index.”
Microsoft
Chunks cut mid-clause
The clause and its number land in different chunks, so the model sees half the rule.
Exact words missed
Meaning search alone can miss identifiers such as clause numbers, part codes and names.
Stale index
A policy changed last week, but the index still holds the old chunk.
Right chunk, weak answer
The chunk arrived, yet the model ignored it or filled the gaps from memory.
Measure retrieval and the answer apart. For retrieval, take questions with known source passages and check how often the right chunk lands in the top results. For the answer, give the model the right chunks and check that it answers correctly and cites them.
Tip
Asked how to improve one? Name the half first. Fix retrieval before you touch the prompt, because the model can only work with the chunks it receives.
What does the interviewer want to hear?
A strong answer covers
- Two halves: indexing ahead of time, and answering for each question.
- The steps by name: chunk, label, embed and store; then search, merge, rerank, prompt and generate.
- Why two searches: meaning finds paraphrases, exact words find IDs and names.
- Why rerank: fewer, better chunks give the model less to read and cost less.
- How you measure: retrieval and answers, tested apart, on your own questions.
- Honest limits: each part adds moving pieces, and a small document set may not need RAG.
Common traps
Trap"Just embed the documents and search them."
BetterThat is the start. Add chunking, labels, exact-word search and a reranker, then measure each part.
Trap"The model searches the database for you."
BetterIn a standard pipeline your code runs the search, and the model only reads the chunks it is given. Agentic RAG is different: the model can call search as a tool.
Trap"A bigger context window means no RAG is needed."
BetterA big window helps with a small set. For a big archive, retrieval still picks what goes in.
Can you spot the weak setup?
Spot the weak setup
A contract search team says clause numbers never come back. Which setup is the likely cause?
Quick check
A legal bot cites the wrong contract. What do you check first?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
Learn it in depth
Last updated on 26 November 2026
Building a bot on your own documents?
I design and build AI products and agents, including the search and the answer behind them. Book a short call and tell me what your documents are and what people ask.