The problem: agents act blind

In Lesson 08, you learned function calling: tell the LLM to call a tool, it calls the tool, you give it the result. But this is reactive. The model can't reason about what it learned. It can't say "I fetched the price, now I need to fetch the competitor's price, then compare."

A simple function-calling loop is like giving instructions to someone with no working memory—they execute, get results, but never pause to think.

The gap:
Function calling alone lets agents act. ReAct adds reasoning. The agent thinks, acts, sees the result, thinks again. This transparency is crucial for complex, multi-step tasks and for debugging when the agent fails.

What is ReAct: Thought → Action → Observation

ReAct is a structured reasoning pattern where the agent loops through three steps:

Thought — The agent reasons about what it knows and what it needs. "I need to find two stock prices and compare them."
Action — The agent calls a tool. "fetch_stock_price(HDFCBANK)"
Observation — The agent reads the result. "HDFCBANK: ₹2,850 per share"

Then it loops: Thought → Action → Observation → Thought → Action → ... until it reaches "Final Answer."

The key difference from Lesson 08: the model's output includes all three. You parse it. The model can see its own reasoning and observations in the same response.

System prompt that encourages ReAct

To get a model to use ReAct, you need to instruct it. Here's a working template:

react_system_prompt.py System prompt that elicits ReAct format

The prompt is explicit: use this format. When the LLM sees it, it learns to structure its output. This is different from function-calling schemas—it's a reasoning template.

Worked example: investment decision (4 iterations)

Arjun asks: "Should I invest in HDFCBANK or SouthBank India stock this week?" The agent runs ReAct for 4 iterations.

📊
Real scenario
Arjun's investment decision (SouthBank India, Mumbai)

Arjun is in Mumbai and wants to compare two financial stocks to decide where to invest ₹50,000 this week. The agent must fetch prices, fetch recent news, and recommend one.

react_trace.py Full ReAct trace with 4 iterations
ReAct trace (excerpt)
Thought: Arjun wants to decide between HDFCBANK and SouthBank India... Action: fetch_stock_price(HDFCBANK) Observation: HDFCBANK: ₹2,850 per share (up 2.3% today) Thought: Good. Now I need SouthBank India's price... Action: fetch_stock_price(SOUTHBANK) Observation: SouthBank India: ₹1,420 per share (down 1.1% today) Final Answer: HDFCBANK is the better choice for this week...

Parsing ReAct output: regex for Thought/Action/Observation

The LLM outputs one big string with multiple Thought/Action/Observation blocks. You need to parse it to extract each block and execute the actions.

parse_react.py Regex to extract Thought, Action, Observation blocks
Expected output
Iterations: 4 --- Iteration 1 --- Thought: Arjun wants to decide between HDFCBANK... Action: fetch_stock_price(HDFCBANK) --- Iteration 2 --- Thought: Good. HDFCBANK is at ₹2,850... Action: fetch_stock_price(SOUTHBANK) Final Answer: HDFCBANK is the better choice...

Loop termination: max iterations, Final Answer, safety caps

ReAct loops can run forever if not bounded. Three safety mechanisms:

1. Max iterations

Hard cap: stop after N loops regardless of state. Typical: 5-10 iterations.

2. "Final Answer:" token

When the LLM outputs "Final Answer:", break the loop and return that string. This is the intended exit.

3. Tool-call limit

Some agents have budgets: "You can call tools at most 20 times." Prevents runaway execution.

react_loop_with_safety.py ReAct loop with termination conditions

