lesson
50 min

RAG and Agents

Ground a language model in your own documents with retrieval, and understand what changes when AI can take actions.

Objective

By the end of this lesson, you can explain how retrieval-augmented generation works, build a toy retriever, and name the main risks of AI agents.

Retrieval-augmented generation

An LLM only knows its training data. Retrieval-augmented generation (RAG) gives it the right documents at question time:

  1. Retrieve: search your own trusted documents for the passages most related to the question.
  2. Augment: paste those passages into the prompt.
  3. Generate: ask the model to answer only from those passages, and to cite them.

RAG lowers made-up answers and lets you update knowledge by updating documents, not retraining. Real systems search with embeddings: lists of numbers that place similar meanings close together. The toy below uses shared words instead.

Run it. The retriever picks the document that shares the most words with each question. Look at the bus question: it matched the cooling document only on filler words like when, is, and the. Fix it by ignoring those words, or by requiring a higher score before answering.
Python 3
Ctrl+Enter to run | Tab to indent | Python runs in your browser via Pyodide

The bus question has no good answer in these documents. A safe RAG system says "I don't have that information" instead of answering from the wrong document. Teaching a system to say "I don't know" is part of the job.

Agents

An AI agent is an LLM that can use tools and take steps on its own: search the web, run code, send email, fill out forms. You give it a goal. It plans, acts, checks results, and repeats.

Agents raise the stakes, because mistakes become actions:

  • Wrong action: sending the wrong message to the wrong person.
  • Prompt injection: hidden instructions in a web page or document that trick the agent into doing something else.
  • Runaway loops: repeating actions, wasting time and resources.

Safe agent design: give it the fewest tools it needs, require a person to approve anything that sends, spends, deletes, or contacts people, and log every action.

A national example

Quiz: RAG and agents check

  1. 01What does the retrieve step in RAG do?

    Choose one.

  2. 02An agent is about to email 200 families. What should happen first?

    Choose one.

  3. 03What is prompt injection?

    Choose one.

Go deeper: Automated Workflows: The Playbook for Cutting Busywork, Protecting Revenue, and Scaling Without Burning Out

Ask. Answer. Build together.