ReAct Prompting: Build AI Agents Step-by-Step (With Code)
ReAct prompting combines reasoning and acting in a continuous loop to help AI agents solve complex tasks. It alternates between internal thought, external actions, and observation of results to reduce hallucinations and improve accuracy. This framework mimics human tool use by allowing models to pause, reason, and verify before continuing. Developers use ReAct to build agents that browse the web, query APIs, or run calculations without hard-coded logic.
What Is ReAct Prompting?
ReAct prompting merges reasoning traces with task-specific actions in an interleaved loop. The term comes from the 2022 paper "ReAct: Synergizing Reasoning and Acting in Language Models" by Yao et al. Standard chain-of-thought prompting asks models to think step-by-step internally. ReAct goes further by allowing the model to execute actions like web searches or API calls between reasoning steps.
This synergy prevents hallucinations. When a model reasons alone, it might invent facts. When it acts alone, it lacks planning. ReAct combines both: the model thinks about what it needs, performs an action to get real data, observes the result, and then reasons again. Customer support bots use ReAct to check order status in real-time rather than relying on training data that might be outdated.
The ReAct Loop Explained
The ReAct framework follows a strict three-part cycle: Thought, Action, and Observation. First, the model produces a Thought, reasoning about the current state and planning the next step. Second, it generates an Action, typically a tool call with specific parameters. Third, it receives an Observation, which is the result of that action injected back into the context.
This loop repeats until the model determines it has enough information to provide a final Answer. Research shows this structure reduces factual errors by up to 40% on knowledge-intensive tasks compared to zero-shot prompting. This grounding effect is why ReAct outperforms pure reasoning on HotPotQA by 8% absolute accuracy. Each iteration appends the new Thought, Action, and Observation to the prompt history, creating a traceable reasoning path that developers can audit later.
ReAct Code Example: Python Implementation
Below is a minimal but complete ReAct agent using Python and the OpenAI API. This example uses a calculator tool, but you can extend it with search APIs or database queries.
```python import re import openai
def calculate(expression): return str(eval(expression))
def react_agent(query, max_iterations=5): system_prompt = """You are a ReAct agent. Solve the query using this format: Thought: [reasoning] Action: [tool_name][input] Observation: [result] ... Answer: [final answer] Available tools: calculate"""
history = f"Query: {query}\n"
for i in range(max_iterations): response = openai.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": history} ] ) output = response.choices[0].message.content history += output + "\n"
if "Answer:" in output: return output.split("Answer:")[-1].strip()
action_match = re.search(r"Action: (\w+)\[(.*?)\]", output) if action_match: tool, input_val = action_match.groups() if tool == "calculate": result = calculate(input_val) history += f"Observation: {result}\n" else: history += "Observation: Unknown tool\n" else: break
return "Max iterations reached"
Usage
result = react_agent("What is 15 * 24 + 100?") print(result) # Outputs: 460 ```
This code implements the loop manually. Modern frameworks like LangChain or LlamaIndex automate this scaffolding, but understanding the raw structure helps you debug behavior when agents fail.
ReAct vs. Chain-of-Thought: When to Use Which
ReAct excels when tasks require external data, but it adds latency and token cost. Use this comparison to decide:
| Feature | Standard Prompting | Chain-of-Thought | ReAct Prompting |
|---|---|---|---|
| Tool Use | None | None | Required |
| Reasoning Visibility | Low | High | High |
| Hallucination Risk | High | Medium | Low |
| Latency | Low | Low | High (multi-step) |
| Best For | Simple Q&A | Math/Logic puzzles | Research, data retrieval |
Choose standard prompting for creative writing or straightforward questions. Use Chain-of-Thought for multi-step math where all information exists in the context. Deploy ReAct when the model must verify facts against a search engine, query a SQL database, or interact with an API during the reasoning process.
Common ReAct Implementation Mistakes
Developers often fail to parse the Observation correctly. If your code does not inject the tool result back into the prompt history with the exact "Observation:" prefix, the model loses track of reality and hallucinates the result instead. Always validate that the Observation string appears in the next context window.
Another frequent error is ambiguous Action formatting. The model must output actions in a parseable format like `Action: tool_name[input]`. Without strict regex or JSON parsing, the agent outputs natural language like "I will search for this" and stalls. Use regex patterns like `r'Thought: (.?)\nAction: (.?)\[(.*?)\]'` to extract steps reliably. Finally, always implement a maximum iteration ceiling. Without it, agents can enter infinite loops when observations fail to satisfy stop conditions, burning tokens while repeating the same failed query.
Streamlining Complex ReAct Workflows
Writing effective ReAct prompts requires precise syntax and strict formatting. A misplaced bracket or vague instruction breaks the loop. Prompto rewrites your prompt on a single global hotkey before it reaches the AI. You draft the ReAct structure quickly, press the hotkey, and receive an optimized version that maintains the Thought-Action-Observation pattern while improving clarity.
Prompto's Windows desktop app works in any app — ChatGPT, Claude, Gemini, Perplexity, even your terminal — from one global hotkey. This means you can test ReAct patterns across different models without rewriting your prompt for each interface. Prompto optimizes prompts using a fast AI model and returns the rewrite in about a second, letting you iterate on agent behavior faster than manual editing allows. Better prompts yield tighter reasoning loops and fewer failed actions.
Frequently asked questions
Do I need coding skills to use ReAct prompting?
Basic Python helps for building agents, but you can test ReAct patterns in ChatGPT or Claude using structured text without writing code. The framework works anywhere you can define Thought, Action, and Observation steps.
Can ReAct work with GPT-4o or Claude 3.5 Sonnet?
Yes. ReAct is model-agnostic and works with any LLM that follows instructions. GPT-4o and Claude 3.5 Sonnet handle the reasoning traces particularly well due to their strong instruction-following capabilities.
How is ReAct different from function calling?
Function calling is the mechanism; ReAct is the reasoning pattern. ReAct uses function calling as its Action step but adds explicit Thought and Observation steps to create a traceable loop. Most modern APIs now support ReAct-style loops natively.
Why does my ReAct agent get stuck in loops?
Agents loop when they lack clear stop conditions or when observations fail to trigger the Answer phase. Always include explicit instructions like "If you have the final answer, output Answer: [result]" and set a maximum iteration limit in your code.