The agent loop
Under the hood, every AI agent runs the same repeating cycle — called the agent loop. Each iteration moves the agent one step closer to completing its goal.
The loop has five stages:
Step 1 — Perceive
The agent receives input. This could be a user's goal, the result of a previous action, a file, a web page, an API response — anything in its context window.
Example: You tell the agent "research the top 5 Python web frameworks and summarise them." The agent now has its goal.
Step 2 — Think
The LLM at the agent's core reasons about the current situation. It asks itself: what do I know, what do I still need, and what is the best next action?
This is where planning happens. The agent may break the goal into sub-tasks, decide which tool to use, or decide it already has enough information to answer.
Example: The agent decides: "I need to search the web. I will call the search tool with the query 'best Python web frameworks 2024'."
Step 3 — Act
The agent executes the chosen action by calling a tool. Tools are real functions — a web search API, a Python interpreter, a file writer, a database query, or a browser.
Example: The search tool runs and returns a list of links and snippets about Django, FastAPI, Flask, Tornado, and Starlette.
Key insight: The LLM does not directly browse the web or run code. It decides what to do; the tool does the actual work.
Step 4 — Observe
The tool's output is fed back into the agent's context. The agent now "sees" the result and can reason about whether it is useful, accurate, or complete.
Example: The agent reads the search results and determines it has enough to summarise Django and FastAPI but needs more detail on Flask.
Step 5 — Repeat or Stop
The agent decides: is the goal complete? If yes, it produces a final response. If no, it loops back to Think with the new information and picks the next action.
Example: The agent runs one more search for Flask, reads the results, then decides it has enough to write the full summary — and does so.
Why the loop matters
The loop is what separates an agent from a single LLM call. Without it, you get one answer. With it, you get an iterative problem-solver that adapts as it gathers new information.
Without a loop
One LLM call → one response. If the answer requires information the model does not have in its weights, it guesses or says it does not know.
With a loop
Many LLM calls, each building on the previous. The agent can gather real data, fix its own mistakes, and retry failed steps — until done.
When does the loop stop?
An agent exits the loop for one of three reasons:
- Goal reached — the agent has produced a final answer and signals it is done.
- Max steps reached — a safety limit prevents infinite loops. Most frameworks let you set a maximum number of iterations.
- Error or tool failure — if a tool fails repeatedly and the agent cannot recover, it stops and reports what went wrong.
The agent loop in pseudocode
If the five-step diagram above still feels abstract, here is what the loop looks like as code. Every agent framework implements some version of this — the differences are in how much the framework handles for you.
messages = [system_prompt, user_goal]
for step in range(max_steps):
response = llm.call(messages, tools=available_tools)
if response.has_tool_call:
result = execute_tool(response.tool_call)
messages.append(tool_call_message)
messages.append(tool_result_message(result))
else:
print(response.text) # final answer
break
That is the entire agent in 10 lines. The for loop is the
"repeat" step with a safety limit. The if check is the
"decide" step — did the LLM want to use a tool, or is it ready to answer? On
Page 7
you will write this as real, runnable Python.
Common variations of the loop
The basic perceive-think-act-observe loop is the foundation, but production agents often add variations to handle real-world complexity.
Parallel tool calls
Some models (GPT-4o, Claude) can request multiple tool calls in a single turn. Instead of searching once, waiting, then searching again, the agent fires three searches simultaneously. The loop stays the same — you just process multiple tool results before the next LLM call.
Self-correction
When a tool returns an error or unexpected result, a well-prompted agent will recognise the failure in the Observe step, reason about what went wrong in the Think step, and try a different approach. This self-correction capability is one of the main advantages agents have over single LLM calls.
Human checkpoints
In production, you often want a human to approve certain actions before the agent executes them — sending an email, modifying a database, or deploying code. The loop pauses at the Act step, shows the proposed action to a human, and resumes only after approval. This is the human-in-the-loop pattern that AutoGen and LangGraph support natively.
Multi-agent handoffs
Instead of one agent running the full loop, the Act step can hand control to a different agent specialised for a sub-task. A research agent gathers data, then hands off to a writing agent to produce the report. Each agent runs its own loop, and the parent coordinates.
How AI Agents Work FAQ
What is the AI agent loop?
The agent loop is the repeating cycle every AI agent runs: perceive input, think about what to do, act by calling a tool, observe the result, then repeat until the goal is reached or a stop condition is met.
How is an AI agent different from a chatbot?
A chatbot makes a single LLM call and returns one response. An AI agent runs a loop — it can call tools, gather real data, fix its own mistakes, and retry failed steps across multiple iterations until the task is done.
When does the agent loop stop?
The loop stops for one of three reasons: the goal is reached and the agent produces a final answer, a maximum step limit is hit to prevent infinite loops, or a tool fails repeatedly and the agent cannot recover.
What does the Think step do in the agent loop?
During the Think step, the LLM reasons about the current situation — what it knows, what it still needs, and the best next action. This is where planning happens, including breaking goals into sub-tasks.
Does the LLM directly browse the web or run code?
No. The LLM decides what to do and which tool to call, but it does not execute actions directly. Tools — separate functions for web search, code execution, file I/O, etc. — do the actual work.
Continue learning
Next up: dive into the four core components that make up every agent. When you are ready to write code, jump to build your first AI agent in Python. Or browse the full tutorials catalog for more topics.
Quick summary
- The agent loop: Perceive → Think → Act → Observe → Repeat or Stop
- The LLM reasons and decides; tools do the actual work
- Each iteration gives the agent new information to work with
- The loop stops when the goal is reached, a step limit is hit, or an error occurs