Skip to content

The Agent Loop and Acting

The agent loop and ReAct

Perceive, reason, act, observe

One LLM call answers a question. A loop around that call, reason, act, observe, repeat, is what turns the model from a text generator into an agent. This lesson is the spine of the whole course: the agent loop, and its most famous shape, ReAct.

The loop is the whole idea

An agent is not a smarter prompt. It is an ordinary LLM placed inside a control cycle that runs until a goal is met. The most widely agreed definition is deliberately mechanical: an LLM agent runs tools in a loop to achieve a goal.[1] The load-bearing words are in a loop, the line between a one-shot tool call and a real agent. Anthropic phrases it the same way: agents are "typically just LLMs using tools based on environmental feedback in a loop."[2]

The loop has four beats. The model perceives the current state, reasons about what to do, acts by calling a tool, then observes the result, and that observation becomes the next perception. It repeats until the goal is satisfied or a stopping condition fires.

Tap a node to see what it does.

The agent loop: perceive, reason, act, observe, then repeat until the goal is met or a step limit stops it.

Cross-reference the definitions you already met: a single LLM call is not an agent, and this loop is exactly the component the anatomy lesson called orchestration. This is where all of it becomes concrete.

Why the loop is what makes it an agent

Ask a bare LLM "what other device controls Front Row?" and it must answer from parametric memory: it might be right, it might confidently invent an answer. Give it a search tool once and you have tool use, not an agent: it runs the tool a single time and stops, never using what came back. The loop changes the physics. After every action the model receives ground truth from the environment, a tool result, a code-execution output, an error, and reasons again from that fact.[2] That feedback is what lets the system self-correct against reality instead of drifting.

The discriminator

One call that emits a tool request and halts is tool use. A model that observes the result, decides the next action from it, and iterates is an agent. Same model, the loop is the difference.[1]

ReAct: reasoning and acting, interleaved

is the canonical shape of the loop. Instead of separating "think" from "do," it interleaves them into a repeating three-part cycle, Thought, Action, Observation, until an action like Finish[answer] ends it.[3] The payoff is a two-way synergy:

Reason then Act

Thoughts let the model track a plan and handle exceptions: "that search failed, let me reformulate the query." Reasoning steers the actions.[3]

Act then Reason

Observations inject fresh ground truth, so the next thought is corrected against reality. This is what pure chain-of-thought lacks: it never checks the world.[3]

A real ReAct trace

Here is the canonical worked trace from the ReAct paper, a two-hop question answered by interleaving thoughts with Search calls against a Wikipedia API.[3][4] Watch how a failed search in Obs 2 is recovered by the reasoning in Thought 3, the moment an act-only agent would stall.

Q: Aside from the Apple Remote, what other device can control the
   program the Apple Remote was originally designed to interact with?

Thought 1  Search Apple Remote, find the program it was designed for.
Action 1   Search[Apple Remote]
Obs 1      ... introduced in 2005, originally designed to control the
           Front Row media center program ...

Thought 2  It controls Front Row. Search Front Row next.
Action 2   Search[Front Row]
Obs 2      Could not find [Front Row]. Similar: ['Front Row (software)', ...]
           ^ failed search

Thought 3  Front Row is not found. Search "Front Row (software)".
           ^ the recovery: reasoning reformulates the query
Action 3   Search[Front Row (software)]
Obs 3      ... controlled by an Apple Remote or the keyboard function keys.

Thought 4  So the answer is keyboard function keys.
Action 4   Finish[keyboard function keys]
A ReAct trace answering a 2-hop question. The failed Obs 2 is recovered by the reasoning in Thought 3.

Read the recovery

Thought 3 is the entire argument for ReAct. A pure act-only agent that just fired Search commands would be stuck at Obs 2. A pure reasoning agent (chain-of-thought, no tools) would never have discovered "Front Row" at all and might guess "iPhone" from memory. Interleaving both lets the agent reformulate the query, do the second hop, and ground the final answer in real text.

Four ways to answer the same question

The ReAct paper ablates the design by stripping components. Only the version with both reasoning and acting survives a failed search:[4]

