ReAct Agents
Reasoning + Acting. The loop that defined 2023.
1. Beyond the Chain
A "Chain" is a straight line: A -> B -> C.
But real life is not linear.
If I ask "Who is the CEO of the company that made the iPhone?", you can't just fetch one URL.
You need to:
- Search "Who made iPhone?" -> Apple.
- Search "CEO of Apple" -> Tim Cook.
This requires a Loop.
2. The ReAct Pattern
ReAct stands for Reasoning + Acting. It is a prompt structure that forces the model to "Think out loud" before it touches a tool.
The Prompt Format:
Question: {input}
Thought: What do I need to do?
Action: {tool_name}
Action Input: {tool_args}
Observation: {tool_result}
... (Repeat) ...
Final Answer: {answer}
3. The Execution Flow
- Model: "Thought: I need to find the maker of iPhone. Action: Search('Who made iPhone')"
- System: Executes Search. Returns "Apple Inc."
- Model: "Observation: Apple Inc. Thought: Now I need CEO of Apple. Action: Search('CEO Apple')"
- System: Executes Search. Returns "Tim Cook."
- Model: "Observation: Tim Cook. I have the answer. Final Answer: Tim Cook."
4. Pros and Cons
- Pros: Highly flexible. Can solve problems the developer never anticipated.
- Cons: Verbose (uses lots of tokens). Can get stuck in "Thought Loops" (I need to search... I need to search...).
5. Summary
ReAct was the breakthrough that made Agents possible. It turns the "Black Box" of inference into a transparent "Step-by-Step" log.
Key Intuition: "Don't just do it. Think about it first."