GO TO A SECTION

↑ ↓ to navigate · Enter to openExplore all sections →
FDEInterviews logo
← 🤖 Retrieval & Agents
Foundational

Retrieval-Augmented Generation (RAG)

RAG grounds a language model in your own data by retrieving relevant passages at query time and putting them in the prompt, so the model answers from real sources instead of memory. It is the default pattern for almost every enterprise FDE deployment, which is why nearly every loop tests it.

WATCH THE EXPLAINER3:21
Transcript

Ask a language model about your company's refund window and you'll get an answer. Maybe thirty days, since thirty days is common. The model has never read your policy. It answers from what it absorbed during training, which is frozen, undated, and impossible to cite, and it sounds exactly as sure when it's guessing.

Retrieval-augmented generation fixes that. At the moment you ask, it gives the model fresh, specific context: you keep your knowledge in an external store, find the passages that match the question, and hand those passages to the model as part of the prompt. The answer is constrained by what you retrieved.

There are two paths through the system, and the first one runs only once. Your documents are split into chunks, passages of a few hundred tokens each. Every chunk is embedded, turned into a vector. Then it goes into a vector store.

The second path runs on every question. The question is embedded with the same model that indexed the documents, and the store hands back the top-k chunks, the k passages whose vectors sit nearest the question's. Usually three to ten.

Those chunks go into the prompt, next to the question, along with one instruction that matters more than it looks: answer using only the context, and if the answer isn't there, say you don't know. That refusal is most of your hallucination defense.

Back to refunds. Say the policy paragraph reads: enterprise plan refunds are accepted within 45 days of invoice. The answer now says 45 days and cites the paragraph it came from. Without retrieval, the model's thirty was a guess with the confidence of a fact.

Now draw a line through the middle of the pipeline. Everything to the left of the prompt is retrieval, which is an information-retrieval problem, and everything to the right is generation, a language problem. Any bug lives on exactly one side. Keeping the two halves apart in your head is the most useful habit you can have when you build or debug RAG.

So measure them separately. Retrieval gets recall@k: when the passage a question needs is in the corpus, how often does it show up in the k you retrieved? Generation gets faithfulness, meaning every claim in the answer has to follow from the context. If recall is low, no amount of prompt engineering will save you. Fix retrieval first.

Which leaves the question interviewers love: fine-tune, RAG, or prompt? Reach for RAG when the knowledge changes often, has to be cited, or is too big to bake into the weights. Reach for fine-tuning when what you need to change is the model's behavior or format rather than its facts. And if the corpus is small and stable, skip the machinery and put the whole thing in the prompt. The first two also compose: a tuned model reading retrieved context is a common endgame.

One caution. Grounding cuts hallucination down, but it can't take it to zero, because retrieval can miss and the model can ignore what it was handed. The full explainer, with the code and the metrics, is on fdeinterviews.com.

TL;DR: Retrieval-Augmented Generation answers a question by first retrieving the most relevant chunks of your data and then generating an answer from them, grounded in that retrieved context. It beats fine-tuning for knowledge that changes or must be cited, and it fails in two distinct places, retrieval and generation, which you must evaluate separately.

The idea in one picture

A raw language model answers from what it absorbed during training: frozen, undated, and impossible to cite. RAG fixes that by giving the model fresh, specific context at the moment you ask. You keep your knowledge in an external store, find the passages that match the question, and hand those passages to the model as part of the prompt. The model then writes an answer constrained by what you retrieved.

rendering diagram…

In the flowchart the dotted arrow is the part that runs once, and the solid path is what runs on every question. Everything to the left of "Build prompt" is retrieval; everything to the right is generation, and any bug lives on exactly one side.

Two terms in the boxes: a chunk is a passage of a few hundred tokens that a document was split into when it was indexed, and top-k means the k chunks whose vectors sit nearest the question's, typically three to ten.

The name comes from Lewis et al. (2020, 'Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks'), who paired a pre-trained generator, the parametric memory, with a dense vector index of Wikipedia as non-parametric memory, and found the combination produced more specific and factual language than the generator alone, with a provenance trail the parametric model could not give. Every production RAG system is that split with your documents in place of Wikipedia.

The left half (embed, retrieve) is an information-retrieval problem. The right half (prompt, generate) is a language problem. Holding those two halves apart is the single most important habit when you build or debug RAG.

