The Agent Loop

Module 6, Lecture 6.1 | Building the Coding Agent

This lecture takes a deep dive into the two-loop architecture that powers every tool-calling agent. Building on the coding agent introduced in Lecture 5.3, it examines why two loops are necessary, how stop_reason drives the inner loop as the sole branching condition, and how the messages array grows through single-tool and multi-tool exchanges. Students live-code the agent loop from scratch using stub tools, tracing the messages array at each step to see exactly what the model sees — and why tool results enter the conversation as role: "user".

Read the full lecture narrative

Additional Resources