Agentic AI Notebook
Phase 17

ReAct

~3 min read

Concept & How It Works

  • Key points are in the visual diagram above.

Why Does It Exist?

Pure planning can't adapt to surprises. Pure acting without thinking leads to errors. ReAct combines both in every step — the most widely used agent pattern because it's simple, interpretable, and handles unexpected tool results gracefully.

Real-World Analogy

ReAct is a detective at a crime scene: 'The window is broken (observation). Likely forced entry (thought). Let me check for fingerprints (action).' Each step builds on the last.
Loading diagram...

Visual Workflows

What is ReAct?

Loading diagram...

Example

Scenario

Q: 'What is the stock price of the company that makes the iPhone?' Thought: iPhone is made by Apple. Action: get_stock_price('AAPL'). Observation: $178.50. Final Answer: $178.50.

Solution

In Agent Design Patterns, apply ReAct to this scenario: Q: 'What is the stock price of the company that makes the iPhone?' Thought: iPhone is made by Apple. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Do this before moving to the next module — reading alone is not enough.

Open the Code Walkthrough below and run it locally. Change one parameter related to ReAct (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where ReAct happens in the code.

ReAct
1REACT_SYSTEM = """You are a helpful agent. For each step:21. Thought: reason about what to do  # agent reasons about what to do next32. Action: call a tool  # agent picks a tool to call43. After seeing the result, continue reasoning5
6Repeat until you can give a Final Answer."""7
8def react_agent(query: str, tools: list, max_steps: int = 10) -> str:  # define a reusable function9    messages = [10        {"role": "system", "content": REACT_SYSTEM},11        {"role": "user", "content": query},12    ]13    for step in range(max_steps):14        response = client.chat.completions.create(  # call the API15            model="gpt-4o", messages=messages, tools=tools,16        )17        msg = response.choices[0].message18        if not msg.tool_calls:19            return msg.content  # return the result20        messages.append(msg)21        for tc in msg.tool_calls:22            result = execute_tool(tc)23            messages.append({"role": "tool", "tool_call_id": tc.id, "content": str(result)})24    return "Max steps reached"  # return the result

Commands to Remember

Commands to Remember

  • pip install langchain langchain-openai # patterns work with any LLM SDK
  • python react_agent.py # run a ReAct-style agent loop
  • pip install tenacity # retry logic for agent steps

Common Mistakes

  • No Thought step — agent acts without reasoning
  • Not logging traces — impossible to debug
  • Max steps too low — agent gives up on valid tasks
  • Hallucinated observations — LLM fakes tool results
  • Using ReAct for predictable repetitive workflows

Cheat Sheet

Quick recap — the most important points from this module.

Cheat Sheet

quick ref
  • Thought → Action → Observation
  • Default agent pattern
  • Log full traces
  • max_steps=10-15
  • Native tool calling
  • Debug via trace reading