Every AI coding agent, research assistant, or autonomous tool follows the same core pattern. Whether it is writing code, searching files, calling APIs, or running tests, the agent moves through a loop:
Plan → Act → Observe → Evaluate → Repeat
This tutorial explains exactly how that loop works, why failure handling matters, and how you can build a safe simulation yourself.
Why the Agent Loop Matters
Without a loop, an LLM is a one-shot system:
User → LLM → Response
With a loop (agent):
User → Plan → Act → Observe → Evaluate → Plan → Act → Observe → Evaluate → ... → Done
The loop allows the system to:
- Adapt — if the first tool call fails, try a different approach
- Chain actions — use the output of one step as input to the next
- Self-correct — detect errors and recover
- Complete multi-step tasks — tasks that require 5, 10, or 50 actions
- Stop at the right time — not just execute forever, but know when the goal is reached
The loop is what separates a tool from an agent.
The Five-Step Loop in Detail
Each step in the loop has a specific responsibility:
Step 1: 🎯 Goal
The user provides a goal. This can be a natural language request, a ticket description, or an instruction from a higher-level system. The goal defines what "done" means.
Example: "Calculate 2 + 3 * 4 and read the README file."
Step 2: 📋 Plan
The LLM analyzes the goal and decides what to do first. It selects a tool and constructs arguments. This is the reasoning step.
Example: "First, I'll use the calculator tool with the expression '2 + 3 * 4'."
Step 3: ⚡ Act
The system executes the tool call. The tool receives the arguments and runs its logic. This is where real work happens — computation, file I/O, API calls, database queries.
Example: The calculator tool parses "2 + 3 * 4" and returns 14.
Step 4: 👁️ Observe
The agent reads the tool's result. This observation becomes part of the conversation context for the next planning step. The agent now knows what happened.
Example: "The calculator returned 14."
Step 5: ✅ Evaluate
The agent decides: is the goal complete? If yes, produce the final answer. If not, plan the next step. This decision point is what makes the system autonomous — it can choose to continue or stop.
Example: "I've calculated 14, but I still need to read the README. Plan next step."
The Complete Architecture
┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
│ GOAL │───▶│ PLAN │───▶│ ACT │───▶│ OBSERVE │
│ (user) │ │(decide) │ │ (tool) │ │(result) │
└──────────┘ └──────────┘ └──────────┘ └────┬─────┘
│
▼
┌──────────────────────────────────┐
│ EVALUATE │
│ (done? or plan next step) │
└──────────┬───────────────────────┘
│
◀─────────┘
(loop back to Plan)
Components:
• LLM — plans and evaluates
• Tool Registry — available actions
• Tool Executor — runs tool calls safely
• State — conversation history + observations
• Loop Controller — manages iterations and limits
What Happens Inside Each Step
Let us trace through a concrete example. The goal is: "Find all Python files that contain TODO comments."
| Step | Input | Action | Output |
|---|---|---|---|
| Plan | Goal: find TODOs | Select file_search tool | file_search(pattern="TODO", glob="*.py") |
| Act | Tool call | Search filesystem | Found in 3 files: app.py, utils.py, tests.py |
| Observe | Tool result | Record findings | 3 TODO items in context |
| Evaluate | Goal status | Search complete? | Yes — return results |
Now a more complex goal: "Calculate 14, search for config files, then read the README."
| Iteration | Plan | Tool | Observe | Evaluate |
|---|---|---|---|---|
| 1 | Calculate first | calculator(2 + 3 * 4) | 14 | More to do → next |
| 2 | Search for config | file_search(config) | No matches | More to do → next |
| 3 | Read README | read_file(readme.txt) | File content... | All done → final answer |
The agent ran 3 iterations, used 3 different tools, and stopped when the goal was satisfied.
Failure Handling: What Happens When Things Go Wrong
Real agents encounter failures. A tool might fail, a file might not exist, an API might timeout. Good agent loops handle failures gracefully.
Strategy 1: Recover and Retry
When a tool call fails, the agent observes the error and plans a different approach:
Iteration 2: plan → calculator("2 + 2") → ✅ Result: 4
Iteration 3: plan → read_file("readme.txt") → ✅ File content...
The agent detected the error, adapted, and continued. This is resilience.
Strategy 2: Abort After Too Many Failures
If an agent keeps failing, it should stop rather than loop forever:
Iteration 2: ❌ Error
Iteration 3: ❌ Error
→ ABORT — 3 consecutive failures, stopping loop
The max consecutive failures limit prevents infinite loops and runaway costs.
Strategy 3: Max Iterations
Every agent loop needs an iteration cap. Even if the agent wants to continue, it stops after a predefined limit:
Python Demo: Build Your Own Agent Loop
Here is a complete, safe agent loop you can run locally. It uses mock tools — no filesystem access, no network, no real LLM. It demonstrates the architecture.
demo.py and run python demo.py to see the agent loop in action. Run python demo.py --test to verify all 15 test cases.
Key Components of an Agent Loop
| Component | Role | Example |
|---|---|---|
| Goal | What the agent should achieve | "Fix the failing test in test_login.py" |
| Plan | Which tool to call and with what arguments | read_file("test_login.py") |
| Tool | Executes an action in the environment | File reader, calculator, Git command |
| Observation | The result of executing the tool | File contents, error message, computed value |
| Evaluate | Decision: continue, complete, or abort | "Got the file — now search for the bug" |
| State | Accumulated history of all steps | Previous plans, actions, and results |
| Limits | Safety bounds on the loop | Max 20 iterations, max 3 consecutive failures |
How Real Agents Use the Loop
In production AI coding agents, the same loop applies but with real tools:
The agent might take 10-30 iterations for a real task. Each iteration follows the same Plan → Act → Observe → Evaluate cycle.
State and Context Management
At each iteration, the agent's LLM sees the accumulated state:
- Original goal
- Instructions (system prompt)
- Available tools and their descriptions
- History of all previous plans, actions, and observations
- Current step number
This context grows with each iteration. Context management is crucial — too much history wastes tokens, too little causes the agent to repeat itself.
| Context Strategy | Pros | Cons |
|---|---|---|
| Full history | Complete information | Token-heavy, slow |
| Sliding window | Fixed token count | Loses early context |
| Summary + recent | Balanced | Summary may miss details |
| Tool-specific | Relevant context only | Complex to implement |
Common Failure Patterns
| Pattern | Cause | Solution |
|---|---|---|
| Infinite loop | No iteration limit | Set max_iterations |
| Repeated failures | Agent retries same failed action | Track consecutive failures, abort |
| Goal drift | Agent loses track of original goal | Include goal in every context |
| Context overflow | History grows too large | Summarize or truncate old steps |
| Overthinking | Agent plans but never acts | Enforce max planning steps |
| Under-planning | Agent acts without reasoning | Require explicit plan before action |
Human-in-the-Loop Integration
Production agent loops often include a human approval step for high-risk actions:
When You Need an Agent Loop
| Task Type | Needs Loop? | Why |
|---|---|---|
| Answer a question | No | One-shot response is sufficient |
| Generate code from spec | Maybe | Single generation might work |
| Fix a failing test | Yes | Must read → fix → test → verify |
| Multi-file refactoring | Yes | Many sequential file operations |
| Debug a complex issue | Yes | Hypothesis → test → refine |
| Build a feature end-to-end | Yes | Plan → implement → test → commit |
Agent Loop vs Simple Prompting
| Aspect | Simple Prompting | Agent Loop |
|---|---|---|
| Interactions | 1 | Many |
| Tool access | Optional (function calling) | Core capability |
| Failure recovery | Manual retry | Automatic adaptation |
| Complexity | Simple tasks | Multi-step workflows |
| Cost | Single API call | Multiple iterations |
| Autonomy | None | High (within limits) |
| Human effort | Manual orchestration | Review and approve |
Practical Exercises
Exercise 1: Trace the Loop
Read through the demo output. For each step, identify which part of the loop (Plan, Act, Observe, Evaluate) it corresponds to. Count how many iterations it took.
Exercise 2: Add a Tool
Add a new tool called word_count that counts words in a string. Modify the agent to use it in the loop. What happens if the tool returns an error?
Exercise 3: Handle Failures
Modify the max_consecutive_failures to 1. Run the demo. What changes? Why is having a higher threshold useful?
Exercise 4: Build a Production Loop
Extend the demo to include a risk-level check. Some tools should require "approval" before execution. How would you implement this?
✅ Agent Loop Implementation Checklist
- ☐ Max iterations defined (prevent infinite loops)
- ☐ Max consecutive failures defined (abort safety)
- ☐ Goal included in every planning step (prevent drift)
- ☐ Tool results validated before passing to LLM
- ☐ Human approval for high-risk actions
- ☐ Error handling for each tool
- ☐ State/history management strategy chosen
- ☐ Logging of each step for debugging
- ☐ Final answer validated before returning to user
FAQ
Q: How many iterations does a typical agent take?
A: Simple tasks (1-3 tools): 2-5 iterations. Complex tasks (code changes + tests): 10-30 iterations. Very complex tasks: 50+ iterations.
Q: Can an agent loop run forever?
A: Without limits, yes. Always set max_iterations and max_consecutive_failures. This is a safety requirement, not optional.
Q: What happens when the LLM does not select a tool?
A: The loop should treat "no tool selected" as the agent deciding to produce a final answer. The Evaluate step should detect this.
Q: How is state managed between iterations?
A: Each iteration appends its plan, action, and observation to a history list. The LLM receives the full (or summarized) history in its context window for the next planning step.
Q: Does the loop always converge?
A: Not guaranteed. The agent might oscillate between approaches, retry failed actions, or lose track of the goal. Safety limits handle these cases.
Q: Can multiple agents share a loop?
A: Yes — this is called multi-agent orchestration. A planner agent might delegate to a coder agent, which delegates to a tester agent, each running their own loop.
Further Reading
- How AI Coding Agents Actually Work: Models, Tools, Context and Execution
- AI Coding Agents Explained: From Code Completion to Autonomous Development
- AI Pair Programming vs Agentic Programming
- AI Agents and Software Architecture
- How to Use AI Coding Agents Safely
- How to Benchmark AI Coding Agents Fairly
- Context Engineering Explained
Continue Learning: Understand the full agent architecture, explore how agents evolved, learn to use agents safely, and discover what developers still need to know.
Discuss this topic on BestWordz Community.