Skip to content

All lessons

Area 03 // AI AgentsLesson 49 / 14014 min read

The agent loop and the ReAct pattern

Every agent runs the same loop (the model looks at the situation, decides on an action, the action runs, the result is added to the context, repeat until done), and ReAct is the classic recipe for it, interleaving reasoning ("Thought"), tool use ("Action") and results ("Observation").

Think of it like this

Analogy

Think of how you assemble flat-pack furniture:

  1. Look at what you've got so far and the instructions.
  2. Decide the next step ("attach side panel B").
  3. Do it.
  4. Check the result ("it's wobbly, the screw isn't tight").
  5. Repeat until the wardrobe is built, or you give up and call for help.

An agent loop is exactly this, with the model doing the looking and deciding and tools doing the work.

The problem it solves

A single LLM call can answer, but it can't do multi-step work: look something up, use the result to decide the next lookup, notice a mistake and fix it. To get there, you need a loop that keeps giving the model the latest situation and letting it choose the next action, plus rules for when the loop ends.

How it works: the agent loop

  1. Start with the goal (user message), the system prompt and the tool definitions.
  2. Call the model with the full message list.
  3. If it requests tools, run them and append both the model's turn and the observations (tool results) to the message list.
  4. If it gives a final answer (or calls a finish tool), stop.
  5. Check stop conditions every turn: step limit, token or cost budget, time limit, repeated identical actions (stop conditions).
  6. Repeat from step 2. The model is stateless; the growing message list is the loop's memory.

How it works: the ReAct pattern

  1. ReAct ("Reasoning and Acting", 2022) prompts the model to alternate:
    • Thought: what it knows and what to do next.
    • Action: one tool call with arguments.
    • Observation: the tool's result (added by your code).
  2. Repeat Thought → Action → Observation until it writes Final Answer.
  3. Why it works: writing the reasoning before each action keeps the model on track, and observations ground the next thought in real data instead of guesses.
  4. Today: tool-calling APIs implement this natively. Tool calls are the Actions, tool results the Observations, and models reason before acting (visibly or as hidden reasoning). You rarely parse "Action:" text any more, but the mental model is the same.
The loop that every agent framework wraps.
Loading the playground…

See it in code

Needs
Nothing: plain Python 3. The model's replies are scripted so the loop runs offline.

The original text-based ReAct loop, with a calculator tool and a step limit.

react_loop.py
import re
 
def calculator(expr: str) -> str:
    if not re.fullmatch(r"[\d\s+\-*/().]+", expr):     # only allow arithmetic
        return "error: not arithmetic"
    return str(eval(expr))
 
TOOLS = {"calculator": calculator}
 
# What a ReAct-style model writes on each turn (scripted here so it runs offline).
SCRIPT = [
    "Thought: I need the total cost of 3 tickets at 42.50.\nAction: calculator[3 * 42.50]",
    "Thought: Now add the 7.99 booking fee.\nAction: calculator[127.5 + 7.99]",
    "Thought: I have the total.\nFinal Answer: The total is 135.49.",
]
 
def fake_model(transcript: str, turn: int) -> str:
    return SCRIPT[turn]
 
transcript = "Question: What do 3 tickets at 42.50 cost with a 7.99 fee?\n"
for turn in range(5):                                    # the agent loop, with a step limit
    reply = fake_model(transcript, turn)
    transcript += reply + "\n"
    print(reply.splitlines()[-1])
    if "Final Answer:" in reply:                          # stop condition 1: the model is done
        break
    tool, arg = re.search(r"Action: (\w+)\[(.*)\]", reply).groups()
    observation = TOOLS[tool](arg)                        # act
    transcript += f"Observation: {observation}\n"         # observe, then loop
    print(f"Observation: {observation}")
Output
Action: calculator[3 * 42.50]
Observation: 127.5
Action: calculator[127.5 + 7.99]
Observation: 135.49
Final Answer: The total is 135.49.

