How Does an AI Agent Work? The Agent Loop Explained

How does an AI agent work? Follow the agent loop: the model proposes a step, software runs it, results feed back, and the cycle repeats until it stops.

An AI agent works by repeating one cycle until a task is finished. A language model, the AI that reads and writes text, proposes a next step. The software around it carries that step out, for example by running a search or editing a file, and hands the result back. The model uses that result to propose the following step. The cycle repeats until the job is done, a limit is reached, something breaks, or control goes back to a person.

That repeating cycle is called the agent loop. Anthropic describes agents as, typically, language models “using tools based on environmental feedback in a loop” [1], where a tool is any action the software lets the model request, such as a web search or a file edit. OpenAI’s guide to building agents describes the same idea as a “run”: a loop that continues until an exit condition is reached, such as a particular structured output, an error or a maximum number of turns [2]. BuildAIQ uses four labels to teach the cycle: Plan → Act → Observe → Adjust. The previous guide, on what "autonomous" means for an AI agent, asked how much an agent does without a person; this one shows the cycle each of those unsupervised turns runs through. If you are new to agents, start with what an AI agent is.

TL;DR

  • An agent loop repeats one cycle until a stop condition is met. The model chooses an action, a tool runs it, and the result is fed back as the next input [1, 2].
  • Plan → Act → Observe → Adjust is a teaching model, not a standard. ReAct calls the steps thought, action and observation; Anthropic’s Claude Code loop uses gather context, take action and verify work [3, 4].
  • The model asks; software acts. In common setups the model requests a tool call, and the surrounding application executes it and decides whether it is allowed [5, 6].
  • Every result becomes context for the next turn. That context grows, so agents need ways to summarize or set aside what they no longer need [6, 7].
  • Loops stop in four ways: the goal is met, a turn or budget limit is hit, an error occurs, or a person is asked to take over [2, 8, 9, 6].
  • People stay responsible. Whoever deploys an agent chooses its tools, permissions and limits, and answers for what it does.

Who Does What in the Loop

Three parts take turns in every cycle.

  • The language model produces text: either a request to use a tool or a final answer. It does not run anything itself [5].
  • The agent system around the model, often called the application or harness, holds the goal, the instructions, the list of available tools and the history of the task. It sends these to the model each turn, checks whether a requested action is allowed, enforces limits and keeps the loop going [5, 6].
  • The tools and connected software do the actual work, such as searching, reading a file or sending a message, and return a result [5].

Behind all three are people. The developers and organizations that build and deploy an agent decide which tools it can reach, which actions need approval and when it must stop, and they remain accountable for what it does. Knowing the loop tells you where to look when an agent gets something wrong: at what it was shown, what it was allowed to do, and when it was supposed to stop.

Plan → Act → Observe → Adjust Is a Teaching Model, Not a Standard

No framework uses exactly these four words. BuildAIQ uses them because they name what happens in each pass in plain language, but the published descriptions divide the cycle differently.

BuildAIQ stepReAct (research method)Anthropic’s Claude Code loopReflexion (research method)
PlanThought [3]Gather context [4]Actor, using stored reflections [10]
ActAction [3]Take action [4]Actor [10]
ObserveObservation [3]Tool results feed back [6]Evaluator scores the attempt [10]
AdjustNext thought [3]Verify work, then repeat [4]Self-Reflection writes feedback [10]

An academic survey of agents uses yet another scheme: separate profiling, memory, planning and action modules [11]. The labels differ, but the shape is shared. The model produces a step, something in the outside world responds, and that response shapes the next step.

The loop is what separates an agent from a fixed workflow. Anthropic distinguishes workflows, where models and tools follow predefined code paths, from agents, where the model directs its own process and tool use [1]. For the fuller comparison, see how chatbots, assistants, copilots, workflows and agents differ.

What Happens at Each Step

Plan, act, observe and adjust are the four moves of one operating cycle; carrying state forward, stopping and failing are what happen around them. Each step takes something in and hands something on. Tracking those inputs and outputs is the easiest way to follow what an agent is doing.

Plan: Choosing the Next Move

In: the goal, the system’s instructions, descriptions of the available tools, the history so far and the latest result [6]. Out: one next action, or a final answer.

In the systems described here, planning is the language model generating text about what to do next; some research systems also hand part of the planning to a separate planner program [12]. In ReAct, this text is called a “thought”: it changes nothing outside the model and produces no observation, but it becomes part of the context for the next step [3]. The ReAct authors found such reasoning helped the model create, track and update action plans and handle exceptions on their benchmark tasks [3]. OpenAI’s guide says an agent selects tools depending on the workflow’s current state [2].

Some agents write a full plan at the start, which needs a separate way to adjust or one bad step can sink the task; others plan a step or two at a time, which tolerates faults better but can drift from the goal over very long runs [12]. Another survey argues that a flawless plan is very hard to write up front [11]. Splitting a larger goal into smaller tasks is covered in the next guide.

