Build an AI Agent from Scratch in Python
IntermediateA support agent you write yourself: the loop, tools, stop conditions, memory and tests.
You build
A command-line agent that answers customer messages for a small online shop: it searches and reads the shop's help pages, looks up orders, and saves an action, such as a refund or a replacement, with a reply to the customer.
You need
- Python: functions, classes, dictionaries, exceptions, reading and writing files, and running commands in a terminal
- Calling a language model from Python through a chat API: messages and their roles, the system prompt, tokens, the context window and the finish reason of a reply
- Structured output: describing data as a Pydantic model and validating an answer of the model against it
- Tool calling: defining a tool with a JSON schema, reading the tool call the model returns, running the function in your own code and sending the result back (the course Build Your First LLM App in Python is one way to get this and the two items above)
To build it on your own machine
This is optional and comes with a plan. The course pack brings the starting files, a ready container and a check for every lesson. You need only Docker and git, plus your own AI coding agent if you want a tutor.
What you learn
An agent is a loop around a model. Write it yourself and nothing in it stays hidden.
You write one agent by hand, with no framework: a customer support agent for a small online shop, on the command line. Given a customer's message, it searches and reads the shop's help pages, looks up the order, decides what to do and saves an action with a reply to the customer. Each lesson adds one part: the loop, the tools, the final result, the limits, the tests, the memory. You can already call a model from Python and use tool calling; every check runs on saved model replies, so you need no key.
- The Loop. What separates an agent from a single model call or a fixed pipeline: the model chooses its own next step. The whole mechanism is a short loop and a list of messages, and you write both.
- Tools and Results. An agent is only as good as what it can do and what it gets back. You shape a small set of tools the model picks correctly, return results sized for the next decision, and make the agent finish with a result your code can check and save.
- Limits and Tests. A model in a loop can run forever or go in circles. Your code decides when a run ends and says why, and a fake model lets you test every path of the loop, the rare ones included, in a moment and without a network.
- Memory. The transcript is the only memory a run has, and it grows with every step. You keep it small while the run goes on, and give the agent notes that outlive a run so the next one starts knowing more.
Lessons
What an Agent Is: the Loop in a Few Lines
What an agent is, Parts of an agent, The agent loop, The transcript
Tools the Agent Can Use Well
The tool registry, Descriptions as instructions, A few purposeful tools, Results sized for the next step
Instructions and a Result Your Code Can Use
Agent instructions, Ways to finish, The final-answer tool, Rejecting a result
Stop Conditions: Keep the Loop Under Control
How runs go wrong, The step limit, Repeated calls, Stop reasons
Test the Agent with a Fake Model
Why not test on a live model, Tests with pytest, The model as a dependency, A scripted fake model
Short-Term Memory: Keep a Long Run Small
Short-term memory, Why a run grows, Context rot, Clearing old tool results
Long-Term Memory: Notes That Outlive a Run
A fresh run per task, Long-term memory, Notes the agent writes, Loading memory
Where it fits
Course 3 of 3 on the AI Engineer path.