
Lesson 2: The Agent Loop
Deconstruct the internal physics of autonomous AI. Master the lifecycle of the Agent Loop: Perceive, Plan, Act, and Reflect, and learn how to optimize each phase for reliability.
Module 2: Foundations of Agentic AI Systems
Lesson 2: The Agent Loop: Perceive → Plan → Act → Reflect
If you look inside any agentic system—be it a simple Python script using LangGraph or a complex enterprise orchestrator—you will find a Cyclical Control Flow. This is the Agent Loop.
In this lesson, we will deconstruct the four phases of the loop. Understanding these phases is critical because most agent failures can be traced back to a specific breakdown in one of these four steps.
1. Phase 1: Perceive (Observation)
Before an agent can act, it must understand its environment. In the "Standard Chat" world, the environment is just the History of the conversation. In an "Architectural" world, the environment includes:
- File contents.
- Database schemas.
- API response codes.
- Real-time user input.
The Architect's Challenge: Only provide the "Signals," not the "Noise." If an agent "Perceives" too much irrelevant data, it will hallucinate.
2. Phase 2: Plan (Selection)
Once the agent knows the state of the world, it must decide what to do next. It maps the current state to a Task Step.
Two Types of Planning:
- Monolithic Planning: The agent creates a massive 10-step list and tries to follow it. (Brittle, high failure rate).
- Just-in-Time (JIT) Planning: The agent decides only the next step based on the result of the previous one. (Robust, the "Architect's Choice").
3. Phase 3: Act (Execution)
This is the phase where the agent interacts with a Tool.
- It formats a JSON call.
- It waits for the response.
- It hands the result back to the system.
In this phase, the logic of Module 4 (Tool Design) is paramount. If the tool interface is confusing, the "Act" phase will crash the loop.
4. Phase 4: Reflect (Feedback)
This is the most important and most neglected phase. After acting, the agent must ask: "Did that work?"
- If it called a search tool and got "No results found," it shouldn't just give up. It should "Reflect" on why no results were found (perhaps the query was too specific) and repeat the loop with a broader query.
5. Visualizing the Loop
graph LR
P[Perceive] --> PL[Plan]
PL --> A[Act]
A --> R[Reflect]
R -->|Goal not met| P
R -->|Goal met| E[Exit/Final Answer]
Critical Exam Concept: "Loop Exhaustion"
Scenario questions will often describe an agent that "gets stuck." This is usually a breakdown in the Reflect phase. If the agent doesn't have a specific instruction on how to handle failure, it will repeat the exact same "Plan" and "Act" ad infinitum.
6. Summary
The Agent Loop is the "Instruction Cycle" of the AI age.
- Perceive the state.
- Plan the next move.
- Act using a tool.
- Reflect on the result.
In the next lesson, we will look at how the agent "Remembers" its progress through these cycles: Stateless vs. Stateful Agents.
Interactive Quiz
- Explain the "Reflect" phase using a real-world example (e.g., getting a 404 error from an API).
- Why is "Just-in-Time" planning more robust than "Monolithic" planning?
- What happens to an agent system that lacks a "Perceive" phase (e.g., has no way to see tool results)?
- True or False: Reflection is only necessary if an action fails.
Reference Video: