esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

AI engineering / Beginner / 01.13

ReAct

A model that thinks, acts, and looks again: how the think, act, observe loop works, and the guards that keep it from running forever.

13 min readFree

A model that looks again

The last lesson, Function Calling, ended with a promise: next, ReAct, a model that thinks, acts, and looks again. Here it is.

Think of how you answer a hard question with a phone in your hand. You think, "I need the opening hours." You look them up. You read the result. Now you know more, so you think again. You repeat until you can answer. ReAct gives a model that same habit.

By the end you will know what the loop is, what it adds to chain-of-thought and function calling, what can go wrong, and what guards an engineer adds. You will also run a small loop of your own. Agents get their own lessons later in this course, so here we keep to the loop itself.

Key words

ReAct
Short for "Reason + Act". A way of working where the model switches between thinking in words and using a tool.
Thought
A short note the model writes to itself about what to do next.
Action
A tool call: the model asks your code to run a tool, like a search or a calculator.
Observation
What the tool gave back. Your code adds it to the conversation.
Chain-of-thought
Asking a model to write out its reasoning step by step before it answers.
Step limit
A hard cap on how many times the loop may run.
Trace
The saved record of every thought, action and observation in one run.

Where ReAct comes from

ReAct comes from a 2022 research paper, "ReAct: Synergizing Reasoning and Acting in Language Models". Its abstract says that reasoning (it names chain-of-thought prompting) and acting "have primarily been studied as separate topics".

So the authors tried them together. In their words, they "explore the use of LLMs to generate both reasoning traces and task-specific actions in an interleaved manner". Interleaved means mixed in turn, like the teeth of a zip. You will also see it called ReAct prompting, because the prompt shows the model the pattern to copy.

Why mix them? The abstract gives two reasons: "reasoning traces help the model induce, track, and update action plans as well as handle exceptions, while actions allow it to interface with external sources, such as knowledge bases or environments, to gather additional information".

In plain words: thinking helps the model plan and recover when something goes wrong. Acting lets it fetch facts it does not have.

The loop, step by step

Here is one trip around the loop. Your code runs it. The model only writes text.

  1. 1

    Ask

    Your code sends the question to the model, along with a list of tools it may use.

  2. 2

    Thought

    The model writes what it plans to do, for example: I need the number of shelves first.

  3. 3

    Action

    The model names a tool and an input, such as lookup with "shelves in the library". It then stops and waits.

  4. 4

    Run the tool

    Your code, not the model, runs the tool and gets a result.

  5. 5

    Observation

    Your code adds the result to the conversation and calls the model again, so it can read what happened.

  6. 6

    Repeat or answer

    The model either asks for another tool or writes a final answer. Then the loop ends.

Notice who does what. The model never touches a tool. It only writes a request in text. Your code reads the request, does the work, and pastes the result back. That is function calling, from the last lesson, placed inside a loop.

The project page describes how its prompts were built. A ReAct prompt consists of "few-shot task-solving trajectories, with human-written text reasoning traces and actions, as well as environment observations in response to actions". A few-shot prompt shows the model a few worked examples first, so it copies the pattern.

How it builds on what you know

  • Chain-of-thought alone

    The model reasons well in words but has only what it memorised. If a fact is missing or wrong, it can state a wrong one with confidence.

  • Function calling alone

    The model can use tools, but with no written reasoning it can pick badly, and you cannot see why.

  • ReAct

    Reasoning picks the next tool. The tool's result feeds the next reasoning. Each loop turn gives the model fresh facts.

The project page makes the same point with an example. The reason-only baseline, it says, "is not grounded to external environments to obtain and update knowledge". The act-only baseline "suffers from the lack of reasoning". ReAct, in contrast, "solves the task with a interpretable and factual trajectory". A trajectory is the full path from question to answer.

Why it helps

The main gain is simple: the model can look things up instead of guessing. A model that guesses a date or a number may sound sure and be wrong. A model that calls a lookup tool and reads the result has something solid to build on.

The abstract says this on two real tasks, question answering (HotpotQA) and fact checking (Fever). On both, it says, ReAct "overcomes issues of hallucination and error propagation prevalent in chain-of-thought reasoning by interacting with a simple Wikipedia API". Hallucination means making up a fact. Error propagation means one early mistake spoiling every later step.

The abstract also reports "improved human interpretability and trustworthiness". The written thoughts act like a diary of the run. You can read why the model did each thing.

On two decision-making tests (ALFWorld and WebShop), the abstract says ReAct beat imitation and reinforcement learning methods "by an absolute success rate of 34% and 10% respectively, while being prompted with only one or two in-context examples". Those are results for that paper's setup. Your product will have its own numbers, so test it.

