Skip to content
learn
  1. Learn
  2. Artificial Intelligence
  3. Retrieval Augmented Generation

AI AI-08.01

Why RAG?

Understand retrieval augmented generation: why language models need it, how the pipeline works end to end, and where it breaks.

Beginner 10 min Last reviewed

The big idea

Ask a language model "What is our refund policy for annual plans?" and one of two things happens. Either it says it does not know, or, worse, it writes a confident, plausible policy that your company never had. The model was trained on public text up to some date. It has never seen your help center, your contracts, or last week's pricing change.

Retrieval augmented generationRetrieval augmented generation: fetching relevant text from your own data at question time and giving it to the model as context, so its answer is grounded in that text. (RAG) fixes this with an open-book exam. Before the model answers, your system looks up the most relevant passages from your own documents and pastes them into the prompt with an instruction: answer using only this material, and cite it. The model does what it is good at (reading and writing), and your retrieval system does what the model cannot (knowing your private, current facts).

How it works

RAG has two halves that run at different times.

The RAG pipeline, offline and online Offline, along the top: documents are loaded, split into chunks, turned into vectors by an embedding model and stored in a vector index with metadata. Online, along the bottom: a user question is embedded with the same model, the nearest chunks are retrieved from the index and reranked, and the top few go into the prompt with the question. The language model writes an answer that cites its sources. The online data path glows. nearest chunks documents chunk embed vector index + metadata question embed retrieve top 30 rerank keep 5 LLM cited answer offline: ingest online: answer Figure 1. Offline (top), documents are chunked, embedded and indexed. Online (bottom), the question is embedded, the nearest chunks are retrieved and reranked, and the model answers from them with citations.

Step 1, offline: ingest your documents

  1. Load sources: PDFs, web pages, wiki pages, tickets, database rows.
  2. Chunk them into passages of a few hundred tokens. A whole manual is too big to retrieve usefully; a single sentence loses its context. Chunking is covered in depth in Document ingestion and chunking .
  3. Embed each chunk into a vector with an embedding model, so passages with similar meaning end up close together.
  4. Index the vectors in a vector indexA store built for fast nearest-neighbour search over vectors, usually with approximate indexes such as HNSW., along with metadata such as source URL, title, date and who is allowed to see it.

This runs once up front and then again whenever documents change.

Step 2, online: answer a question

  1. Embed the question with the same embedding model.
  2. Retrieve the top k nearest chunks, often 20 to 50, usually combining vector similarity with keyword search (BM25) so exact terms like error codes are not missed.
  3. Rerank those candidates with a slower, more precise model and keep the best 3 to 8.
  4. Generate: build a prompt with the instructions, the retrieved chunks (each labelled with its source) and the question. Ask for an answer that cites sources and says "I don't know" when the chunks do not contain the answer.

Step 3: what the prompt actually looks like

You answer questions about Acme's products using ONLY the documents below.
If the answer is not in the documents, say you don't know.
Cite sources as [1], [2].

[1] (help/refunds.md) Annual plans can be refunded in full within 14 days of purchase...
[2] (help/billing-faq.md) After 14 days, annual plans are refunded pro rata for unused months...

Question: What is the refund policy for annual plans?

Nothing magical: the model is reading two paragraphs and summarizing them. That simplicity is why RAG is usually the first thing to try when a model needs your knowledge.

Try it

  1. Question arrives A user asks: Can I get a refund on my annual plan after 3 weeks?
  2. Embed the question The question becomes a vector with the same model used for the documents.
  3. Retrieve Vector search plus keyword search returns 30 candidate chunks. The billing FAQ and refunds page are near the top.
  4. Rerank A cross-encoder scores each candidate against the question and keeps the best 5.
  5. Generate with citations The model answers: After 14 days, annual plans are refunded pro rata [2]. It cites the FAQ.

Try this:

  1. Step through once and note which component decides whether the right paragraph is even visible to the model.
  2. Imagine the billing FAQ was never ingested. Which step fails, and what should the model say?
  3. Imagine retrieval returns an outdated 2023 refunds page. How could metadata (dates) help?

The same pipeline in miniature, with word overlap standing in for embeddings:

mini-rag.pyruns with python3 + numpy
# The smallest useful RAG loop: chunk, index, retrieve by word overlap, build a cited prompt.
# Real systems use embeddings and a vector index; the shape of the pipeline is the same.
import re

DOCS = {
    "help/refunds.md": "Annual plans can be refunded in full within 14 days of purchase. "
                       "Monthly plans are not refundable. Contact billing to start a refund.",
    "help/billing-faq.md": "After 14 days, annual plans are refunded pro rata for unused months. "
                           "Refunds go back to the original payment method within 10 days.",
    "help/export.md": "You can export notes as Markdown or PDF from the File menu. "
                      "Exports include attachments up to 50 MB.",
}

def chunks(path, text, size=2):                    # offline: split into 2-sentence chunks
    sents = re.split(r"(?<=\.) ", text)
    for i in range(0, len(sents), size):
        yield {"src": path, "text": " ".join(sents[i:i + size])}

STOP = {"the", "a", "of", "can", "i", "my", "to", "in", "an", "on", "after"}
STEM = False                                         # crude stemming: refunds/refunded -> refund
def words(s):
    w = set(re.findall(r"[a-z0-9]+", s.lower())) - STOP
    return {re.sub(r"(ed|s)$", "", x) for x in w} if STEM else w
INDEX = [c for p, t in DOCS.items() for c in chunks(p, t)]

def retrieve(question, k=2):                         # online: score every chunk, keep the top k
    q = words(question)
    scored = sorted(INDEX, key=lambda c: len(q & words(c["text"])), reverse=True)
    return [c for c in scored[:k] if q & words(c["text"])]

