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.
What is ReAct: Thought → Action → Observation
ReAct is a structured reasoning pattern where the agent loops through three steps:
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:
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.
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.
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.
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.
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.
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.
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.
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
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 →