Skip to main content
AI Agents & Automation

⏱ About 15 min15 XP

Looping Until Done

The agent loop is powerful because it keeps going — but that very persistence raises an important question: how does the agent know when to stop? A loop without an exit condition runs forever, consuming resources and potentially causing harm. A loop that exits too early leaves the job unfinished. Getting the stopping condition right is one of the most important design decisions in any agent system.

Goal Satisfaction: The Natural Stopping Point

The most straightforward stopping condition is goal satisfaction: the agent stops when it has achieved what it was asked to do. After the observe stage, the agent asks itself a question — 'Has the goal been reached?' If yes, it exits the loop and delivers its result. If no, it runs another cycle. But goal satisfaction requires the agent to know what 'done' looks like. For some goals this is obvious. 'Write a haiku about autumn' is done when a haiku has been produced. 'Find the phone number of the nearest hospital' is done when a phone number has been found and verified. For vaguer goals — 'make the code better' or 'research climate change' — the agent needs a clearer definition of done, otherwise it may loop indefinitely because there is always more improvement or more information to find.

Goal Satisfaction

Goal satisfaction is the primary stopping condition: the agent exits the loop when it has completed the assigned task. Vague goals with no clear finish line are a common source of runaway agent loops.

Safety Limits: Loop Caps and Timeouts

Even well-designed agents can get stuck. A bug in the reasoning stage might cause the agent to choose the same failing action over and over. An ambiguous goal might make the agent keep searching without converging on an answer. External APIs might fail repeatedly, leaving the agent in a frustrated loop. This is why agent systems always include safety limits — hard boundaries that stop the loop even if the goal has not been reached. Common safety limits include a maximum number of loop iterations, a maximum total execution time, a maximum cost in tokens or compute resources, and a maximum number of tool calls. When a safety limit triggers, the agent should exit gracefully: report what it has accomplished so far, explain what stopped it, and give the user a chance to decide whether to continue, adjust the goal, or abandon the task.

Safety Limits

Safety limits — maximum iterations, timeouts, token budgets — are essential guardrails that prevent an agent from looping forever due to bugs, ambiguous goals, or external failures. All production agent systems have them.

Human-in-the-Loop: Checkpoints and Approvals

A third way the loop can stop — or pause — is when the agent reaches a checkpoint that requires human input. Not every stopping condition is automatic. Sometimes the agent needs clarification from the user before it can continue. Sometimes it has reached a decision point where taking the next action requires human approval — for example, before deleting files, sending a message to many people, or committing money. These human-in-the-loop checkpoints are not failures. They are intentional pauses that keep humans in control of high-stakes decisions while still benefiting from the agent's ability to handle the routine steps. A well-designed agent knows when to proceed on its own and when to pause and ask.

Human-in-the-Loop

A human-in-the-loop checkpoint pauses the agent loop and requests human input or approval before proceeding. This is a deliberate design pattern, not an error — it keeps humans in control of consequential decisions.

Match each stopping condition to the situation that triggers it.

Terms

Goal satisfaction
Maximum iteration limit
Timeout
Human-in-the-loop checkpoint

Definitions

The agent confirms it has produced the answer or output the user requested
The agent is about to send an email to 500 people and pauses to request user approval
The agent has run 50 loop cycles without finishing and the hard cap is reached
The task has been running for 10 minutes and the system cuts it off to free resources

Drag terms onto their definitions, or click a term then click a definition to match.

Why do agent systems include maximum iteration limits even when the agent has a clear goal?

A user asks an agent to 'make the code as good as possible.' Why is this a problematic goal for stopping condition design?

Design the Exit Conditions

  1. Step 1: You are building a 'travel booking agent' that helps users find and book flights. The goal given to the agent is: 'Find me a good flight from New York to Tokyo in December.'
  2. Step 2: Write a clear goal satisfaction condition — exactly what must be true for the agent to consider the task done?
  3. Step 3: Design three safety limits: an iteration cap, a timeout, and a cost limit. Explain the number you chose for each.
  4. Step 4: Identify one decision point where the agent should pause and require human approval before continuing. What makes this decision high-stakes enough to require a human?
  5. Step 5: Rewrite the original vague goal as a precise goal that is easier to build a stopping condition around.