AI Skills

AI Agents Explained: The ReAct Loop

Aditya Kumar JhaAditya Kumar JhaLinkedIn·May 8, 2026·10 min read

An AI agent is a while-loop around an LLM. The reason-act-observe loop in plain English, with code and failure modes.

The ReAct loop is the reason-act-observe cycle an AI agent runs: the model reasons about what to do, acts by calling a tool, observes the result, then feeds that result back into the next prompt, repeating until the task is done or a stop condition fires. Put plainly, an AI agent is a while loop around a language model. Anthropic frames the same building block as an augmented LLM, a model wired up with retrieval, tools, and memory, then run in a loop that pulls ground truth from its environment at each step. The loop has a name: the reason-act-observe cycle, which Yao et al. formalized as ReAct in October 2022 (arXiv:2210.03629).

Insight

No framework, no magic: an AI agent is five lines of Python that let a model think, act, and read the result, on repeat, until it decides to stop.

Once you see the loop, you see it everywhere. The same skeleton powers a coding assistant, a research agent, and a customer-support bot. What changes is the model, the tools, and the prompt. That is why agents feel powerful and break in boringly predictable ways at the same time.

What is an AI agent, exactly

An AI agent is a system where a language model directs its own actions in a loop, deciding which tools to call and when to stop, rather than following a fixed script. Anthropic draws a sharp line: workflows are LLMs and tools orchestrated through predefined code paths, while agents are systems where the model dynamically directs its own process and tool usage, keeping control over how it accomplishes the task. The difference is who decides the next step. In a workflow your code does; in an agent the model does.

The foundation under every agent is what Anthropic calls the augmented LLM: a base model enhanced with three capabilities. It can generate search queries to retrieve information, select and call tools, and decide what to keep in memory. Stack a loop on top of that augmented model and you have an agent. Each capability maps to a part of the loop: retrieval feeds the reasoning step, tool calls are the action step, and memory decides what survives into the next observation.

  • Retrieval: the model writes its own queries to pull in outside information.
  • Tools: the model picks a function and fills in the arguments, like calling a weather API or running code.
  • Memory: the model decides what to carry forward into the next turn.

The ReAct loop in plain English: reason, act, observe

The loop has three repeating steps. The Hugging Face Agents Course labels them Thought, Action, and Observation, and describes the agent literally as a while loop that continues until the objective is fulfilled. Thought: the model decides what to do next. Action: it emits a tool call with arguments. Observation: the tool's response comes back and is appended to the prompt as new context, and the loop returns to Thought with that fresh information in hand.

Here is the whole pattern as code. Read it once and the abstraction collapses into something you could write yourself.

Insight

messages = [system_prompt, user_task] while True: reply = llm(messages) # REASON if reply.is_final: return reply.answer result = run_tool(reply.action) # ACT messages.append(reply) messages.append(result) # OBSERVE: result goes back in context

The key detail most thin explainers skip is the last line: the observation is appended to messages. The model does not remember the tool result; the result is stuffed back into the prompt as text so the next reasoning step can see it. Context accumulates, which is where both the power and the failures come from. Every turn the prompt carries the running transcript of thoughts, actions, and observations, so the model reasons over its own history rather than a clean slate.

You will meet this same loop under different names. LangGraph and LangChain expose it as a ReAct agent, and the OpenAI and Anthropic SDKs surface it as tool calling in a loop. The label changes; the reason-act-observe structure does not.

Why ReAct beat reasoning alone

ReAct stands for Reason plus Act, and its core finding is that interleaving the two beats doing either by itself. Google Research describes ReAct as interleaving verbal reasoning traces with text actions: actions pull feedback from the environment, and reasoning traces update the model's internal plan. Pure chain-of-thought reasoning hallucinates because it relies only on what is inside the model's weights, with no external grounding. Pure action without reasoning lacks the working memory to track a multi-step goal. Combining them lets the model reason to act and act to reason. The reasoning trace acts as scratch working memory for the action stream, and each action returns a fresh fact that the next reasoning step can correct against.

On ALFWorld, a text-based household task benchmark, and WebShop, an online-shopping benchmark, ReAct beat imitation and reinforcement learning methods by an absolute success rate of 34 percent and 10 percent respectively. On HotpotQA and FEVER, fact-checking and multi-hop question answering, grounding each reasoning step in live Wikipedia lookups cut the hallucination and error propagation that hit reasoning-only prompts.

Insight

34 points on ALFWorld, 10 on WebShop, just from letting the model reason between actions instead of acting blind.

There is a side benefit the original authors stressed: interpretability. Because the agent writes out its thoughts and actions as a readable trace, a human can inspect why it did something and even edit a thought mid-run to redirect it. Google Research notes that correcting an agent often requires editing only a few thoughts. The trace is not decoration. It is the audit log. That readable trace is also what makes a ReAct agent debuggable: when it fails, you can see the exact thought that sent it down the wrong path.

StepWhat the model doesWhere the output goesFailure if skipped
Reason (Thought)Plans the next move in natural languageStays in context as a visible traceAgent acts blindly, no goal tracking
Act (Action)Emits a tool call with argumentsSent to the tool runtimeModel can only talk, never affects the world
Observe (Observation)Reads the tool resultAppended to the prompt as new contextModel never learns whether the action worked

Three failure modes baked into the loop

The same loop that makes agents powerful makes them fragile, and the failures fall into three buckets: infinite loops, context bloat, and missing stop criteria. These are not edge cases. They follow directly from the while-loop structure, so every agent builder meets them.

