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.

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.

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.

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, generation with faithfulness (does the answer follow from the context) and answer relevance. 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
LESSONS THAT TEACH THIS
PRACTICE THIS IN REAL QUESTIONS