Skip to main content
Early access — new tools and guides added regularly
Practical

Agent Loop

Last reviewed: April 2026

The repeating cycle an AI agent follows: plan what to do, execute a step using tools, observe the result, decide whether to continue or adjust.

The agent loop is the core execution pattern behind every AI agent. It is a repeating cycle of four steps: plan, act, observe, decide. The agent continues looping until the goal is achieved or a stopping condition is met.

The four steps

  1. Plan: The agent looks at the current goal and decides what step to take next. For a fresh task, this means decomposing the goal into steps. Mid-task, this means deciding the next action based on results so far.
  1. Act: The agent executes the planned step using its available tools — searching the web, writing a file, running code, calling an API, or generating text.
  1. Observe: The agent examines the result of its action. Did the search return useful results? Did the code run without errors? Is the generated text relevant to the goal?
  1. Decide: Based on the observation, the agent makes a choice:

Why the loop matters

The agent loop is what separates agents from simple prompt-response AI. A chatbot processes your input once and gives one response. An agent processes, evaluates, and iterates — potentially dozens of times — until the task is complete.

This self-correction capability is what makes agents reliable enough for real work. If a web search returns irrelevant results, the agent can refine the query. If generated code fails a test, the agent can debug and fix it. If a draft does not meet requirements, the agent can revise it.

Loop limits

Most agent implementations include safeguards to prevent infinite loops: - Maximum iterations: The agent stops after N cycles regardless of completion (typically 10-50 for most tasks) - Token budgets: The agent stops when it has used a defined number of tokens - Time limits: The agent stops after a set duration - Human checkpoints: The agent pauses for human approval after every N steps

In practice

When Claude Code handles a task like "fix the failing test in auth.ts," it runs the agent loop: read the test file (act), identify the failure (observe), plan a fix (plan), edit the file (act), run the test again (observe), check if it passes (decide). If the test still fails, it loops again with a different approach.

Want to go deeper?
This topic is covered in our Advanced level. Unlock all 52 lessons free.

Why This Matters

Understanding the agent loop helps you design, debug, and optimise AI agents. When an agent produces poor results, the fix usually lies in one of the four steps: was the planning wrong? Was the tool use incorrect? Did the observation miss something? Did the decision logic fail? The loop gives you a framework for diagnosing agent behaviour systematically.

Related Terms

Learn More

Continue learning in Advanced

This topic is covered in our lesson: Building Your First AI Agent from Scratch