AI Agents and RAG: Build Them Step by Step

How retrieval-augmented generation and AI agents really work, built from small Python programs: embeddings, chunking, vector search, grounded answers, the agent loop, memory, planning, multi-agent routing, guardrails and MCP. Watch every score, tool call and decision as it happens.

AI · intermediate · 16 lessons · 1 h 50 min · free

What you'll learn

Before you start

Lessons

Retrieval

Embeddings, chunking, vector stores and hybrid search.

  1. Embeddings and Cosine Similarity: Turn text into vectors and measure how close two vectors point. The closest document is the one RAG retrieves.
  2. Chunking with Overlap: Split a long text into pieces small enough to embed and retrieve, overlapping so no fact is cut in half.
  3. A Tiny Vector Store: Store each text with its vector, then return the top k matches for a question. This is what a vector database does.
  4. Keyword vs Meaning: Hybrid Search: "reimburse my cash" shares no words with "we refund your money", yet means the same. Combine keyword and meaning scores.

The RAG Pipeline

Prompt budgets, grounded answers with citations, and measuring retrieval.

  1. Building the Prompt Within a Budget: Number the retrieved chunks so the model can cite them, and stop adding chunks before the prompt gets too long.
  2. Grounded Answers and "I Don't Know": Answer only from a retrieved source and say which one. When nothing matches, say so instead of making something up.
  3. Evaluating Retrieval: Run test questions with known answers and measure the hit rate. One miss shows exactly where keyword matching fails.

Agents

Tools, the agent loop, memory, planning, agentic RAG and routing.

  1. Tools, Schemas and Validation: A model asks for a tool by name with arguments. Check the tool exists and the arguments are complete before running anything.
  2. The Agent Loop: Think, Act, Observe: The model picks a tool, the program runs it and adds the result to the history, and the model decides again, until it can answer.
  3. Agent Memory: Keep the last few messages word for word and move older ones into notes, so long conversations still fit the context window.
  4. Planning: Breaking a Goal into Steps: Ask for a plan first, then run each step and record its result and status, like a to-do list the agent works through.
  5. Agentic RAG: Rewrite, Search, Decide: Rewrite the user's words into search terms, search, and decide whether the result is good enough to answer from.
  6. Routing to Specialist Agents: A router reads each message and hands it to the right specialist: billing or tech support.

Agents in Production

Guardrails, retries and the Model Context Protocol.

  1. Guardrails: Injection and Allow-lists: Retrieved text can contain instructions aimed at your model. Filter it, and only let the agent use tools you allow.
  2. Handling Tool Failures with Retries: Tools time out. Catch the error, wait a little longer each time, try again, and give up gracefully.
  3. MCP: A Standard Way to Offer Tools: The Model Context Protocol lets any AI app discover and call tools from any server. Build a tiny server that answers tools/list and tools/call.

Related courses