ReAct Agent
Combine Reasoning and Acting in an interleaved loop where the LLM thinks about what to do, takes an action, observes the result, and repeats.
Overview
ReAct (Reasoning + Acting) is a foundational agent pattern where the LLM operates in a Thought β Action β Observation loop. At each step, the model reasons about the current situation (Thought), decides what tool to invoke (Action), receives the result (Observation), and then reasons about the next step.
This interleaved approach lets LLMs solve complex multi-step tasks that require both reasoning and real-world interaction.
When to Use
- Tasks requiring multiple steps with external data gathering
- Problems where the path to the answer isnβt known in advance
- Scenarios needing both reasoning and action (research, debugging, data analysis)
- Building general-purpose AI assistants that can use multiple tools
Architecture
flowchart TB
Q[User Task] --> T1[Thought 1<br>I need to find...]
T1 --> A1[Action 1<br>search'topic']
A1 --> O1[Observation 1<br>Search results...]
O1 --> T2[Thought 2<br>Now I know X, I need...]
T2 --> A2[Action 2<br>lookup'detail']
A2 --> O2[Observation 2<br>Detail found...]
O2 --> T3[Thought 3<br>I have enough info to answer]
T3 --> F[Final Answer]
style T1 fill:#1c2128,stroke:#bc8cff,color:#e6edf3
style T2 fill:#1c2128,stroke:#bc8cff,color:#e6edf3
style T3 fill:#1c2128,stroke:#bc8cff,color:#e6edf3
style A1 fill:#1c2128,stroke:#3fb950,color:#e6edf3
style A2 fill:#1c2128,stroke:#3fb950,color:#e6edf3
style O1 fill:#1c2128,stroke:#58a6ff,color:#e6edf3
style O2 fill:#1c2128,stroke:#58a6ff,color:#e6edf3
How It Works
The ReAct loop follows a strict pattern:
- Thought: The LLM reasons about what it knows and what it needs to do next
- Action: The LLM selects a tool and provides arguments
- Observation: The tool executes and returns results
- Repeat until the LLM decides it has enough information to give a final answer
Implementation
Gotchas & Best Practices
Without a step limit, agents can loop endlessly β retrying failed actions, going in circles, or exploring irrelevant tangents. Always set a max_steps limit and handle graceful termination.
If a tool returns an error, the agent must be able to recover. Include error handling guidance in the system prompt: βIf a tool fails, try a different approach or explain the limitation.β
Each ReAct step requires an LLM call. A 5-step agent loop means 5x the cost and latency. Monitor step counts and consider caching or pre-computing when possible.
Use structured output (JSON mode) for the Thought/Action/Action Input format. This makes parsing more reliable than regex-based extraction from free text.
Feed the full trace (all previous Thought/Action/Observation triplets) back to the LLM at each step. This gives it context about what itβs already tried.
Variations
- ReAct β Basic reasoning + acting loop
- Reflexion β Adds self-reflection after failures
- Plan-and-Execute β Plan all steps first, then execute
- LATS β Language Agent Tree Search with backtracking
- Multi-Agent β Multiple specialized ReAct agents collaborating