Module 7 · AI infrastructureLesson 82 of 888 minAdvanced

RAG (retrieval-augmented generation)

RAG looks up relevant passages from your own data and puts them in the prompt, so the model answers from real, current facts instead of guessing.

Try it

Play "With retrieval" and follow each step. Then play "Without retrieval" and compare the answer.

LLMs know only what was in their training data, which is frozen in time and doesn't include your company's documents. Ask about your refund policy and they'll produce a plausible guess. Retrieval-augmented generation (RAG) fixes this without retraining the model.

Indexing (ahead of time)

  1. Load documents: PDFs, wiki pages, tickets, product data.
  2. Chunk them into passages of a few hundred tokens, often with some overlap, keeping headings and source info.
  3. Embed each chunk.
  4. Store the vectors and text in a vector database, with metadata (source, date, permissions).

Answering (at question time)

  1. Embed the user's question.
  2. Retrieve the most similar chunks (often with hybrid keyword + vector search, and metadata filters such as "only documents this user may see").
  3. Optionally re-rank them with a more precise model.
  4. Prompt the LLM with instructions, the retrieved passages, and the question.
  5. Return the answer with citations to the sources.

Why it's popular

  • Answers use current and private data.
  • Updating knowledge means re-indexing documents, not retraining.
  • Citations let users check the answer.
  • Much cheaper than fine-tuning for knowledge.

Where it goes wrong

  • Bad chunks: split mid-table, or too big to be specific.
  • Retrieval misses: the right passage isn't in the top results. Use hybrid search and re-ranking; measure retrieval separately.
  • Too much context: stuffing 50 chunks confuses the model and costs tokens.
  • Stale or unauthorised data: keep the index fresh and enforce permissions at retrieval time.

Build an evaluation set of real questions with known answers, and measure both retrieval and final answers as you change things.

Quick check

A RAG chatbot gives a wrong answer even though the right document is indexed. What should you check first?