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
Build a working RAG pipeline: embed, chunk, store, retrieve, assemble, answer
Stop hallucinations with grounded answers, citations and "I don't know"
Measure retrieval quality with a test set
Write the agent loop yourself: tools, validation, memory and planning
Ship safely: prompt-injection guards, tool allow-lists, retries and MCP
Before you start
Python Fundamentals
How AI Language Models Work
Lessons
Retrieval
Embeddings, chunking, vector stores and hybrid search.
Embeddings and Cosine Similarity: Turn text into vectors and measure how close two vectors point. The closest document is the one RAG retrieves.
Chunking with Overlap: Split a long text into pieces small enough to embed and retrieve, overlapping so no fact is cut in half.
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.
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.
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.
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.
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.
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.
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.
Agent Memory: Keep the last few messages word for word and move older ones into notes, so long conversations still fit the context window.
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.
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.
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.
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.
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
Machine Learning from Scratch: How machines learn from data, built from plain Python on tiny datasets so you can watch every number change. No libraries, no black boxes. (13 lessons, 2 h 25 min)
How AI Language Models Work: Tokens, embeddings, attention, training, RAG and agents, each built in a few lines of plain Python so you can watch every number. No libraries, no black boxes. (20 lessons, 2 h 51 min)
AI Beyond Language Models: Search, game playing, learning from rewards, image generation, recommendations, forecasting and sound, each in a few lines of plain Python you can step through. (7 lessons, 58 min)