When ReAct helps (and when it's overkill)

ReAct shines for:

  • Multi-step research: "Compare 3 competitors' pricing and recommend the cheapest." Needs 6-9 tool calls across 3 iterations.
  • Conditional logic: "If the stock price is above ₹2,500, fetch analyst reports. Otherwise, skip."
  • Debugging & transparency: You see the agent's reasoning. If it makes a wrong call, you see the thought that led to it.
  • Complex domains: Finance, legal research, data analysis—where reasoning matters.

ReAct is overkill for:

  • Simple lookups: "Get the current balance for account SB-2026-4521." One tool call, done. Lesson 08's function calling is enough.
  • Streaming to users: ReAct produces verbose output. If you're building a chat where users see the agent think, consider hiding intermediate steps.
  • Latency-critical systems: Each loop iteration adds a round-trip to the LLM. For real-time applications, prefer simpler patterns.

Kenya example: find cheapest electricity tariff for M-Pesa kiosk

Aisha runs an M-Pesa money-transfer kiosk in Nairobi. She uses solar panels during the day but buys grid power at night. She needs to find the 3 cheapest residential electricity tariffs in Nairobi and calculate which is best for her 10-hour daily usage.

⚡
Real scenario
Aisha's M-Pesa kiosk power bill (Nairobi)

Aisha's kiosk uses 30 kWh per day on grid power. She compares 3 providers: KPLC, Umeme, KECO. The agent runs ReAct to fetch rates, calculate costs, and recommend the cheapest.

The ReAct loop for Aisha:
Thought: "I need 3 providers' rates and Aisha's usage profile."
Action: fetch_electricity_rate(KPLC)
Observation: "KPLC: ₹8.50 per kWh for residential"
Thought: "Got KPLC. Now Umeme."
Action: fetch_electricity_rate(Umeme)
Observation: "Umeme: ₹8.20 per kWh"
Thought: "Umeme is cheaper. Now KECO."
Action: fetch_electricity_rate(KECO)
Observation: "KECO: ₹7.80 per kWh"
Thought: "KECO is cheapest. Let me calculate annual costs for Aisha's 30 kWh/day usage."
Final Answer: "KECO at ₹7.80 per kWh saves Aisha ₹2,190 per month vs KPLC."

ReAct vs. native function-calling loop (Lesson 07 recap)

Lesson 07 showed native function calling: you define tool schemas, the LLM decides to call them, you handle the response. It's simpler and faster.

Feature Function Calling (Lesson 07) ReAct (Lesson 09)
Setup complexity Medium (JSON schemas) Low (just a system prompt)
Output format Structured (tool name + args as fields) Text (parse with regex)
Transparency Low (you see actions, not reasoning) High (you see thoughts before each action)
Flexibility Fixed tool list Agent can improvise, ask clarifying questions
Best for Simple, multi-tool workflows Complex reasoning, debugging, auditing

Win for ReAct: Transparency. You literally see the agent's thought process. This is invaluable in regulated domains (finance, healthcare) where you must explain decisions.

How AI systems use ReAct

Every frontier AI agent system uses variants of ReAct. OpenAI o1 uses extended thinking (a form of ReAct where the model spends 500ms reasoning before responding). Claude uses agentic loops with explicit Thought blocks. When you see an AI research agent solve a complex problem step-by-step, it's usually ReAct or a close relative.

The pattern is universal because it works: reasoning before acting reduces mistakes. In humans, this is "think before you act." In LLMs, we have to make it explicit with prompts.

Agentic patterns Reasoning loops Chain of thought Transparency & auditing
🎯 Mini Challenge: ReAct for a loan decision

Scenario: Priya at SouthBank India receives a loan application from a customer. She wants an AI agent to run ReAct and decide: approve, request more info, or deny.

Task: Write a ReAct system prompt for a loan-decision agent. Include Thought / Action / Observation format. List 4 tools the agent should use: fetch_credit_score, fetch_income_docs, fetch_loan_history, and calculate_debt_to_income_ratio.

Then write a sample Thought → Action → Observation block for the agent's first iteration.

Hint: Start with: "You are a loan underwriter for SouthBank India. You have access to tools: fetch_credit_score(...), fetch_income_docs(...), etc. Use the ReAct format: Thought, Action, Observation. Always start by thinking about what documents you need."

Test your knowledge

Question 1 of 5
In ReAct, what is the purpose of the "Thought" step?
Question 2 of 5
Which pattern is ReAct most useful for?
Question 3 of 5
How do you tell a ReAct agent to stop looping?
Question 4 of 5
What is one advantage of ReAct over native function calling (Lesson 07)?
Question 5 of 5
What is a realistic max iteration limit for a ReAct loop?
0
/5
Great! You understand ReAct reasoning loops and when to use them.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You've mastered ReAct reasoning.
Now build agents that think at scale.

Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Agentic Engineer →