StyleReasons?Acts (tools)?Failure mode
StandardnonoAnswers blind, often wrong, no trace to inspect.
Chain-of-thoughtyesnoHallucinates the intermediate facts, never checks them.
Act-onlynoyesCannot recover from a failed action or plan a second hop.
ReActyesyesRecovers, grounds each step, answers correctly.
Stripping either reasoning or acting breaks a task that needs external info. ReAct keeps both.

ReAct is not always the winner: on pure closed-book recall it can lag chain-of-thought with voting, and the strongest setup often switches between the two. But for any task where the agent must gather external info or act in an environment, ReAct is the default.[4] Modern tool-calling agents, including Claude's tool use, are ReAct at production scale.[2]

The loop is a generate then verify cycle

Zoom out and the loop has a rhythm: the model generates a candidate action, the environment verifies it by returning a real result, and the next generation is conditioned on that verification. Karpathy frames the whole discipline of building agents as keeping this generate then verify loop tight and fast, with a human able to watch it.[5] It is why the agent-friendly domains are the verifiable ones: a coding agent generates a patch and the test suite verifies it, and the failing test becomes the next observation. When there is no cheap verifier, the loop has nothing to self-correct against, a theme the reflection lesson pushes further.

Stopping conditions and step limits

A loop with no exit is a bug, not an agent. Every observation costs tokens and latency, and an early mistake can propagate through the whole chain, so the loop needs an explicit bound.[6] Real agents stop on one of four conditions:

  1. Goal satisfied. The model emits a terminal action (Finish[...]) or otherwise signals the task is done.
  2. Step or iteration budget. A hard cap, max_iterations, so a confused agent cannot spin forever burning tokens.[2]
  3. Blocker or human checkpoint. It pauses for a human before an irreversible write (a payment, a delete) or when it hits something it cannot resolve.
  4. Loop detection. The same action producing the same observation repeatedly means the agent is stuck, a classic "make progress or stop" heuristic.[7][8]

Unbounded loops are the default failure

Without a stopping condition an agent will happily loop until your API bill or your context window runs out. Always ship completion detection and a step limit, and gate irreversible actions behind a human or a spend cap. This is the cheapest reliability win you will ever make.

Check yourself

Match each beat of the agent loop to what happens there.

drop here

read the current context

drop here

plan the next action

drop here

call a tool

drop here

the tool result becomes the next perception

In a ReAct loop, what does the Observation step provide that pure chain-of-thought reasoning lacks?

Name the three repeating steps of a ReAct cycle, in order, and what ends the loop.

What ends it? Try to state it, then check.

Lock it in

  • The loop makes the agent: perceive, reason, act, observe, repeat. A one-shot tool call is tool use; iterating on the result is an agent.
  • Observations inject ground truth. Real tool results re-anchor the model each step, the antidote to chain-of-thought's unchecked hallucination.
  • ReAct is Thought, Action, Observation interleaved. Reasoning steers actions and handles exceptions; actions feed reality back into reasoning.
  • It is a generate then verify cycle. Agents work best where progress is cheaply verifiable: code, search, environments.
  • Always bound the loop. Stop on goal met, a step limit, a human checkpoint, or loop detection, never leave it open-ended.

Primary source

Yao et al., ReAct: Synergizing Reasoning and Acting in Language Models (ICLR 2023)

The origin of the Thought, Action, Observation loop, the worked Apple-Remote trace, and the ablation that shows why interleaving beats reasoning-only and acting-only.

Sources

  1. 1.Simon Willison, Tools in a loop (2025)
  2. 2.Anthropic, Building Effective Agents
  3. 3.Yao et al., ReAct: Synergizing Reasoning and Acting in Language Models (ICLR 2023)
  4. 4.Prompt Engineering Guide, ReAct Prompting
  5. 5.Andrej Karpathy, 2025 LLM year in review
  6. 6.Chip Huyen, Agents (2025)
  7. 7.Shinn et al., Reflexion (2023)
  8. 8.Lilian Weng, LLM Powered Autonomous Agents (2023)