How Does an AI Agent Actually Work, Step by Step?
An AI agent works in a loop: the model picks a step, a tool runs it, the result comes back, and the model decides what to do next. Here is each step.
By 8MB Tech Published 7 min read
The short answer
An AI agent works in a loop. A language model reads the task and decides on a step, usually a tool call. Your software runs that tool and sends the result back. The model reads it, decides the next step, and repeats until it gives a final answer or hits a limit.
Think about how a good mechanic finds a fault in your car. They do not guess the answer in one go. They listen to the engine, form a theory, test one thing, look at what happened, and let that result decide the next test. An AI agent works in much the same way, and once you see the pattern you can read almost any agent product with confidence.
How does an AI agent work, in one sentence?
An AI agent works in a loop: a language model decides on a step, a tool carries it out, the result goes back to the model, and the model decides what to do next. It repeats until the task is done or a limit stops it.
Anthropic puts it plainly in its post “Building effective agents” (December 2024). A note on that page now says much of the tooling it describes has changed since then, so we use only its description of the loop. It says agents “are typically just LLMs (large language models, the AI models that read and write text) using tools based on environmental feedback in a loop.” OpenAI’s guide says the same thing from the engineering side: “This concept of a while loop is central to the functioning of an agent.” A while loop is a programming term for “keep repeating this until a condition says stop”.
If you have not read the first post, What Is an AI Agent, and How Is It Different from a Chatbot? defines the terms used here, including LLM (a language model that reads and writes text) and tool (a function or service the model can ask to use).
What are the steps an AI agent follows?
There are six steps, and steps two to five repeat.
- The goal arrives. A person gives the agent a task. Anthropic says agents begin with “a command from, or interactive discussion with, the human user”.
- The model decides. The model reads everything it has been given so far, which is called its context: the task, its instructions, the list of tools it may use, and the results of earlier steps. It chooses either a tool to call or a final answer.
- The model requests a tool call. A tool call is a structured request that names a tool and the inputs to use, such as “look up order 1042”. The model only writes this request.
- The software runs the tool. Ordinary code outside the model carries out the request. As of October 2026, Anthropic’s documentation is explicit: “The model never executes anything on its own.” Your application, or for some tools Anthropic’s servers, runs the operation and returns the outcome.
- The result comes back as an observation. An observation is whatever the tool returned: a record, a web page, an error message, a test result. It is added to the context. Anthropic says it is crucial for agents “to gain ‘ground truth’ from the environment at each step”, meaning real results rather than guesses.
- The loop ends. When the model replies with an answer instead of another tool call, the loop stops. As of October 2026, Anthropic’s tool use documentation describes the same pattern: keep going while the model asks for a tool, and exit on any other stop reason, such as the model finishing its turn.
The model does not remember the previous steps the way a person does. It sees them again each time, because the software puts the results back into its context before the next decision. That is why a long task needs careful handling of context, which we cover in a later post on memory.
Where did this idea come from?
The think, act, observe pattern was written up in a 2022 research paper called ReAct, by Shunyu Yao and colleagues. The paper, first posted to arXiv in October 2022 and published at ICLR 2023, showed language models producing “reasoning traces and task-specific actions in an interleaved manner”.
In the paper’s terms, a thought is a piece of reasoning in plain language that does not affect the outside world. An action does. In the question-answering experiments the model could search a simplified Wikipedia interface, look up a term on a page, and finish with an answer, and each search result shaped its next thought. The authors report that, by interacting with a simple Wikipedia API, ReAct overcame problems of hallucination (confidently stating things that are false) and error propagation that they saw in reasoning without actions.
The paper steered the model with example prompts. Modern tool-calling interfaces, like the one in Anthropic’s documentation, use structured requests instead, but the round trip is the same: the model asks, software acts, and the result comes back.
What does the loop look like on a real task?
Here is a worked example, using the fabric shop from the first post.
Illustrative example. Imagine an agent for a small Lagos fabric shop. The tool names below are made up for this example. The owner asks: “Has order 1042 been delivered?”
- Thought: I need the order record to find the tracking number. (Model decides.)
- Action:
lookup_order(1042)(Tool call.) - Observation: Order 1042, shipped last week, tracking code ready. (Result returns.)
- Thought: I should check where the parcel is now.
- Action:
track_parcel(code) - Observation: Held at depot, delivery address incomplete.
- Thought: I cannot fix an incomplete address alone. I should draft a message to the customer and ask the owner before sending it.
- Final answer: “Not delivered. The courier is holding it because the address is incomplete. Here is a draft message to the customer. Shall I send it?”
Three decisions, two tool calls, and one handover to a person. Nobody wrote a script for “address incomplete”. The model chose the steps because of what each observation said.
How does an agent know when to stop?
An agent stops when the model gives a final answer instead of asking for another tool, or when a built-in limit is reached. OpenAI’s guide lists common exit conditions: tool calls, a certain structured output, errors, or reaching a maximum number of turns.
As of October 2026, in OpenAI’s Agents SDK (a software toolkit for building agents), for example, a run keeps looping over the model until either a final-output tool is called or the model returns a response with no tool calls. Anthropic adds that agents can “pause for human feedback at checkpoints or when encountering blockers”, and that “it’s also common to include stopping conditions (such as a maximum number of iterations) to maintain control.” In the example above, the agent stopped to ask before sending anything to a customer. That kind of checkpoint is a design choice, and it is one way to keep control of what the agent does.
Why can the loop go wrong?
Every extra turn of the loop costs time and money, and a wrong turn early on can spread. Anthropic notes that “Agentic systems often trade latency and cost for better task performance” and that the autonomy of agents means “higher costs, and the potential for compounding errors.” As of October 2026, its tool use documentation adds that every tool call is at least one extra round trip, meaning one more request sent and answer received.
This is why agent designs add limits, human checkpoints, and checks on real results at each step.
What to remember
- An agent runs a loop: the model decides, a tool acts, the result comes back as an observation, and the model decides again.
- The model only asks for actions. Ordinary software runs the tools, and real results from the environment keep the agent grounded.
- The loop ends on a final answer or a limit, and good designs add human checkpoints for anything risky.
Next in the series: what AI agents can do well today, and where they still fail.
Frequently asked questions
What is the agent loop?
The agent loop is the repeated cycle at the heart of an agent. The model chooses an action, the surrounding software carries it out, and the result goes back to the model as new information. OpenAI's guide calls this idea of a while loop central to how an agent functions.
Does the AI model run the tools itself?
No. Anthropic's documentation says the model never executes anything on its own. It emits a structured request, and your code or Anthropic's servers run the operation and return the result to the conversation. The model decides which tool to call and with what inputs; ordinary software does the work.
How does an AI agent know when to stop?
An agent stops when the model gives a final answer without asking for another tool, or when a limit is reached. Anthropic notes that agents commonly include stopping conditions such as a maximum number of iterations, and can pause for human feedback at checkpoints or when they hit a blocker.
What is ReAct?
ReAct is a method from a 2022 research paper by Shunyu Yao and colleagues. It has a language model alternate between written reasoning steps and actions, such as searching Wikipedia, so that what it finds can shape what it thinks next. The paper describes a thought, action, and observation pattern for solving tasks.
Sources
- Building effective agents Anthropic · Accessed
- A practical guide to building agents OpenAI · Accessed
- ReAct: Synergizing Reasoning and Acting in Language Models Yao et al., arXiv (ICLR 2023) · Accessed
- How tool use works Anthropic (Claude Developer Platform documentation) · Accessed