ReAct Agent¶
The ReAct pattern (Yao et al., 2022) interleaves reasoning (Thought) and acting (Action) steps. The agent iterates until it reaches a Final Answer or exhausts max_steps.
Usage¶
from brain.patterns import ReActAgent
agent = ReActAgent(
tools={"search": lambda q: f"[result: {q}]"},
max_steps=6,
)
result = agent.run("What is the capital of France?")
print(result.answer)
API Reference¶
brain.patterns.react.ReActAgent
¶
ReActAgent(tools: dict[str, Tool] | None = None, provider: LLMProvider | None = None, max_steps: int = 8, system_prompt: str | None = None)
Bases: BasePattern
ReAct agent: reason and act in alternating steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tools
|
dict[str, Tool] | None
|
mapping of tool name → callable(str) -> str. |
None
|
provider
|
LLMProvider | None
|
LLMProvider instance; if None, uses LocalEchoProvider (offline-safe). |
None
|
max_steps
|
int
|
maximum Thought/Action/Observation cycles before giving up. |
8
|
system_prompt
|
str | None
|
override the default ReAct system prompt. |
None
|
Source code in brain/patterns/react.py
run
¶
Source code in brain/patterns/react.py
Step Trace¶
Each iteration is captured in result.steps:
for step in result.steps:
print(f"[{step.index}] Thought: {step.thought}")
print(f" Action: {step.action}({step.action_input})")
print(f" Obs: {step.observation}")
When to Use¶
| Situation | Recommendation |
|---|---|
| Need real-time tool calls | ReAct |
| Output depends on external data | ReAct |
| Pure reasoning / no tools needed | Reflexion |
| Multiple data sources | MultiAgentOrchestrator |