Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 39 ยท Planning and Reasoning

The ReAct Pattern: Reason, Act, Observe

Learn the ReAct agent pattern that interleaves reasoning with tool calls, with a runnable Python stand-in, a parser, a trace view and the common ways ReAct loops fail.

ReAct, short for reasoning and acting, is the pattern behind most tool-using agents. The model alternates between three kinds of output: a thought (what do I know and what should I do next), an action (a tool call with arguments) and then, supplied by your code, an observation (the tool's result). The loop repeats until the model produces a final answer. It is the perceive, think, act loop with the thinking made explicit.

Reasoning makes acting better because the model plans each call from what it has seen. Acting makes reasoning better because tool results ground the thoughts in facts instead of guesses.

What a ReAct trace looks like

For the question "What is 15% of the price of the cheapest laptop?", a trace could read:

TEXT
Thought: I need the laptop prices first.
Action: search_products {"category": "laptop"}
Observation: [{"name": "Nova 13", "price": 899}, {"name": "Atlas 15", "price": 1299}]
Thought: The cheapest is Nova 13 at 899. Now compute 15 percent.
Action: calculator {"expression": "899 * 0.15"}
Observation: 134.85
Thought: I have what I need.
Final Answer: 15% of the cheapest laptop (Nova 13, $899) is $134.85.
flowchart LR
  T["Thought"] --> A["Action"]
  A --> O["Observation"]
  O --> T
  T --> F["Final answer"]

Observations feed the next thought; a thought can also end the loop.

Modern tool calling versus text ReAct

Early ReAct agents printed Action: lines in plain text and used regular expressions to parse them. Today most providers support native tool calling, where the model returns structured tool-call objects (see what are tools). The pattern is the same; only the transport changed. Native calls are more reliable, so use them in real systems. Text ReAct is still the best way to learn the pattern, because you can see every piece.

A runnable ReAct loop

The model below is a scripted stand-in that returns the text a real model might produce. Everything else, the parser, the tools and the loop, is real code you could keep when you swap in an actual model.

Python A ReAct loop with a scripted model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Study the loop: it parses, executes through a registry, appends the observation, and stops either on a final answer or at the step limit. The same skeleton appears in your first agent loop.

Handling bad model output

Models sometimes produce an action you cannot parse, a tool that does not exist, or arguments in the wrong shape. A robust ReAct runner turns each of those into an observation, so the model can correct itself, as in the could not parse branch above. Test those branches on purpose.

Python Recover from a malformed action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Where ReAct goes wrong

  • Loops: the model repeats the same action. Detect identical consecutive calls and stop or change strategy (stop conditions).
  • Short-sightedness: it chooses each step locally and wanders on long tasks. For those, plan first (task decomposition).
  • Context growth: every thought and observation stays in history. Trim or summarize old steps.
  • Hallucinated observations: with text ReAct, a model may write its own Observation: line. Stop generation at that word, or use native tool calls.
  • Over-trusting observations: tool results (web pages, emails) can contain hostile instructions. See guardrails and prompt injection.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three repeating parts of ReAct?

    The model reasons, calls a tool, and then receives the tool result as an observation.
  2. 2. Who produces the Observation?

    Observations are real tool results. If the model writes them, they are made up.
  3. 3. What should the runner do when an action cannot be parsed?

    Feedback keeps the loop going and lets the model self-correct.
  4. 4. Which is a typical ReAct failure on long tasks?

    Without a plan, the model can lose the thread on multi-stage tasks.

Summary

  • ReAct alternates thoughts, tool actions and real observations until a final answer.
  • Native tool calling is the modern transport; text ReAct is a good way to learn.
  • The runner parses, executes through a registry, feeds back results and enforces a step limit.
  • Turn parse errors and unknown tools into observations, not crashes.
  • Guard against loops, context growth, made-up observations and hostile tool output.

Next: Planning and Task Decomposition