ReAct (tool-use loop)
Also known as: tool-calling loop, agent loop, function-calling agent
- Who decides
- The model
- Shape
- Loop
Intent
Give one model a goal and a set of tools, and let it choose each next action after seeing the result of the last one. Your code runs every tool call, feeds the result back, and stops the loop when the model gives a final answer or a limit is hit.
About the name. Strictly, ReAct is a 2022 prompting method by Yao et al. in which the model writes Thought:, Action: and Observation: lines as plain text. Today the name is used for any model that calls tools in a loop, which is the meaning on this page.
Problem
You are building a coding assistant. A developer types: “CI is red on branch feature/x. Fix it.”
Nobody knows the steps yet: which test fails, which file is at fault, how many rounds of editing and retesting it will take. The two obvious designs, a fixed pipeline and a single call to the model, both choose the path before anyone has looked. Three things go wrong:
- A fixed pipeline breaks on the first surprise. “Read file A, edit it, run the tests” only works if file A is the culprit. Here the failing test points to a file nobody planned for, and a pipeline written for one kind of failure does nothing for the next one.
- A single call cannot look or act. The model cannot run the tests or save an edit, and the whole repository does not fit in one prompt. It answers from whatever was pasted in.
- Reasoning without tools invents causes. A model that only thinks gives a confident diagnosis it never checked. The developer finds out when CI is still red.
What is missing is a way to look, decide, act and check again.
Solution
Put the model in a loop with tools. On each turn it reads everything so far (the task, its earlier actions and their results) and returns either a tool call or a final answer. Your code, called the harness, runs the tool and adds the result to the conversation. Nothing fixes the number or the order of the steps in advance: they come out of what the model observes.
On the CI task, a run looks like this:
- Look. The model does not know what fails, so it calls
run_tests. Result: 1 failed, 41 passed;test_discountexpected 90.0 and got 0.9. - Decide from what it saw. A ratio instead of a price suggests the discount function. It calls
search_code("def apply_discount")and getsshop/pricing.py:12. - Read. It calls
read_fileand sees line 13:return (100 - pct) / 100. The price factor is missing. - Act. It calls
edit_fileto replace that line withreturn price * (100 - pct) / 100. The tool confirms one occurrence was replaced. - Check. It runs
run_testsagain instead of assuming the edit worked. Result: 42 passed. - Finish. It returns a final answer with no tool call: root cause, the line changed, 42 of 42 tests passing. The harness ends the loop.
The model decides which tool to call, in which order, and when it is done; the harness decides whether a call is allowed and when to force a stop, so anything you need guaranteed belongs in the harness, not in the prompt. The nearest pattern, Plan-and-Execute, writes the whole plan up front, while ReAct chooses only the next action, after seeing the latest result.
Structure
Hover over a number, or tap it, to read what it is.
- Context. The system prompt, the tool definitions and the message history, which grows with every step.
- Model. Reads the whole context and returns a tool call or a final answer.
- Harness. Your code: it checks that a call is valid and permitted, runs it, catches errors and enforces the limits.
- Tools. Functions that read data or change something: files, a test runner, a search API, a database. A tool’s description is all the model knows about it.
- Observation. The tool’s output or its error message, trimmed and appended to the context so the model can react to it.
- Final answer. The normal end: a reply with no tool call.
- Forced stop. The harness ends the run at a limit on turns, budget or time, and returns an error or a partial result.
Use it when / Avoid it when
Use it when
- The steps cannot be predicted. Debugging, research and data exploration all depend on what the first steps reveal.
- The environment can say whether the model is right. Tests, compilers and API responses give a result the model can check after each step.
- Mistakes can be undone. The loop takes wrong steps along the way, which is fine in a sandbox or on a branch.
- You can afford the time and cost. Every step is at least one model call plus the tool’s run time.
Avoid it when
- The steps are known and stable. Use a prompt chain, which costs less and takes the same path on every run.
- One call answers the question. Use a single call: a summary or a translation gains nothing from tool round trips.
- The work splits into independent parts. Use Parallelization, which runs the parts at the same time instead of one after another.
- Actions cannot be undone. Add Human-in-the-Loop approval before those tool calls, or keep the agent read-only.
How it fails
- Repetitive loops
-
The agent calls the same tool with the same arguments again and again, and every turn costs a model call.
Mitigation: have the harness detect a repeated call and tell the model to change strategy or finish.
- False success
-
The model says it is done without checking, and the tests still fail.
Mitigation: verify in the harness, for example by running the tests when the model gives its final answer.
- Context overflow
-
Large tool outputs fill the context, and the model loses track of the task and of what it already found.
Mitigation: trim results at the tool, with a note saying how to narrow the query.
- Prompt injection
-
A web page, an email or a file the agent reads contains instructions, and the model follows them as if the user had written them.
Mitigation: do not give one agent private data, untrusted content and a way to send data out all at once.
Related patterns
- Plan-and-Execute: plans every step up front and replans when needed, which gives fewer large-model calls and a plan a person can review, at the price of adapting less.
- Evaluator–Optimizer: adds a second call that checks the work and sends it back with feedback, the usual answer to false success.
- Supervisor: a ReAct loop whose tools are other agents; control returns to it after every call.
- Human-in-the-Loop: an approval step on tool calls, placed in the harness’s permission check, for the actions that cannot be undone.
Try it
Describe one agent: its role, its goal, the tools it can call and how it proves the work is done. The generator writes a system prompt for a single tool-calling agent.
Open the generator on this pattern
Further reading
- ReAct: Synergizing Reasoning and Acting in Language Models (Yao et al., 2022): the original method and its error analysis.
- Building effective agents (Anthropic, 2024): when an agent is worth it over a fixed workflow.
- The lethal trifecta for AI agents (Simon Willison, 2025): the prompt-injection risk for agents that use tools.