Lesson 11.3 · 24 min · video lesson
The Agent Loop: Observe, Think, Act, Repeat
Every AI agent, from a tiny script to a coding assistant, runs the same few lines of code over and over; what are they, and how do they know when to stop?
In short: The agent loop is the small piece of ordinary code that repeatedly calls the model, runs any tools it asks for, feeds the results back, and stops when the model gives a final answer or a limit is hit. Each pass is one think-act-observe cycle. Getting the loop right (stop conditions, error handling, parallel calls, budgets) is what separates a demo from a dependable agent.
What you will learn
- The Big Picture
- What is the AI Agent Loop
- Why an AI Agent Needs a Loop
- The Think-Act-Observe Cycle
- The Loop Step by Step
- The Loop in Real Code
- Parallel Tool Calls in One Turn
- How the Loop Knows When to Stop
- Common Loop Failures
- Quick Summary
In this lesson
- The big picture
- What is the AI agent loop?
- Why an AI agent needs a loop
- The think-act-observe cycle
- The loop step by step
- The loop in real code
- Parallel tool calls in one turn
- How the loop knows when to stop
- Common loop failures
- Worked example, step by step
- Practice: try it yourself
Key terms in this lesson
- Agent loop
- The repeating code that calls the model, executes requested tools, and feeds results back until a stop condition.
- Turn (step)
- One iteration of the loop: one model call plus execution of any tool calls it returned.
- Think-act-observe
- The cycle inside each turn: reason about the next move, take an action with a tool, read the result.
- Stop condition
- A rule that ends the loop, such as a final answer, a step budget, or stuck detection.
- Stuck detection
- Checks that notice the agent repeating itself or making no progress, and stop or redirect it.
- Budget
- A hard limit on steps, tokens, money or time that guarantees the loop terminates.
This lesson is part of the Generative AI Engineering and Complete AI Engineer tracks. See plans and pricing to open the full lesson, its video, labs and quiz, or try the free lesson first (free account needed).
← 11.2 Function Calling: Giving LLMs Tools to Act on the World · 11.4 ReAct Agents: Interleaving Reasoning and Acting →