RAG Fundamentals

Retrieve relevant chunks for a question, put them in the prompt, and have the model answer from them instead of from memory.

Intermediate · about 6 hours · difficulty 3/5

Learn first: Chunking Strategies, Structured Prompts and Long Context

Retrieval-augmented generation (RAG) is the most common production LLM pattern. The model's built-in knowledge is frozen and generic; RAG injects fresh, private, relevant information at question time.

The pipeline

  1. Ingest (offline): load documents, clean them, chunk, embed, store with metadata.
  2. Retrieve (online): embed the user question, fetch the top-k most similar chunks (optionally filtered).
  3. Augment: build a prompt with the chunks in clearly tagged blocks plus instructions.
  4. Generate: the model answers using only the provided context, and says so when the context is insufficient.

Why RAG over alternatives

  • Updating knowledge means re-indexing documents, not retraining a model.
  • You can show sources, which builds trust and allows verification.
  • Access control is enforced at retrieval time.
  • Even with huge context windows, sending your entire knowledge base every request is slow and expensive; retrieval selects what matters. For small, stable corpora, simply putting everything in context (with caching) can be a valid alternative.

Typical failure modes

  • Retrieval miss: the answer exists but was not retrieved. The model then guesses or declines.
  • Wrong context: similar-looking but irrelevant chunks lead to confident wrong answers.
  • Ignoring context: the model answers from memory. Tighten instructions.

Most RAG quality problems are retrieval problems, not generation problems. Always inspect what was retrieved.

Good enough to move on

You have a working question-answering app over a real document set that retrieves chunks, answers from them and admits when it does not know.

Key concepts

  • Ingest, retrieve, augment, generate
  • Top-k selection
  • Grounding instructions
  • RAG vs long context vs fine-tuning
  • Inspecting retrieved context: Debug retrieval first
  • Conversational RAG (follow-up questions)

Mini project

Build a question-answering app over your college or company handbook (or any public PDF set): ingestion script, retrieval endpoint, and a chat page that shows which chunks were used.

Check yourself

The model gave a wrong answer in your RAG app. What is the first thing you check, and why?

Free resources