The agent capstone builds the same loop with a real model and native tool calls.

A real example

Example

An IT helpdesk agent resetting access

An employee writes "I can't get into the expenses app". The agent loop runs:

  1. Thought: check the user's account. Action: get_user("emp-381"). Observation: active, in group "Finance-Read".
  2. Thought: the app needs "Expenses-Users". Action: check_group_membership(...). Observation: not a member.
  3. Thought: managers must approve group changes. Action: request_access(group, approver=manager). Observation: request #8812 created.
  4. Final answer: "You weren't in the Expenses group. I've asked your manager to approve access (request #8812)."

No one wrote that sequence in advance; the loop discovered it, within a 10-step limit.

The easy way

Start here

Write the loop yourself once (it's about 30 lines, see the capstone), with a finish tool and a step limit. Print every action and observation. Once you understand it, frameworks will make sense, because they all wrap this loop.

When you grow: the production setup

Production loops add tracing of every step, token and cost budgets, retries for flaky tools, context management for long runs (context engineering), checkpoints to resume after crashes (LangGraph), human approval for risky actions, and trajectory evals.

Trade-offs

What you gain

  • One simple loop handles open-ended, multi-step tasks.
  • Observations ground decisions in real data.
  • Easy to extend with more tools.

What it costs you

  • Each turn resends the growing history, so cost and latency grow with steps.
  • Can loop, wander or stop early without good stop conditions and prompts.
  • Harder to test than a fixed workflow.

When not to use it

  • The steps are known in advance: write a workflow.

Common mistakes

  • No step limit or budget.
  • Forgetting to append the model's own turn before the tool results, breaking the conversation.
  • Parsing free-text "Action:" lines when the API supports structured tool calls.
  • No clear "done" signal, so the agent keeps going or stops too early.
  • Not logging each step, so failures are a mystery.

Interview questions

Try answering out loud first, then open the model answer.

Explain the agent loop.

Model answer

The application sends the model the goal, system prompt, tool definitions and conversation so far. The model either returns a final answer or requests tool calls. The application executes the tools, appends the model's turn and the results to the history, checks stop conditions (step limit, budget, time, repeated actions), and calls the model again. The loop ends on a final answer, a finish tool, or a limit, returning the best available result.

What is ReAct and why was it influential?

Model answer

ReAct prompts a model to interleave reasoning traces with actions and observations: Thought, Action, Observation, repeated until a final answer. Interleaving lets reasoning guide tool use and observations correct reasoning, reducing hallucination compared to reasoning alone and making actions more purposeful than acting alone. Modern tool-calling APIs implement the same pattern natively.

Why does an agent's cost grow faster than linearly with the number of steps?

Model answer

Each step resends the full history, including all previous tool results, so input tokens per call grow with the step count, and total input tokens grow roughly quadratically. Prompt caching of the stable prefix, compacting or clearing old tool results, concise tool outputs and step limits keep this in check.

Quiz

Pick an answer to see whether it’s right and why.

  1. 01What are the steps of the basic agent loop?
  2. 02Where does the agent's "memory" of earlier steps live during a task?
  3. 03What does ReAct stand for?
  4. 04In ReAct, what is an Observation?
  5. 05Why does every agent loop need a step limit?
  6. 06With modern tool-calling APIs, how is ReAct usually implemented?

0 of 6 answered

Key terms

Agent loop
The cycle at the heart of every agent. The model picks an action, the action runs, the result is added to the context, repeat until a stop condition.
ReAct pattern
Reasoning and Acting. The model alternates a written thought, a tool action and the observed result until it can answer.
Observation
The result of an agent's action (a tool result) fed back into the model's context for its next decision.
Stop condition
A rule that ends an agent loop, such as a final answer, a maximum number of steps, a token or time budget, or an error.
Agent
A system where a model decides, in a loop, which tools to call and what to do next until a task is done.

Before and after

Progress is saved in this browser only.