Free course · Beginner to intermediate

Build an AI Agent in Python From Scratch

Build a working AI agent in plain Python, one piece at a time, on a free local model.

  • 5 modules
  • 15 lessons
  • About 2 hours of reading
  • Beginner to intermediate
  • A coding problem in every lesson
  • Quizzes, final exam and certificate

What you will build

A trip helper agent in plain Python that checks a weather table, converts money, searches hotels, remembers preferences, connects to tools over MCP, and logs every step so you can test and debug it. All data is made up, so everything runs offline on a free local model. This is a real trace from the finished agent in lesson 15, asked "Is Oslo colder than Lisbon right now?":

step 1 LLM   5.74s  in=381   out=43   get_weather, get_weather
step 1 TOOL get_weather({"city":"Oslo"}) -> {"city": "Oslo", "forecast": "cloudy", ...}
step 1 TOOL get_weather({"city":"Lisbon"}) -> {"city": "Lisbon", "forecast": "sunny", ...}
step 2 LLM  13.43s  in=525   out=66   answer: 'Right now, Oslo is colder than Lisbon. ...'
total tokens in=906 out=109 cost=$0.0000

Tested with qwen2.5 (1.5B) on Ollama on a CPU only machine. Timings will differ on your machine. Hosted providers such as OpenAI are supported by the code through environment variables but were not tested.

What you will learn

  • Call an LLM from Python, see why the API forgets, and write a working agent loop with tool calling.
  • Design tools a model uses correctly, get validated structured output, and guard risky actions.
  • Keep long chats inside the context window, add long-term memory with embeddings, and make retrieval a tool.
  • Compare ReAct with plan-then-execute, add budgets and retries, and decide when multiple agents help.
  • Connect to MCP servers, test the agent with an eval harness, and trace every step to debug failures.

Curriculum

5 modules, 15 lessons. Each lesson is a short article, a hands-on problem you solve in the browser, and a 3-question quiz. See the day-by-day study plan.

1Your First Agent3 lessons · 21 min

Call an LLM from Python, see why the API forgets, and write a working agent loop with tool calling.

  1. 1Call an LLM From Python and See Why the API ForgetsLesson 1 · 7 min read · Easy problem · quiz
  2. 2Write an AI Agent Loop in 60 Lines of Python, No FrameworkLesson 2 · 7 min read · Medium problem · quiz
  3. 3Tool Calling in Python: Describe a Function With JSON Schema, Run It, Send the Result BackLesson 3 · 7 min read · Medium problem · quiz
Module 1 practice test →
2Tools That Work3 lessons · 23 min

Design tools a model uses correctly, get validated structured output, and guard risky actions.

  1. 4How to Design Tools an LLM Agent Uses Correctly: Names, Descriptions and ErrorsLesson 4 · 9 min read · Easy problem · quiz
  2. 5Structured Outputs in Python: Get Valid JSON From an LLM Every TimeLesson 5 · 7 min read · Medium problem · quiz
  3. 6Safe Agent Tools in Python: Allowlists, Human Approval and Prompt InjectionLesson 6 · 7 min read · Easy problem · quiz
Module 2 practice test →
3Memory and Context3 lessons · 24 min

Keep long chats inside the context window, add long-term memory with embeddings, and make retrieval a tool.

  1. 7Agent Short-Term Memory in Python: Trim and Summarize Chat HistoryLesson 7 · 8 min read · Medium problem · quiz
  2. 8Long-Term Agent Memory in Python With Embeddings and NumPyLesson 8 · 8 min read · Easy problem · quiz
  3. 9Agentic RAG in Python: Make Retrieval a Tool the Agent Decides to CallLesson 9 · 8 min read · Easy problem · quiz
Module 3 practice test →
4Planning and Control3 lessons · 22 min

Compare ReAct with plan-then-execute, add budgets and retries, and decide when multiple agents help.

  1. 10ReAct vs Plan-and-Execute Agents, With Python Code for BothLesson 10 · 8 min read · Medium problem · quiz
  2. 11Stop Runaway AI Agents: Step Limits, Retries and Cost Budgets in PythonLesson 11 · 7 min read · Medium problem · quiz
  3. 12Multi-Agent Systems in Python: Orchestrator, Workers, and When One Agent Is BetterLesson 12 · 7 min read · Easy problem · quiz
Module 4 practice test →
5Ship It3 lessons · 23 min

Connect to MCP servers, test the agent with an eval harness, and trace every step to debug failures.

  1. 13Connect a Python Agent to MCP Servers With the Official MCP SDKLesson 13 · 8 min read · Easy problem · quiz
  2. 14Test Your AI Agent in Python: A Small Eval Harness With Tool-Call ChecksLesson 14 · 7 min read · Medium problem · quiz
  3. 15Trace and Debug an AI Agent in Python: Step Logs, Token Costs and ReplaysLesson 15 · 8 min read · Easy problem · quiz
Module 5 practice test →

Finish the course

Pass the lesson quizzes, then take the final exam and submit the capstone project. Your Certificate of Completion is issued once you pass both.

The Certificate of Completion is issued by BlogLLM. It is not an accredited qualification and it is not affiliated with any university or vendor.