Have you ever asked an AI to look up real-time data and got hallucinated numbers? Or to calculate a shipping cost and got it wrong because it couldn't query the courier API? The problem isn't the model — it's that you're asking a brain to do hands-on work. You need a loop: think, then act, then think again. That's ReAct prompting.
At Meteora Web, we build AI systems for real clients — and we've seen hundreds of prompts fail because the model reasons in a vacuum. ReAct (Reason + Act) is the technique that lets the AI use external tools (APIs, databases, calculators) while reasoning, returning answers grounded in facts, not fantasy. In this guide, we explain how it works, when to use it, and how to implement it right away with Python and OpenAI.
What Is ReAct Prompting and How Does the Reasoning-Action Loop Work?
Imagine a detective solving a case. They don't just write a novel: they form a hypothesis (Thought), go out to gather evidence (Action), analyze the results (Observation), then form a new hypothesis. ReAct formalizes this: the model generates a thought, decides an action (e.g., call an API, query a database), receives the observation, and repeats until enough information is gathered.
Each step follows a clear structure:
- Thought: the model reasons about what to do next.
- Action: specifies which tool to use and with what parameters (e.g., "search: latest oil price").
- Observation: the result of the action is fed back into the context.
- Loop continues until a Final Answer is produced when the model decides it has enough data.
This loop is the foundation of all modern AI agents (LangChain, AutoGPT). But you can implement it with a simple OpenAI API call and a while loop. The key is a system prompt that instructs the model to produce structured output (e.g., JSON with thought, action, action_input, observation).
Sponsored Protocol
When Should You Use ReAct Instead of a Direct Prompt?
ReAct isn't always needed. For simple factual questions ("What is the capital of France?") a direct prompt is faster and cheaper. But here are the cases where the loop changes the game:
- Real-time data retrieval: stock prices, weather, inventory. The model alone doesn't know current data.
- Complex calculations: many LLMs make errors on multi-digit arithmetic. With ReAct, you can call a calculator (e.g., Python eval) for exact results.
- Multi-step reasoning with verification: e.g., "Check if product X is in stock, then calculate the price with tax and shipping." Each step uses a different tool.
- Reducing hallucinations: by forcing the model to rely on real observations, you drastically lower the risk of fabricated information.
We often apply this for e-commerce clients: the bot must check inventory (ERP API), calculate shipping (carrier API), and then respond. Without ReAct, the model invents data. With the loop, every piece of information is verified.
Sponsored Protocol
How to Implement ReAct Prompting with OpenAI's API
Core structure: system prompt and output format
To make the model follow the loop, we need to instruct it to produce parseable output. We use JSON with fields thought, action, action_input, and observation. Here's an example system prompt:
You are an assistant that answers using the ReAct method.
Each of your responses must be a valid JSON with these fields:
- thought: your current reasoning (string)
- action: the name of the tool to call or "final_answer" if done
- action_input: parameters for the tool (string)
Available tools:
- search: web search query
- calculator: math expression to evaluate
Rules:
1. Take one step at a time.
2. After each action, you will receive an "observation" field with the result.
3. When you have enough data, use action "final_answer" and put the answer in action_input.
Execution loop in Python
The following code implements a minimal example with OpenAI. Copy, paste, and replace your API key.
import openai
import json
import re
openai.api_key = "sk-..."
def call_llm(messages):
response = openai.ChatCompletion.create(
model="gpt-4",
messages=messages,
temperature=0
)
return response.choices[0].message.content
def extract_json(text):
match = re.search(r'\{.*\}', text, re.DOTALL)
if match:
return json.loads(match.group())
return None
def execute_action(action, action_input):
# Simulate tools: in production call real APIs
if action == "search":
return f"Fake results for '{action_input}': olive oil price = 12 €/L"
elif action == "calculator":
try:
result = eval(action_input)
return str(result)
except:
return "Calculation error"
else:
return "Unknown action"
# Example: a question requiring calculation + search
question = "How much does 3 liters of olive oil cost if the current price per liter is the latest?"
system_prompt = """You are an assistant that answers using the ReAct method... (as above)"""
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": question}
]
max_loop = 5
for i in range(max_loop):
output = call_llm(messages)
step = extract_json(output)
if not step:
print("JSON parsing error")
break
print(f"Step {i+1}:", json.dumps(step, indent=2))
if step["action"] == "final_answer":
print("\nFinal answer:", step["action_input"])
break
observation = execute_action(step["action"], step["action_input"])
messages.append({"role": "assistant", "content": output})
messages.append({"role": "user", "content": f"Observation: {observation}"})
else:
print("Max loop reached")
This code is intentionally simple to illustrate the mechanism. In production you would use real tools (search API, safe calculator, database connection) and more robust parsers.
Sponsored Protocol
Real-world example with a search API
If you want to integrate a real search API (e.g., SerpAPI or Tavily), replace the execute_action function with real HTTP calls. We often use Tavily for its low latency and reasonable cost.
Sponsored Protocol
What Are the Costs and Risks of ReAct Prompting?
Each loop step consumes input tokens (the entire conversation history) and output tokens. With GPT-4, a 3-5 step loop can easily cost 10-20 cents per response. For low-volume B2B applications it's acceptable; for public chatbots you need to optimize or use smaller models (GPT-4o-mini).
Risks to manage:
- Infinite loops: the model might keep calling actions without reaching a final answer. Always set a maximum number of iterations.
- Unsafe tools: if you allow code execution (e.g., eval), sanitize the input. Never execute AI-generated code without a sandbox.
- Residual hallucinations: if the observation is empty or wrong, the model may still invent. Verify that the action was executed correctly.
At Meteora Web, we recommend starting with a test budget: 10 ReAct queries, compare quality against standard prompts, and only then decide if the extra cost is worth the improvement.
Sponsored Protocol
What to Do Now
- Try the example code: get your OpenAI API key, copy the script above, and test it with a question that requires search or calculation.
- Add real tools: replace the simulated functions with calls to real APIs (weather, prices, database). Start with one tool (e.g., a calculator) to get comfortable.
- Measure cost and accuracy: compare the same question with and without ReAct. How often do you get a correct answer? What does it cost on average?
- Limit the loop: set a maximum of 3-5 iterations in your code to avoid surprises.
- Go deeper: read our main guide on Advanced Prompt Engineering for other complementary techniques.
ReAct is not magic — it's discipline. It teaches the AI not to shoot in the dark, but to seek light one step at a time. And once you try it, you'll understand why they call it the method that turns a chatbot into an agent.