Skip to main content

Retrieval-Augmented Generation

Retrieval-augmented generation (RAG) combines a query, a retriever over external evidence, context construction, and a generative model. Retrieval can make evidence inspectable and updateable, but it does not guarantee that the evidence is relevant, trusted, correctly interpreted, or faithfully reflected in the answer.

Prerequisites​

Understand retrieval rankings, access control, prompt contracts, and structured validation.

Learning goals​

  • identify retrieval, context construction, generation, and checking as separate stages.
  • carry stable source IDs into citations.
  • exclude evidence that fails access or provenance policy.

Worked example​

The retriever and response are fixed stand-ins so the orchestration is testable offline. The mocked answer is not evidence about a real model.

documents = {
"kb-retention": {"text": "Receipts are retained for seven years.", "allowed": True},
"kb-travel": {"text": "Travel requires manager approval.", "allowed": False},
}
retrieved = ["kb-retention", "kb-travel"]
context_ids = [doc_id for doc_id in retrieved if documents[doc_id]["allowed"]]
context = "\n".join(f"[{doc_id}] {documents[doc_id]['text']}" for doc_id in context_ids)
prompt = f"TASK: Answer only from EVIDENCE.\nQUESTION: How long are receipts retained?\nEVIDENCE:\n{context}\nRETURN: answer and citations."
mocked_response = {"answer": "Receipts are retained for seven years.", "citations": ["kb-retention"]}

print(f"retrieved={retrieved}")
print(f"context_ids={context_ids} prompt_chars={len(prompt)}")
print(f"answer={mocked_response['answer']} citations={mocked_response['citations']}")

Output:

retrieved=['kb-retention', 'kb-travel']
context_ids=['kb-retention'] prompt_chars=168
answer=Receipts are retained for seven years. citations=['kb-retention']

Production context construction also needs chunk boundaries, deduplication, ordering, a token budget, document versions, access decisions, and explicit marking of untrusted retrieved content. Citations should point to stable evidence, and code must verify that cited IDs were actually admitted.

Common mistakes​

  • Describing RAG as eliminating hallucination.
  • Sending top-k chunks directly to a model before authorization and provenance checks.
  • Letting long or duplicated chunks crowd out the decisive evidence.
  • Showing citations that are plausible-looking but absent from the supplied context.

Practice​

Add two conflicting policy versions. Define a freshness rule and a fallback that asks for human review instead of silently choosing one.

Next steps​

Evaluating Generative-AI Systems measures each stage independently.

Primary sources​

Checked 2026-10-16: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks, REALM, and OWASP RAG Security.