Contents

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:

What is missing is a way to look, decide, act and check again.

A red CI task goes to a fixed pipeline, which edits the wrong file, and to a model without tools, which invents a cause it never checked.CI is red"fix feature/x"Fixed pipelineread file A, edit it, run testsOne call, no toolsreasons about the code from memoryWrong filethe bug was elsewhereInvented causenever ran the tests
Scroll to see all of it.
Both designs commit to a path before anyone has seen the failing test.

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:

  1. Look. The model does not know what fails, so it calls run_tests. Result: 1 failed, 41 passed; test_discount expected 90.0 and got 0.9.
  2. Decide from what it saw. A ratio instead of a price suggests the discount function. It calls search_code("def apply_discount") and gets shop/pricing.py:12.
  3. Read. It calls read_file and sees line 13: return (100 - pct) / 100. The price factor is missing.
  4. Act. It calls edit_file to replace that line with return price * (100 - pct) / 100. The tool confirms one occurrence was replaced.
  5. Check. It runs run_tests again instead of assuming the edit worked. Result: 42 passed.
  6. 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

The task enters the context; the model reads the context and either returns a final answer or a tool call; the harness validates and runs the tool against the environment; the observation is appended to the context and the loop repeats until the model finishes or a limit forces a stop.donetool callappendlimit hitTaskContextprompt, tools, historyModelpicks the next stepFinal answerno tool callHarnessvalidate, permit, runToolsfiles, tests, webObservationresult or error, trimmedForced stopturns, budget, time1234567
Scroll to see all of it.

Hover over a number, or tap it, to read what it is.

Solid arrows carry a request or the next step, dashed arrows carry a result. The accent arrows are the loop, which ends at a final answer or a forced stop.
  1. Context. The system prompt, the tool definitions and the message history, which grows with every step.
  2. Model. Reads the whole context and returns a tool call or a final answer.
  3. Harness. Your code: it checks that a call is valid and permitted, runs it, catches errors and enforces the limits.
  4. 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.
  5. Observation. The tool’s output or its error message, trimmed and appended to the context so the model can react to it.
  6. Final answer. The normal end: a reply with no tool call.
  7. 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

Avoid it when

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

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.

Further reading