def build_prompt(question):
    ctx = "\n".join(f"[{i}] ({c['src']}) {c['text']}" for i, c in enumerate(retrieve(question), 1))
    return ("Answer using ONLY the documents below. If the answer is not there, say you don't know.\n"
            f"Cite sources as [1], [2].\n\n{ctx}\n\nQuestion: {question}")

Q = "Can I get a refund on my annual plan after 3 weeks?"
print(f"{len(INDEX)} chunks indexed")
print("exact words:", [c["src"] for c in retrieve(Q)])   # misses the FAQ: "plans", "refunded"
STEM = True
print("with stems: ", [c["src"] for c in retrieve(Q)], "\n")
print(build_prompt(Q))

Output

4 chunks indexed
exact words: ['help/refunds.md', 'help/refunds.md']
with stems:  ['help/refunds.md', 'help/billing-faq.md'] 

Answer using ONLY the documents below. If the answer is not there, say you don't know.
Cite sources as [1], [2].

[1] (help/refunds.md) Annual plans can be refunded in full within 14 days of purchase. Monthly plans are not refundable.
[2] (help/billing-faq.md) After 14 days, annual plans are refunded pro rata for unused months. Refunds go back to the original payment method within 10 days.

Question: Can I get a refund on my annual plan after 3 weeks?

Matching exact words, retrieval returns two chunks of the refunds page and misses the billing FAQ, the one passage that answers the question, because it says "plans" and "refunded" instead of "plan" and "refund". Crude stemming fixes it. Real systems use embeddings and hybrid search for the same reason ( Hybrid search and reranking ), and the lesson holds at any scale: if retrieval misses, the model never sees the answer.

Going deeper

Where RAG breaks

Most RAG failures are retrieval failures, not model failures. The model can only use what retrieval hands it.

  • The answer is split across chunks, and only half was retrieved.
  • Vocabulary mismatch: the user says "money back", the docs say "reimbursement". Hybrid search and query rewriting help ( Contextual retrieval and query transformation ).
  • Lost context: a chunk says "this limit is 10 per day" but no longer says which product "this" is. Contextual retrieval adds a short document summary to each chunk before embedding.
  • Stale or conflicting sources: two versions of the same policy. Use dates, versioning and deletion in the ingestion pipeline.
  • Permission leaks: retrieval returns a document the user should not see. Filter by access control at query time, never after generation.

RAG vs fine-tuning vs long context

RAG gives a model knowledge. Fine-tuning mostly changes behaviour, style and format. Long context windows let you paste in more material, but cost and latency grow with every token, and models attend less reliably to the middle of very long inputs. A common production pattern combines them: RAG for facts, a light fine-tune or strong prompt for tone and format. See the decision guide in Prompting vs RAG vs fine-tuning: choosing .

Measuring it

Evaluate the two halves separately ( Evaluating RAG ): retrieval with recall at k ("was the right chunk in the top k?") on a set of questions with known source passages, and generation with faithfulness ("is every claim supported by the retrieved text?") and answer relevance.

Trade-offs

OptionWins whenLoses when
RAG Knowledge is private, large, or changes often; you need citations Adds an ingestion pipeline, an index and retrieval latency; quality is capped by retrieval
Paste everything into a long context Small, stable corpus that fits comfortably; quick prototype Cost and latency per request grow with every token; weaker recall in very long inputs
Fine-tune on the documents Teaching format, tone or a narrow skill Poor at memorizing facts reliably; must retrain to update; no citations

Common mistakes

  • Skipping evaluation and judging quality by trying five questions by hand.
  • Tuning the prompt when the real problem is that retrieval never returned the right chunk.
  • Chunking by a fixed character count that splits tables and code in half.
  • Embedding queries and documents with different embedding models.
  • Not telling the model what to do when the context does not contain the answer.

In the wild

DoorDash

Path to high-quality LLM-based Dasher support automation (opens in a new tab)

DoorDash describes a RAG system that answers delivery drivers' support questions from its knowledge base. Their post focuses on what it took to make it reliable: guardrails that check responses for grounding and policy issues, an LLM-based judge for quality monitoring, and a loop for improving the knowledge base where answers were weak.

Anthropic

Introducing contextual retrieval (opens in a new tab)

This post shows how chunks lose meaning when cut out of their document, and proposes prepending a short, model-written context to each chunk before embedding and BM25 indexing. It reports large reductions in failed retrievals, further improved with reranking: a direct answer to the "lost context" failure above.

Check yourself

  1. What problem does RAG primarily solve?

    Show answer

    B. Gives the model access to private or current knowledge at question time The model's weights only contain what it saw in training. RAG supplies your own, current documents in the prompt so answers are grounded in them.

  2. Your RAG bot gives a wrong answer. Logs show the correct paragraph was not in the retrieved chunks. What should you fix first?

    Show answer

    B. The retrieval step: chunking, hybrid search, or reranking If the right text never reaches the model, no prompt or model can use it. Fix retrieval first.

  3. Why must the question be embedded with the same model as the documents?

    Show answer

    B. Different models put text in different vector spaces, so distances between them are meaningless Similarity only makes sense inside one embedding space. Mixing models breaks nearest-neighbour search.

  4. Where should document access control be enforced?

    Show answer

    B. In the retrieval query, before chunks reach the model Once a restricted chunk is in the prompt, the model may reveal it. Filter by permissions at retrieval time.

Recap

  • RAG is an open-book exam: retrieve relevant passages, then let the model answer from them with citations.
  • Offline you chunk, embed and index; online you embed the question, retrieve, rerank and generate.
  • Answer quality is capped by retrieval quality, so most fixes are retrieval fixes.
  • Use RAG for knowledge, fine-tuning for behaviour, and evaluate retrieval and generation separately.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy