Course contentsShow
← AI Engineering
Lesson 622 of 1,88615. AI Agents - ArchitectureFree lesson

Stopping Conditions: Max Iterations

Prevent infinite loops by limiting the number of reasoning steps or tool calls an agent can make.

Stopping Conditions: Max Iterations

What you'll learn: How to prevent AI agents from running indefinitely by capping the number of reasoning steps they can take.

Why Limit Iterations?

Without guardrails, an AI agent could theoretically loop forever—continuously reasoning, calling tools, and taking actions without ever reaching a conclusion. Imagine an agent stuck trying to solve an impossible problem or caught in a circular reasoning pattern. Setting a maximum iteration limit is your safety valve.

The Core Concept

A max iteration limit sets a hard cap on how many times your agent can execute its perception-reasoning-action loop. Think of it like giving someone a maximum number of turns in a board game—once they've used all their turns, the game stops, whether they've won or not.

Here's what a single iteration typically includes:

  • Observing the current state
  • Reasoning about what to do next
  • Taking an action (like calling a tool or generating output)

If the agent reaches your limit (say, 10 iterations) without naturally terminating, you force it to stop.

When to Use Max Iterations

This stopping condition is essential when:

  • The agent might encounter unsolvable problems
  • You're working with limited compute budgets
  • You need predictable execution times
  • Testing and debugging new agent architectures

Setting Appropriate Limits

Too low (e.g., 3 iterations): Agent might not have time to complete complex tasks.
Too high (e.g., 100 iterations): Risk wasting resources on stuck agents.

A reasonable starting point is 5-15 iterations for most tasks, adjusting based on task complexity and your observations from agent state logs.

Key Takeaway: Max iterations prevent infinite loops by enforcing a hard limit on how many perception-reasoning-action cycles an agent can execute, balancing task completion with resource protection.