Infinite loops and no stop criterion

Nothing in the base architecture guarantees the loop ever terminates. The model decides when it is done, and if its sense of done is fuzzy, it keeps going. A common pattern is the agent calling the same tool with the same arguments repeatedly, making no progress while burning tokens on every pass. The fix is simple: cap the total number of tool calls, and deduplicate identical tool-plus-argument calls so a repeat is caught instead of replayed. Stop conditions are something you add, not something the architecture gives you for free.

Context bloat

Because every observation is appended to the prompt, the context grows on every turn. After several turns the window is either full, forcing truncation that drops earlier information, or so crowded that the model cannot find the relevant pieces among the noise. The agent built the haystack and then lost its own needle.

Insight

The agent built the haystack and then lost its own needle. Mitigate it three ways: summarize old turns, prune observations that no longer matter, and keep only what the next step needs.

Goal ambiguity

When the agent has no precise picture of what success looks like, it cannot tell whether a partial result satisfies the task. Anthropic's guidance speaks to the antidote: agents need ground truth from the environment at each step, such as tool results or code-execution output, to assess progress and stop compounding errors. A clear definition of done and real feedback after each action are what keep an agent from drifting. Without that signal an agent will often report success on a half-finished task, because nothing in the loop forces it to compare its output against a real target.

Pro Tip

Before you reach for a heavier framework, add two lines: a max-iterations counter and a check that rejects an identical repeat tool call. Those two guards prevent the majority of runaway agent loops.

Agent versus workflow versus a plain LLM call

Not every task needs an agent, and Anthropic is blunt that you should reach for the simplest thing that works. A single LLM call answers a self-contained question. A workflow handles a known sequence of steps where your code holds the control flow. An agent earns its complexity only when the path to the goal is open-ended and the model genuinely needs to decide the next step from live feedback. The cost of an agent is unpredictability: more autonomy means more ways to fail, so the open-ended task is the only one that justifies it.

  • Plain LLM call: one prompt, one answer, no tools, no loop.
  • Workflow: predefined code path orchestrating LLM steps and tools, control flow lives in your code.
  • Agent: the model directs its own process in a reason-act-observe loop, control flow lives in the model.

Where memory fits: the observation problem

Ask an agent to pick up where it left off yesterday, or to use a fact from a doc it read last week, and the loop hits its weakest joint: the observation step, where everything it learns gets crammed into a finite context window. That is fine for a single short session. It breaks down when an agent needs long-term recall without re-reading the entire history into the prompt. This is the retrieval half of Anthropic's augmented LLM: the model writing a query to fetch the right information instead of carrying everything inline. Stuffing more history into the prompt only delays the ceiling, because the window is finite and every token competes for the model's attention.

Context bloat is a retrieval problem, not a loop problem. MemX is an external memory layer built for exactly that gap: a personal RAG store over your own notes, screenshots, and docs. Instead of an agent dumping every observation into the prompt until the window overflows, it queries MemX for the few items that matter and pulls those in. It will not stop a runaway loop and does not replace a stop criterion. What it fixes is context bloat, by moving long-term knowledge out of the prompt and behind a retrieval call, so the reason-act-observe loop stays lean.

Sources

Primary research and documentation behind the claims in this article.

Frequently Asked Questions
01What is an AI agent in simple terms?

An AI agent is a while loop around a language model. The model reasons about a task, calls a tool, reads the result, and repeats with that result added to its context, until it finishes or hits a stop condition. The loop, not the model, is what makes it an agent.

02What does the ReAct loop stand for?

ReAct stands for Reason plus Act. It interleaves reasoning traces with tool actions so the model checks its thinking against real feedback. Yao et al. introduced it in 2022 (arXiv:2210.03629), and it cut hallucination by grounding each reasoning step in live tool results.

03How is the ReAct loop different from chain-of-thought prompting?

Chain-of-thought reasons internally with no external grounding, so it can hallucinate. The ReAct loop interleaves reasoning with tool actions that pull real feedback from the environment, then updates the plan. That grounding is why ReAct cut errors on fact-heavy tasks where chain-of-thought drifted.

04How is an AI agent different from a workflow?

In a workflow, your code decides the next step through predefined paths. In an agent, the language model decides the next step itself based on live feedback. Anthropic draws this line directly: workflows are orchestrated by code, agents direct their own process and tool usage.

05How much code does an AI agent take?

The core loop is roughly five lines: call the model, run the tool it picks, append the result to the messages, and repeat until the model signals done. Frameworks add ergonomics and safety guards, but the reason-act-observe engine itself is small.

Read Next

Or try MemX to access 40+ AI models in one place — including Claude Sonnet 4.6 and GPT-5.4 — and get your questions answered today.

Was this article helpful?

Found this useful? Share it with someone who needs it.

Free · iOS, Android & WhatsApp

Stop losing what you save.
Let MemX remember it for you.

Every screenshot, photo, PDF and voice note — captured, encrypted, and instantly searchable. Ask in plain English, get the answer in seconds.

  • Reads text inside images and handwriting
  • Private and encrypted by default
  • Free to start, no credit card

Takes under a minute to set up. Your data stays yours.

Aditya Kumar Jha
Written by
Aditya Kumar JhaLinkedIn

Core software engineer at MemX, where he builds the website, backend, and data systems. Also a published author of six books on Amazon KDP, writing on AI, memory, and behavior.

Keep reading

More guides for AI-powered students.