promptgarden ๐ŸŒฑ
๐ŸŒ ES
Conceptโ—โ—โ—‹4 min ยท +40 XP

The Agent Loop: Think โ†’ Act โ†’ Check โ†’ Repeat

The agent loop is the cycle an AI agent uses to work through a task step by step.

The four steps

An AI agent typically works in a loop:

  1. Think: What's the next sensible step toward this goal?
  2. Act: Call a tool (read a file, run code, hit an API)
  3. Check: Look at the result โ€” did it work? Any new information?
  4. Repeat: Back to step 1, until the goal is reached or the loop is stopped

Why a loop instead of one step?

Because real tasks are rarely solved in one shot. A bug fix might need: read the file, understand the error, propose a change, run tests, check the result, adjust. Each round produces new information that shapes the next decision.

An example

Goal: "Fix the failing test." Round 1: the agent reads the error message. Round 2: it opens the affected file. Round 3: it changes a line. Round 4: it reruns the test. Round 5: test passes โ€” done.

The danger: endless loops

Without limits, an agent can get stuck โ€” retrying the same wrong fix forever, or racking up cost. That's why good agents need stop conditions: a maximum number of rounds, a budget cap, or a clear success condition.

EXAMPLE

Goal: 'Get all tests passing.' Loop: run tests โ†’ read failures โ†’ change code โ†’ rerun tests โ†’ repeat until green or limit reached.

QUICK QUIZ

Why do agents work in a loop instead of a single step?

SOURCES

RELATED TOPICS

What Is an AI Agent? (vs. a Chatbot) โ—โ—‹โ—‹Loops for Agents: Letting AI Iterate Autonomously โ—โ—โ—Context Window โ—โ—‹โ—‹