CourseAgentic AI Engineer: The Complete Course · Module 5: The Agent Loop · part 32 of 97
Part 32 · Module 5: The Agent Loop

Lab: The agent loop moves into the package

45 min read·13 Oct 2026
Module 5 · Lab · About 75 minutes · Needs: the Module 3 lab's tools.py, and lessons 1 and 3 (run_agent, over_limit, act and build_graph; interrupt, the SQLite checkpointer and RunControl); Lesson 2 for the reasons a loop is worth its cost

What you are building

RETURNS_DESK/LOOP.PY AFTER THIS LAB run_agent 4 limits, 6 reasons build_graph same loop, LangGraph build_approval_graph pause, resume, cancel tools.py Module 3's call, imported BrightCartSystems model or a stand-in checkpoints graph_checkpoints.db 5 tests
The finished piece: three entry points in one new file of your code (blue), all calling Module 3's tools (grey, unchanged), driven by a model or a stand-in (amber) and pinned by five tests (green).

This lab moves the module's loop into returns_desk/loop.py, so later modules import it instead of pasting it. The file holds Lesson 1's loop with its four limits and named stop reasons, the same loop as a LangGraph graph, and Lesson 3's approval graph: it pauses before Aisha's $129.00 refund, survives a restart, and stops when Priya cancels. Every tool call still goes through Module 3's call, imported from tools.py, never copied. Tests pin the stop reasons with a stand-in model, then a real model works two requests. Everything at BrightCart is simulated.

The rest of this course is yours to keep

This course is bought on its own, once, and stays readable afterwards, including the parts added to it later.