Tip

Keep thoughts short. They are text the model writes, and every word of it is paid for.

Two tools, built safely

Let's build the loop. First, two small tools. The first, lookup, reads from a tiny table of made-up facts. The second, calculator, does arithmetic.

A calculator is where a beginner is tempted to hand the model's text straight to Python's eval, which runs whatever it is given. Never do that. The model writes text that comes from outside your control. If it wrote code that deletes files, eval would run it.

Instead, we use the ast module. It parses text into a tree that describes the code, without running it. The Python docs say "the ast module helps Python applications to process trees of the Python abstract syntax grammar". With mode="eval", the tree describes a single expression. Our code then walks the tree and accepts only numbers and the four basic sums. Anything else is refused.

Python
import ast
import operator

FACTS = {"shelves in hillview library": "7", "hillview library opened": "1998"}

def lookup(query):
    return FACTS.get(query.lower().strip(), "no result")

OPS = {ast.Add: operator.add, ast.Sub: operator.sub,
       ast.Mult: operator.mul, ast.Div: operator.truediv}

def walk(node):
    if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):
        return node.value
    if isinstance(node, ast.BinOp) and type(node.op) in OPS:
        return OPS[type(node.op)](walk(node.left), walk(node.right))
    if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
        return -walk(node.operand)
    raise ValueError("not allowed")

def calculator(expression):
    try:
        return str(walk(ast.parse(expression, mode="eval").body))
    except (ValueError, SyntaxError, ZeroDivisionError):
        return "error"

print(calculator("7 * 12"))
print(calculator("2 + 3 * 4"))
print(calculator("__import__('os').getcwd()"))
print(calculator("10 / 0"))
print(lookup("Shelves in Hillview Library"), lookup("moon"))

Output

84
14
error
error
7 no result

Read the output. 7 times 12 is 84. Operator order is respected: 2 + 3 * 4 gives 14, not 20. The sneaky text that tries to run an import is refused with "error". Dividing by zero gives "error" too, instead of crashing the loop. The lookup is case-insensitive and gives "no result" for a fact it does not hold.

One limit, from the same docs: it is possible to crash the Python interpreter with a sufficiently large or complex string, because of stack depth limits in Python's AST compiler. So also cap how long the input may be in a real product.

The whole loop, running

Now the loop. Read this part first: the model here is a stand-in. It is a function called fake_model that replays three fixed lines of text, so the code runs free, with no key and no internet. A real model is slower and costs money for every token. It can give a different answer on each run. It can also fail or refuse. Our stand-in never will, which is exactly why it is not a real test of a model.

The loop reads each reply with a small pattern, finds the line that looks like Action: tool[input], runs that tool, and adds an Observation line. It stops on a line starting with Final Answer. It also has a step limit, max_steps. The second run uses a second stand-in, stuck_model, that never stops asking for the lookup tool.

Python
import ast
import operator
import re

FACTS = {"shelves in hillview library": "7"}

def lookup(query):
    return FACTS.get(query.lower().strip(), "no result")

OPS = {ast.Add: operator.add, ast.Sub: operator.sub,
       ast.Mult: operator.mul, ast.Div: operator.truediv}

def walk(node):
    if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):
        return node.value
    if isinstance(node, ast.BinOp) and type(node.op) in OPS:
        return OPS[type(node.op)](walk(node.left), walk(node.right))
    if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
        return -walk(node.operand)
    raise ValueError("not allowed")

def calculator(expression):
    try:
        return str(walk(ast.parse(expression, mode="eval").body))
    except (ValueError, SyntaxError, ZeroDivisionError):
        return "error"

TOOLS = {"lookup": lookup, "calculator": calculator}

# A STAND-IN, not a real model: it replays fixed text.
SCRIPT = [
    "Thought: I need the number of shelves first.\nAction: lookup[shelves in Hillview Library]",
    "Thought: 7 shelves, 12 books each. Multiply.\nAction: calculator[7 * 12]",
    "Thought: I have the number.\nFinal Answer: 84 books",
]

def fake_model(messages):
    turns = sum(1 for m in messages if m["role"] == "assistant")
    return SCRIPT[turns]

def stuck_model(messages):
    return "Thought: One more look.\nAction: lookup[moon]"

ACTION = re.compile(r"Action: (\w+)\[(.*)\]")