Act: Requesting a Tool Call

In: the chosen action, expressed as a structured request to use a tool. Out: an effect, such as retrieved information, a file edited or a message sent.

The model does not usually run the tool itself. Anthropic’s documentation describes each tool call as a round trip: the model asks, the application executes, the application reports back, and the model continues [5]. That split matters for safety. In Anthropic’s Claude Agent SDK, as documented in November 2026, the model chooses which tools to call, but the developer controls whether those calls may run, and can intercept, modify or block a call before it executes [6]. Which tools agents use, and how this request-and-execute split works for search, browsing, code, messages and edits, is covered in how AI agents use tools.

Observe: Taking In the Result

In: whatever the tool returned: search results, file contents, an error message, test output. Out: that result added to the conversation, where it becomes part of the model’s next input.

In OpenAI’s Agents SDK, when the model produces tool calls, the runner executes them, appends the results and runs the loop again [8]. Anthropic’s SDK does the same automatically on each turn [6]. Anthropic stresses that agents need this “ground truth” from the environment at each step, such as tool results or code execution, to assess their progress [1].

An observation can be wrong, unhelpful or hostile. In ReAct’s error analysis on one question-answering benchmark, uninformative search results made up 23% of error cases and derailed the model’s reasoning [3]. Observations can also carry instructions planted in websites or files, a risk OWASP calls indirect prompt injection [13]; a later guide explains how websites and data can manipulate an agent.

Adjust: Using the Result

In: the latest observation, compared with the goal. Out: a decision to continue, change course, retry or stop.

Adjusting works best when the agent has something concrete to check against. Anthropic notes that coding agents can iterate using automated test results as feedback [1]. It recommends clearly defined rules, with an explanation of which rule failed and why, as the strongest form of feedback, and calls having another model judge the output “generally not a very robust method” [4].

Some research systems formalize this step. Reflexion turns a success or failure signal into a written summary of what went wrong and adds it to the agent’s context for its next attempt, without retraining the model [10]. Its authors note that the method depends on the model’s own ability to evaluate itself and offers no formal guarantee of success [10].

How State Carries From One Turn to the Next

The agent’s “memory” of the task is mostly the growing record of goals, steps and results that is sent back to the model on each turn. Anthropic notes that an agent running in a loop keeps generating information that may matter for the next turn, so what it carries forward has to be refined repeatedly [7].

That record cannot grow forever. Anthropic reports that as more text fills a model’s context window, its ability to recall information accurately from that context declines, a pattern it calls context rot [7]. Common responses are to summarize old turns, as the Claude Agent SDK does automatically near the context limit [6]; to keep progress notes in a file and pull them back in later [7]; or to store only a few lessons, as Reflexion did with one to three written reflections [10].

Whatever survives this trimming is what the next Plan step sees. If a key fact drops out, the agent cannot use it. Context, memory and saved state get their own guide later in this block: how AI agents remember what they're doing.

A Worked Example: Fixing Failing Tests

Anthropic’s documentation for its Claude Agent SDK walks through a request to fix failing tests in a code file [6]. It is a documentation illustration, not a measured deployment, but it shows the loop clearly.

  1. Turn 1. The agent runs the test suite. The observation is three failures [6].
  2. Turn 2. It reads the code file and the test file to see what is wrong [6].
  3. Turn 3. It edits the code, then reruns the tests, and all three pass [6].
  4. Final turn. It replies in text with no further tool calls, which ends the loop [6].

Read through BuildAIQ’s labels, the first test run is an Act whose Observe step reveals the problem. Reading the files gathers what the next Plan needs. The edit is a new Act, and the passing tests are the Observe step that tells the Adjust step to stop.

How an Agent Loop Stops

An agent loop ends for one of four reasons, and good designs plan for all of them, not only success.

The goal is met. In ReAct’s question-answering setup, a dedicated finish action ended the task with an answer [3]. OpenAI’s Agents SDK treats output as final when it is text of the expected type with no tool calls [8]. On Anthropic’s API, the loop continues while the model is requesting tools and exits on any other stop reason, meaning either a final answer or a stop the application must handle [5].

A limit is reached. ReAct’s authors gave the method seven steps on one benchmark and five on another before switching to a different method, because more steps did not improve results [3]. OpenAI’s SDK raises an error when a run exceeds its turn limit [8]. Anthropic’s SDK can cap turns or estimated spend and returns an error result instead of a final answer when either is hit [6]. Without limits, Anthropic notes, the loop runs until the model finishes on its own, which can take a long time on open-ended requests [6].

An error occurs. OpenAI’s guide lists errors among the common exit conditions [2]. Anthropic’s SDK reports a distinct result when an error interrupts the loop, separate from success and from hitting a limit [6].

