ReAct (Reason + Act)
Mental model
ReAct makes the agent 'think out loud' on a scratchpad before picking up a tool.
If you force an LLM to immediately choose a tool, it often guesses poorly. ReAct (Reasoning and Acting) is a prompt pattern that forces the model to write out its thought process step-by-step before it outputs the JSON tool call. These 'thinking' tokens give the model space to plan, leading to drastically better tool choices.
Deep dive
Language models don't have internal hidden thoughts; their 'thinking' happens only when they generate output tokens. If a model must immediately output a tool call to answer a complex question, it has to decide everything in zero steps. It often fails or calls the wrong tool.
So far: Forcing immediate action leads to poor decisions.
The ReAct pattern solves this by injecting a mandatory 'Thought' step before every 'Action'. By prompting the model to explicitly write down 'Thought: I need to find X first...', the model generates tokens that serve as a working memory or scratchpad.
So far: Forcing the model to output a 'Thought' first gives it space to plan.
Once the model writes its thought, it outputs the 'Action' (the tool call). Your application runs the tool and returns the 'Observation'. The model reads this, generates a new 'Thought', and the cycle continues. This interleaved reasoning and acting allows the model to self-correct if a tool fails.
ReAct is a specific loop pattern: Thought → Action → Observation, repeating until the final answer.
Code examples
The ReAct Prompt Pattern
Answer the following questions as best you can. You have access to tools.
Use the following format strictly:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the tool name to run
Action Input: the JSON input to the tool
Observation: the result of the action
... (this Thought/Action/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
This is the classic ReAct system prompt. It explicitly teaches the model the state machine it must follow, forcing the 'Thought' step before it is allowed to output an 'Action'.
Modern implementation via native tool calling
# Modern models have native tool-calling, so we don't need raw text parsing.
# We just add 'thought' as a required field in the tool's JSON schema!
tools = [{
'type': 'function',
'function': {
'name': 'search_database',
'parameters': {
'type': 'object',
'properties': {
'thought': {
'type': 'string',
'description': 'Your step-by-step reasoning for WHY you are doing this search.'
},
'query': {
'type': 'string'
}
},
'required': ['thought', 'query']
}
}
}]
Instead of parsing messy text blocks, modern AI engineering enforces ReAct by simply making 'thought' a required argument in every tool schema. The model must generate the reasoning string before it generates the actual query.
Common mistakes
- Thinking modern models don't need ReAct.: While native tool-calling replaced the need to parse raw text 'Action' strings, models still benefit massively from generating reasoning tokens before they act. Adding a 'thought' field to JSON schemas is the modern ReAct.
- Hiding the thought process from the context window.: If you don't append the model's 'Thought' to the message history, it forgets its own reasoning on the next loop iteration. The scratchpad must persist.
Glossary
- ReAct
- A prompting technique that forces the AI to write down its thought process step-by-step before it tries to take any action.
- Thought
- The mandatory planning step where the AI creates a temporary scratchpad to figure out what it needs to do next.
- Observation
- The result returned after a tool is used, which the AI reads to decide its next step in the loop.
Recall questions
- Why does the ReAct pattern improve an LLM's ability to use tools?
- What are the three core steps in a single ReAct loop iteration?
- How is ReAct most reliably implemented with modern (2024+) function-calling models?
Questions & answers
Explain the ReAct framework and why it is superior to a simple Action-Only agent.
ReAct interleaves reasoning (thinking out loud) and acting (tool use). An action-only agent must decide the perfect tool and arguments immediately based on the prompt. ReAct forces the model to generate 'thought' tokens first, giving it a scratchpad to plan, deduce missing information, and handle errors from previous observations.
Approach: By forcing the model to write out its logic, it uses the generated tokens to plan its next steps, leading to much higher success rates on complex tasks.
Your agent keeps passing incorrect, hallucinated IDs to the database lookup tool instead of finding the ID first. How do you fix this without changing the model?
Implement the ReAct pattern, usually by requiring a 'thought' field in the tool schema. Force the model to explain *where* it is getting the ID from before it populates the ID field. This breaks the hallucination by forcing the model to realize it doesn't have the ID yet.
Approach: Diagnose the issue as skipping a step. Propose reasoning tokens (ReAct) to force the model to plan its dependencies before executing the tool call.