You now have plans, reasoning, memory, reflection, and function calling. The agent loop is the runtime that stitches them: observe → infer → act → observe, until success, failure, or a halt rule. This is the executable heart of Module 15.1.
A one-off while loop is enough to learn. Production loops add persistence—LangGraph checkpointers, HITL interrupts, and later MCP clients as the tool transport. The next lecture, agentic workflows, asks when a loop should be replaced by a mostly deterministic graph.
Learning Objectives
By the end of this lesson, students should be able to:
- Describe the observe–think–act loop and why it must be bounded.
- Implement a complete Python loop with tool dispatch, step limits, and finalization.
- List halt conditions: max steps, token/dollar budget, no-progress, policy deny, user cancel.
- Log each iteration for eval and episodic memory.
- Contrast a naive
whileloop with a durable LangGraph cycle. - Avoid infinite tool ping-pong and silent swallow of errors.
An agent loop is the control cycle that repeatedly (1) sends conversation + tool schemas to a model, (2) executes any requested tools, (3) appends observations (and optional memory/reflection), and (4) stops when the model returns a final answer or a halt condition fires.
Loop vs One-Shot Completion
| Halt signal | Meaning | Typical action |
|---|---|---|
No tool_calls | Model thinks it is done | Return content to user |
| Max steps | Budget exhausted | Partial answer + escalate |
| Repeated same tool+args | No progress | Break; ask human or replan |
| Policy deny | Autonomy bound hit | HITL or safe refusal |
| User cancel / timeout | External stop | Checkpoint and exit |
A Complete Thin Loop
Naive Loop vs Durable Graph
Thin while loop
- Easy to teach and unit-test
- Dies if the process dies
- HITL is DIY
LangGraph cycle
- Checkpoints / resume
- Explicit edges + interrupts
- Better for long jobs
Either needs
- Step + cost budgets
- Tool allowlists
- Traces for eval / 15.2 memory
Strengths
- Handles unknown hop counts
- Composable with any tool set
- Clear place for logging
- Maps 1:1 to ReAct
Tradeoffs
- Unbounded loops explode cost
- Harder SLAs than a DAG
- Error handling is on you
- Eval must be trajectory-aware
“A real agent has no max iterations—that would limit autonomy.” Unbounded loops are outages waiting to happen. Autonomy is bounded by design (autonomous agent). Always ship step, time, and spend caps, plus no-progress detection.
Knowledge Check
- Short Answer: Name the four beats of the agent loop. Answer: Send to model, execute tools, append observations, stop or repeat.
- True/False: No
tool_callsusually means the model is returning a final answer. Answer: True. - Multiple Choice: Repeated identical tool fingerprints suggest: (a) success, (b) no progress / stuck loop, (c) better pooling. Answer: (b).
- Short Answer: Why log each step? Answer: Eval, debugging, episodic memory, cost tracking.
- True/False: LangGraph is required to have an agent loop. Answer: False—a thin while-loop is a valid loop; graphs add durability.
- Multiple Choice:
MAX_STEPSprimarily protects: (a) fonts, (b) cost/latency runaway, (c) BPE vocab. Answer: (b). - Short Answer: What should happen on unknown tool names inside the loop? Answer: Fail closed with a structured error observation (do not crash blindly).
- Short Answer: Which next lecture asks when not to use a free loop? Answer: Agentic workflow.
- Multiple Choice: HITL inside a loop is typically: (a) pause before risky tools, (b) disable JSON, (c) drop memory. Answer: (a).
- True/False: Agent eval can ignore trajectories and score only the final sentence. Answer: False—tool misuse matters even if the final text looks fine.
Key Takeaways
- The agent loop repeatedly calls the model and tools until a final answer or halt rule.
- Bound steps, detect no-progress, validate tools, and log every iteration.
- Thin loops teach the idea; LangGraph (and similar) add durability and interrupts.
- Unbounded looping is not autonomy—it is a missing product constraint.
- Next: agentic workflows—when to mix loops with deterministic pipelines.
Whiteboard: Add a token-budget halt beside MAX_STEPS. Show where you would insert a refund HITL gate.
Lab: Inject a buggy tool that always returns “try again.” Confirm the fingerprint halt fires before step 6.
Recap: The agent loop is bounded observe–think–act. Continue with Agentic Workflow.