The Agent Loop, From Scratch
Build the loop every AI agent runs on, in plain Python, and learn exactly where it breaks.
Text courseBeginnerFree
Frameworks hide the loop. This short course takes it apart: one model call, one tool, one stopping rule at a time. By the end you can read any agent framework's source and know what it is doing, and why your agent loops forever or quits too early.
What you'll learn
- Write a working agent loop in about 30 lines
- Expose tools so the model can act, and handle their errors
- Stop the loop on purpose: budgets, step limits and done-checks
- Decide what state to keep between steps and between runs
- Debug a run from its trace, and replay it without paying for model calls
- Build a codebase Q&A agent end to end, and find the same loop inside any framework
Course content
Chapter 1How the loop works
Chapter 2Making it safe to run
Chapter 3Build one for real
- 5Debugging from the traceThe final answer is a claim; the trace is the evidence. Log every step, spot the usual failure shapes, and replay a run for free.
- 6Project: a codebase Q&A agentEvery lesson in one working program: three read-only tools, brakes, and a done-check that verifies every citation.
- 7Finding the loop in any frameworkFour searches that find the loop inside any agent framework, what frameworks really add, and when you need one.