Reflexion Agent¶
Reflexion (Shinn et al., 2023) generates an answer, critiques it, then refines. The agent stops early if the critique finds no issues.
Usage¶
from brain.patterns import ReflexionAgent
agent = ReflexionAgent(max_iterations=3, early_stop=True)
result = agent.run("Explain the trade-offs between RAG and fine-tuning")
print(result.answer)
print(f"Refined {result.iterations} time(s)")
API Reference¶
brain.patterns.reflexion.ReflexionAgent
¶
ReflexionAgent(provider: LLMProvider | None = None, max_iterations: int = 3, critique_provider: LLMProvider | None = None, early_stop: bool = True)
Bases: BasePattern
Reflexion agent: generate → self-critique → reflect → retry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
LLMProvider | None
|
LLMProvider instance; defaults to LocalEchoProvider. |
None
|
max_iterations
|
int
|
maximum generate→critique→reflect cycles. |
3
|
critique_provider
|
LLMProvider | None
|
separate provider for the critique step (defaults to |
None
|
early_stop
|
bool
|
stop early if critique returns PASS (default: True). |
True
|
Source code in brain/patterns/reflexion.py
run
¶
Source code in brain/patterns/reflexion.py
Critique Format¶
The internal LLM is prompted to produce:
If the LLM does not follow this format, the critique is treated as PASS (graceful degradation).
When to Use¶
| Situation | Recommendation |
|---|---|
| Answer quality is critical | Reflexion |
| Need iterative self-correction | Reflexion |
| Real-time tool calls needed | ReAct |
| Multiple data sources | MultiAgentOrchestrator |