A person takes over. OpenAI’s guide describes an agent that, in case of failure, can halt and transfer control back to the user [2]. It recommends escalating to a person when retry limits are exceeded, and human oversight for sensitive, irreversible or high-stakes actions [2]. Its Agents SDK can pause a run until a person approves or rejects a sensitive tool call, then resume [9]. Anthropic describes agents pausing for human feedback at checkpoints or when they hit a blocker [1]. When an agent should ask is the subject of when an AI agent should stop and ask a human, and how many turns may run without approval is one dimension of autonomy.

Whichever way a loop ends, the stop rules are design choices. Someone sets the turn limit, the spending cap and the actions that need approval, and that person or organization, not the agent, is answerable for how the run turns out.

Where the Loop Goes Wrong

Because each turn builds on the last, a mistake early in the loop can carry forward. Anthropic warns that agents’ autonomy brings higher costs and the potential for compounding errors, and recommends testing in sandboxed environments [1]. ReAct’s authors saw models repeat earlier thoughts and actions without finding a way out of the loop [3]. A planning survey reports plans that refer to items that do not exist in the environment [12]. Bad or manipulated observations add further risk [3, 13]. The full picture is in how AI agents can fail when they take action.

What We Know, What We Don’t, and Where Experts Disagree

What We Know

  • The basic cycle is documented consistently: the model requests an action, software executes it, and the result is returned to the model for the next step [8, 5, 6].
  • The OpenAI and Anthropic agent SDKs provide explicit stop mechanisms, including turn limits, final-output rules and pauses for human approval [8, 9, 6].

What We Don’t Know

  • How often real deployed agents stop because they succeeded, as opposed to hitting a limit, failing or handing back. No independent cross-vendor data on this was found for this article.
  • How much of the published guidance carries over between products. Many of the detailed descriptions used here come from companies that build agent tools [1, 2, 6].
  • How well benchmark and documentation examples predict everyday use. The ReAct and Reflexion results come from research benchmarks, and the worked example above is an illustration, so none of them measures how reliably agents complete real tasks [3, 10, 6].

What Experts Disagree About

Confidence: Expert Debate

The main disagreement is how far an agent can be trusted to check its own work. Anthropic argues that agents able to check and improve their output are more reliable because they catch mistakes before they compound [4]. Reflexion’s authors note their self-reflection method has no formal guarantee of success, and a planning survey says there is no proof that repeated self-reflection will lead an agent to its goal [10, 12]. Researchers also weigh planning up front against planning step by step, with different trade-offs for staying on track versus adapting to feedback [12, 11].

Where This Leads: From the Loop to Planning

This guide treated Plan as a single move inside each turn. The next guide, how AI agents break a goal into smaller tasks, looks at the larger plan: splitting a goal into steps, choosing among plans, revising them and having people review them. After that, this block follows the Act step outward into tools, app connections, computer use, memory and long-running work.

Practical Takeaways

  • Ask what the agent sees after each action. The quality of its next step depends on the quality of the result it was given [3, 1].
  • Look for explicit limits. A turn or spending cap turns a runaway loop into a clear, reportable stop [8, 6].
  • Prefer concrete checks. Tests and clearly defined rules give the Adjust step firmer ground than another model’s opinion [4].
  • Treat tool results as untrusted input. Web pages and files can contain text written to steer the agent [13].
  • Require a handback before irreversible actions. Sensitive or high-stakes steps are where a pause for a person matters most [2, 9].
  • Read the turns, not only the final answer. The sequence of actions and observations shows where a run went right or wrong.

Sources

  1. Building effective agents. Anthropic, 2024-12-19, accessed 2026-11-08.
  2. A practical guide to building agents. OpenAI, accessed 2026-11-08.
  3. ReAct: Synergizing Reasoning and Acting in Language Models. arXiv, 2022-10-06, accessed 2026-11-08.
  4. Building agents with the Claude Agent SDK. Anthropic, 2025-09-29, accessed 2026-11-08.
  5. How tool use works – Claude Platform Docs. Anthropic, accessed 2026-11-08.
  6. How the agent loop works – Claude Code Docs. Anthropic, accessed 2026-11-08.
  7. Effective context engineering for AI agents. Anthropic, 2025-09-29, accessed 2026-11-08.
  8. Running agents – OpenAI Agents SDK. OpenAI, accessed 2026-11-08.
  9. Human-in-the-loop – OpenAI Agents SDK. OpenAI, accessed 2026-11-08.
  10. Reflexion: Language Agents with Verbal Reinforcement Learning. arXiv, 2023-03-20, accessed 2026-11-08.
  11. A Survey on Large Language Model based Autonomous Agents. arXiv, 2023-08-22, accessed 2026-11-08.
  12. Understanding the planning of LLM agents: A survey. arXiv, 2024-02-05, accessed 2026-11-08.
  13. LLM01:2025 Prompt Injection. OWASP Gen AI Security Project, 2024-04-10, accessed 2026-11-08.