RAG PIPELINE (press run)
“what is our enterprise refund window?”
embed queryretrieve + rankbuild promptgenerate
Enterprise refund window is 30 days
Enterprise SLA and uptime terms
Pricing tiers and seat limits
Onboarding checklist for admins
Office locations and hours
answer appears here, grounded in the retrieved chunks
A query is embedded, the closest chunks are retrieved and ranked, the top few are stuffed into the prompt, and the model answers grounded in them. Retrieval quality is the ceiling: the answer can only be as good as what it retrieves.

A worked example

Suppose a customer asks "what is our refund window for enterprise plans?" A frozen model might guess "30 days" because that is common. RAG instead retrieves the actual policy paragraph and answers from it. Suppose, hypothetically, the retrieved chunk reads "Enterprise plan refunds are accepted within 45 days of invoice." The answer now says 45 days and cites that paragraph. Without retrieval the model's 30 was a guess with the confidence of a fact.

python
def answer(question, store, llm, k=5):
    q_vec = embed(question)                 # same model used to index
    chunks = store.search(q_vec, k=k)       # nearest neighbors by cosine
    context = "\n\n".join(c.text for c in chunks)
    prompt = (
        "Answer using ONLY the context. If it is not there, say you do not know.\n\n"
        f"Context:\n{context}\n\nQuestion: {question}"
    )
    return llm.generate(prompt), chunks      # return sources for citation

Two design choices already matter here: the instruction to refuse when the context lacks the answer (this is most of your hallucination defense), and returning the chunks so the answer can cite them.

Why interviewers probe this

The answer that loses is "with RAG the model can't hallucinate any more." Retrieval can miss, the model can ignore what it was handed, and a candidate who cannot say which of those happened cannot debug the system they just drew. RAG is the modal enterprise deployment, so the loop wants to see that you understand the trade-offs, not just the diagram. The classic question is "fine-tune, RAG, or prompt?" The honest answer: reach for RAG when the knowledge changes often, must be cited, or is too large to bake into weights; reach for fine-tuning when you need to change the model's behavior or format rather than its facts. They are complementary, not rivals.

The second probe is always evaluation. A strong candidate says "I measure retrieval and generation separately": retrieval with recall@k and precision@k against a golden set (recall@k: of the questions whose needed passage exists in the corpus, how often it appears among the k retrieved; precision@k: of the k retrieved, how many are relevant), generation with faithfulness (does every claim in the answer follow from the context) and answer relevance (does it answer the question that was asked). If recall@k is low, no prompt engineering will save you, so you fix retrieval first.

The decision the loop is really asking about:

The needReach forWhy
Facts that change, must be cited, or are too large for weightsRAGThe index updates in minutes; retrieved chunks are the citations
A small, stable corpus with no per-user accessPrompt stuffingZero infrastructure; the whole thing fits in context
A different behavior, tone or output formatFine-tuningTraining teaches how to answer, not what is true today
Fresh facts in a particular voiceRAG with fine-tuning on topThey compose; a tuned model reading retrieved context is a common endgame

Common misconceptions

  • "RAG eliminates hallucination." It reduces it by grounding, but the model can still ignore or misread the context. You need the refuse-when-unsupported instruction and a faithfulness metric.
  • "Just stuff more documents in the prompt." Irrelevant context degrades the answer and burns tokens. Precision matters as much as recall; this is why reranking exists.
  • "Bigger chunks are better." Chunking is a real tuning decision. Chunks too large dilute relevance, too small lose the context a passage needs to make sense.
  • "Retrieval quality is the model's job." It is mostly your embedding, chunking, and indexing choices. The generator can only work with what you hand it.

Key takeaways

  • RAG = retrieve relevant chunks, then generate an answer grounded in them; it keeps knowledge fresh and citable without retraining.
  • It fails in two separable places: retrieval and generation. Evaluate and fix them independently.
  • Choose RAG for changing or citable knowledge, fine-tuning for changed behavior or format; they compose.
  • Most of your accuracy comes from retrieval quality (embeddings, chunking, indexing) and a prompt that refuses when the context does not support an answer.
  • Decide the refusal instruction and the citation format before the first document is indexed; both are cheap to build in and expensive to retrofit.

Related concepts

Practise this in questions