def react(question, model, max_steps=5):
    messages = [{"role": "user", "content": question}]
    for step in range(1, max_steps + 1):
        reply = model(messages)
        messages.append({"role": "assistant", "content": reply})
        print(f"--- step {step}")
        print(reply)
        if "Final Answer:" in reply:
            return reply.split("Final Answer:")[1].strip()
        found = ACTION.search(reply)
        tool = TOOLS.get(found.group(1)) if found else None
        result = tool(found.group(2)) if tool else "unknown tool"
        print("Observation:", result)
        messages.append({"role": "user", "content": "Observation: " + result})
    return None

q = "Each shelf holds 12 books. How many books fit in Hillview Library?"
print("ANSWER:", react(q, fake_model))
print()
print("ANSWER:", react(q, stuck_model, max_steps=3))

Output

--- step 1
Thought: I need the number of shelves first.
Action: lookup[shelves in Hillview Library]
Observation: 7
--- step 2
Thought: 7 shelves, 12 books each. Multiply.
Action: calculator[7 * 12]
Observation: 84
--- step 3
Thought: I have the number.
Final Answer: 84 books
ANSWER: 84 books

--- step 1
Thought: One more look.
Action: lookup[moon]
Observation: no result
--- step 2
Thought: One more look.
Action: lookup[moon]
Observation: no result
--- step 3
Thought: One more look.
Action: lookup[moon]
Observation: no result
ANSWER: None

Read the first run. Step 1: a thought, then a lookup. The observation is 7 shelves. Step 2: the stand-in multiplies 7 by 12 using the calculator, and the observation is 84. Step 3: it writes a final answer, "84 books". Three steps, two tool calls.

Read the second run. stuck_model asked for the same useless lookup every time. We set max_steps to 3, so the loop printed three steps and then gave up. It returned None, which Python uses for "nothing". Without that limit, this loop would still be going.

What can go wrong

The loop is powerful because the model decides what happens next. That is also where the trouble starts. There are three common problems.

  • Loops that never end

    The model keeps asking for tools because each result looks unhelpful, or it repeats the same call, like stuck_model above. Nothing in the loop forces it to stop.

  • Wrong tool choices

    The model may pick a tool that does not fit, send it a badly shaped input, or misread the result. A good thought can still lead to a bad action.

  • Cost that grows each step

    Each call re-reads the whole conversation so far, including every earlier thought and observation. So step 10 costs more than step 1.

Cost growing every step

Let's put made-up numbers on it. Say each step adds 100 tokens to the conversation. This number is made up, only for the sum. Step 1 reads 100 tokens, step 2 reads 200, step 3 reads 300, and so on. With t tokens added per step and n steps, the total read is the sum of those, which has a short form.

total=t⋅n(n+1)2\text{total} = t \cdot \frac{n(n+1)}{2}

In words: add up 1, 2, 3 and so on up to n, then multiply by t. With t = 100 and n = 10, the total is 100 times 55, or 5,500 tokens. With n = 20, it is 100 times 210, or 21,000. Twice the steps cost almost four times as much.

The Tokens and Context Window lessons explain what tokens are and why a conversation can only hold so many. A long loop pushes against both limits at once: the bill and the room.

Guards an engineer adds

You cannot make a model behave perfectly. You can build a fence around the loop. Three guards cover most of the risk.

  1. 1

    A step limit

    Set a maximum number of turns, like max_steps in our code. Pick it from the hardest task you expect, plus a little room. When the limit hits, return a clear message, not a silent failure.

  2. 2

    A stop rule

    Decide exactly what ends the loop: a Final Answer line, an error you cannot recover from, or the same action called twice in a row with the same input. Check for the repeat in code, since the model will not do it for you.

  3. 3

    Log every step

    Save the thought, action, input and observation of every turn, with the time. When a run goes wrong, this trace tells you where. It also shows you what it cost.

Add small habits on top. Cap the length of tool input and tool output. Give a tool an error message the model can read, such as "no result", so it can try another way. Never give a tool more power than the task needs.

Tip

Treat a tool's output as untrusted text. A web page the model reads could contain instructions. The model might follow them. Your code should decide what is allowed, not the page.

Quick check

A ReAct loop keeps calling the same lookup with the same input and getting "no result". What is the best guard?

What to remember

  • The loop

    Thought, action, observation, repeat, until the model writes a final answer.

  • The gain

    The model can look things up instead of guessing, and its thoughts leave a trace you can read.

  • The guards

    A step limit, a stop rule, and a log of every step.

Quick check

In a ReAct loop, who actually runs the tool?

Agents build on this loop. They get their own lessons later in this course.

Next: streaming responses, showing the answer as it is written.

Sources

How is this lesson?

Last updated on 16 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.