Hello! Welcome back to our course on agent development.
In our last lesson, we explored several real-world use cases, seeing how agents' unique characteristics—autonomy, goal-directedness, and adaptability—allow them to orchestrate complex business processes and create personalized user experiences that are out of reach for traditional automation.
Today, we're going to open the hood and look at the engine that powers this dynamic behavior. This lesson will introduce the most fundamental concept in agent architecture: the agent loop. By the end of our session, you'll be able to describe this core cycle of perception, reasoning, and action and understand its central role in any agentic system. This is the foundational pattern upon which all other agent capabilities, like tool use and memory, are built.
The Core Cycle: Perceive, Reason, Act
At its heart, an agent operates in a continuous cycle. It's not a one-and-done execution like a script; it's a persistent process that constantly interacts with its environment to move toward a goal. This cycle is universally known as the agent loop.
The simplest model of this loop has three phases:
- Perception: The agent observes its environment and gathers information. This includes the initial user request, data from external systems, and the results of its own previous actions.
- Reasoning (or Planning): The agent "thinks." It takes the information it perceived and decides on the best next step to take to make progress toward its ultimate goal. This is the step where the Large Language Model (LLM) plays a central role.
- Action: The agent executes the chosen step. This might be responding to the user, calling an external API, querying a database, or running a piece of code.
This entire process is cyclical. The outcome of the action changes the state of the environment, which the agent then perceives in the next iteration of the loop, starting the cycle all over again.

To hear this explained, let's watch a short segment from a lecture on agentic design patterns.
AI Agents 3 - Agentic Design Patterns
This clip from 'AI Agents 3 - Agentic Design Patterns' by Prof. Ghassemi Lectures and Tutorials provides a clear, concise definition of an AI agent and its core Perception, Planning (Reasoning), and Action loop.
Watch from 00:35 to 02:25. Focus on the breakdown of an agent into three components (LLM, tools, memory) and the three activities it performs to achieve a goal (perception, planning, action).
As the video explains, this loop is the mechanism that integrates the LLM's intelligence with external tools and memory to achieve a goal within an environment.
The Agent Loop as a Programming Construct
This cycle of "perceive, reason, act" might sound abstract, but for a software engineer, the most direct analogy is a familiar one: a while loop. The canonical architecture for many powerful agents is, at its core, a loop that continues until a task is complete.
The following article provides a brilliantly simple illustration of this idea.
The canonical agent architecture: A while loop with tools
The article 'The canonical agent architecture: A while loop with tools' from the Braintrust blog demystifies the agent loop by showing its direct implementation as a simple code structure.
Please read the first few paragraphs, up to and including the TypeScript code snippet. Pay close attention to how the while loop structure directly maps to the agent loop concept.
Let's break down that TypeScript snippet, as it's a perfect mental model:
while (!done) {
// Loop until the goal is met.
const response = await callLLM();
// REASONING phase: Pass the current state (messages) to the LLM to get a decision.
messages.push(response);
// PERCEPTION for the next turn: Update state with the LLM's thought process.
if (response.toolCalls) {
// The LLM decided to take an ACTION using a tool.
messages.push( ...(await Promise.all(response.toolCalls.map((tc) => tool(tc.args)))), );
// ACTION phase: Execute the tool and perceive the result by pushing it to messages.
} else {
// The LLM decided the next action is to wait for more input.
messages.push(getUserMessage());
// PERCEPTION phase: Get new input from the user.
}
}
Seeing the agent loop as a while loop helps ground it in familiar programming patterns. In fact, many Agent Development Kits (ADKs) provide a function that encapsulates this very loop.
OpenAI + @Temporalio : Building Durable, Production Ready Agents - Cornelia Davis, Temporal
In this presentation on the OpenAI Agents SDK, Cornelia Davis of Temporal makes this exact connection. She introduces the concept of the agentic loop and shows how it's represented by a single function call in the SDK.
Watch from 04:55 to 07:41. Notice how she defines an agent as a system where the LLM decides the application flow, and points to runner.run as the embodiment of the 'agentic loop'.
The key insight here is that the agent loop is the mechanism that grants the LLM "agency"—the power to direct the flow of the application.
A Deeper Look: The Five Phases of a Production Loop
The simple "Perceive, Reason, Act" model is a great start. However, to build robust, production-ready agents, it's helpful to expand this into a more detailed, five-phase cycle: Perceive, Reason, Act, Observe, and Update.
This more granular view makes the feedback mechanism explicit, which is critical for an agent's ability to adapt and correct its course.
Agent Loops: The Foundation of Multi-Agent Ecosystems
The article 'Agent Loops: The Foundation of Multi-Agent Ecosystems' provides an excellent breakdown of this more detailed, five-phase loop and its step-by-step flow.
First, read the section 'What Are Agent Loops?' to understand the five phases. Then, read 'The Step-by-Step Flow Inside an Agent Loop' for a detailed walkthrough of how these phases operate in sequence.
Let's summarize how these five phases map back to our while loop model:
- Perceive: The loop starts by gathering the current context (
messages). This is the agent's understanding of the world at this moment. - Reason: The
callLLM()function is executed. The LLM analyzes the context and plans the next step. - Act: If the LLM's plan involves a
toolCall, thetool(tc.args)function is executed. This is the agent interacting with the outside world. - Observe: This is an implicit but crucial step. The code awaits the result of the tool call. It observes whether the action succeeded, failed, or what data it returned.
- Update: The result from the observation is pushed back into the
messagesarray. This updates the agent's state with new information, ensuring the next iteration of the loop is more informed than the last.
This explicit cycle of acting, observing the result, and updating its internal state is what allows an agent to do things like retry a failed API call, or realize it needs to ask the user a clarifying question before proceeding.
Test your understanding!
Imagine an agent whose goal is to book a flight. In its first turn, it calls a flight search tool with the user's request ("a cheap flight to NYC tomorrow"). The tool returns an error: "Date is in the past."
How would the five phases of the agent loop (Perceive, Reason, Act, Observe, Update) handle this situation in the next turn?
Show answer
- Perceive: The agent's context now includes the original request and the error message from the tool.
- Reason: The LLM analyzes this new context. It understands that "tomorrow" has become "today" or "yesterday" and that the request failed. It reasons that it needs a valid, future date.
- Act: Based on its reasoning, the agent's action is to generate a response to the user, like: "It looks like the date you requested is now in the past. What is a new date you'd like me to search for?"
- Observe: The agent observes that its message was successfully sent to the user.
- Update: The agent adds its own question to the conversation history and now waits for the user's response, which will be the input for the next perception phase.
Conclusion
In this lesson, we dissected the core engine of every AI agent: the agent loop. This is the fundamental pattern that enables the dynamic, goal-oriented behavior we discussed in previous lessons.
Key Takeaways:
- The agent loop is a continuous cycle that allows an agent to perceive its environment, reason about its next step, and take action to achieve a goal.
- For a developer, this abstract concept is most practically understood as a
whileloop that orchestrates calls to an LLM and external tools. - A more robust model of the loop includes explicit Observe and Update phases, where the agent evaluates the outcome of its actions and incorporates that new knowledge into its state for the next turn. This feedback mechanism is the key to adaptability.
We've established that the "Action" phase of the loop is where the agent interacts with the world, often through "tools." In our next lesson, we will focus entirely on this crucial component. We'll explore what tools are, how they connect an LLM to external systems like APIs and databases, and why they are the key to building agents that can perform meaningful, real-world tasks.