AI Fundamentals — How Modern AI Works›07 · RAG: retrieval-augmented generation
Learning Hub / AI Fundamentals & AI-Assisted Engineering / AI Fundamentals — How Modern AI Works

Lesson 07 of 13 · Giving AI your knowledge

RAG: retrieval-augmented generation

Retrieval-augmented generation lets a model answer from your own, current documents without retraining it: split documents into chunks, embed them into a vector database, retrieve the most relevant chunks for each question, and ask the model to answer only from them, with citations. The two pipelines, where quality is won or lost, and when RAG is the wrong tool.

Practitioner
Key wordsRAGretrieval-augmented generationchunkingingestionretrievalgroundingcitationsre-rankinghybrid searchfine-tuning vs RAG

The idea

A model only knows what was in its training data, which is old and does not include your company's documents. Retrieval-augmented generation (RAG) fixes this at question time: find the relevant passages, add them to the prompt, and ask the model to answer from them.

It's the difference between a closed-book and an open-book exam. Instead of answering from memory (and sometimes making things up), the student is handed the right pages of the textbook and told: "Answer using these pages, and say which page you used."

Two pipelines

INGESTION (when documents change)
documents ──► clean & split into chunks ──► embed each chunk ──► vector database
(wiki, PDFs,   (~200–800 words each,        (embedding model)   (vector + text +
 tickets)       with some overlap)                               source, permissions)

QUESTION TIME (every request)
question ──► embed ──► search: top-k chunks ──► prompt = instructions ──► LLM ──► answer
                       + permission filter        + chunks + question            + citations
                       + keyword search (hybrid)
                       + re-rank (optional)

A typical prompt at the end looks like:

Answer the question using ONLY the sources below. If they do not contain the
answer, say "I couldn't find that in the documentation". Cite sources as [1], [2].

[1] help/mfa.md: "To reset MFA, open Settings → Security → Reset device…"
[2] help/sso.md: "Administrators can reset a user's factors from the admin console…"

Question: How do I reset two-factor authentication?

Where quality is won or lost

Stage What goes wrong Fix
Ingestion Scanned PDFs with no text, menus and footers in every chunk Extract text properly, strip boilerplate
Chunking Chunks too big (vague) or too small (no context); tables split in half Split by headings, add overlap, keep the title with each chunk
Retrieval Right document never found; codes and names missed Hybrid search, re-ranking, better chunk titles
Permissions Users see text they shouldn't Filter by the user's access at search time
Generation Model ignores sources or adds its own facts "Only from the sources", require citations, low temperature
Freshness Old versions answer Re-ingest on change, delete removed documents, filter by version

The most important debugging habit: when an answer is wrong, look at which chunks were retrieved. If the right one is not there, the problem is search, not the model.

RAG, fine-tuning or a long prompt?

Approach Good for Weak for
RAG Facts that change, large document sets, citations, permissions Teaching a new style or skill
Fine-tuning (lesson 11) Consistent format, tone, specialised behaviour Frequently changing facts; no citations
Paste everything into a long context A few documents, one-off questions Large collections, cost per request, permissions

Many good systems combine them: a lightly tuned or well-prompted model, with RAG for facts.

Measuring it

Collect 30–100 real questions with known good answers and sources. After every change (chunk size, model, prompt), run them and check: was the right source retrieved? Is the answer correct? Does it cite the source? Does it say "not found" when it should? Without this set, changes are guesses.

Recap

  • RAG = retrieve relevant chunks, augment the prompt, generate a cited answer.
  • Two pipelines: ingestion (chunk, embed, store) and question time (embed, search, prompt, answer).
  • Most failures are retrieval failures; check the retrieved chunks first.
  • Enforce permissions in search, keep the index fresh, and measure with a question set.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.