react
**ReAct: Reasoning and Acting**
**What is ReAct?**
ReAct (Reasoning and Acting) is an agent framework that interleaves reasoning traces with tool-use actions, enabling more reliable and interpretable problem-solving.
**The ReAct Loop**
```
Question: What is the population of the capital of France?
Thought 1: I need to find the capital of France first.
Action 1: search("capital of France")
Observation 1: Paris is the capital of France.
Thought 2: Now I need to find the population of Paris.
Action 2: search("Paris population")
Observation 2: Paris has approximately 2.1 million people in the city proper.
Thought 3: I now have the answer.
Answer: The population of Paris, the capital of France, is approximately 2.1 million.
```
**Key Components**
**Reasoning (Thought)**
- Plan what to do next
- Interpret observations
- Decide if more information is needed
**Acting (Action)**
- Execute tool calls
- Retrieve information
- Take environment actions
**Observing**
- Process tool outputs
- Update understanding
- Continue or terminate
**Implementation**
```python
def react_agent(question: str, tools: dict) -> str:
prompt = f"Question: {question}
"
while True:
response = llm.generate(prompt + "Thought:")
thought = parse_thought(response)
prompt += f"Thought: {thought}
"
if "Answer:" in thought:
return thought.split("Answer:")[-1]
action = parse_action(response)
prompt += f"Action: {action}
"
observation = tools[action.tool](action.args)
prompt += f"Observation: {observation}
"
```
**ReAct vs Other Approaches**
| Approach | Reasoning | Acting | Trace |
|----------|-----------|--------|-------|
| Standard prompting | Implicit | No | No |
| Chain-of-Thought | Explicit | No | Yes |
| Tool use only | No | Yes | No |
| ReAct | Explicit | Yes | Yes |
**Benefits**
- Interpretable decision process
- Error recovery through reasoning
- Combines strengths of reasoning and tool use
- Better than either approach alone
**Available in Frameworks**
- LangChain ReAct agent
- LlamaIndex ReAct agent
- AutoGen with ReAct pattern