Self Reflection
Let agents evaluate and refine their own responses before delivery
Enable reflection=True on an agent and it critiques its own answer before returning — improving quality within the same task.
from praisonaiagents import Agent
agent = Agent(
name="Analyst",
instructions="Provide thorough, accurate analysis.",
reflection=True,
)
agent.start("Summarise recent AI safety developments")The user asks a question; the agent drafts an answer, reflects on quality, then returns the improved response.
Quick Start
- Simple Usage
python from praisonaiagents import Agent agent = Agent( name="Analyst", instructions="Provide thorough, accurate analysis.", reflection=True, ) agent.start("Summarise recent AI safety developments") - With Configuration
python from praisonaiagents import Agent, ReflectionConfig agent = Agent( name="Analyst", instructions="Provide thorough, accurate analysis.", reflection=ReflectionConfig( min_iterations=1, max_iterations=3, llm="gpt-4o", prompt="Check accuracy, completeness, and clarity.", ), ) agent.start("Summarise recent AI safety developments")
How It Works
After each response, the agent runs one or more reflection passes (between min_iterations and max_iterations). Each pass asks whether the answer meets quality criteria; if not, the agent revises before returning.
If a reflection response cannot be parsed (for example, an OpenAI structured-output refusal, a content-filter block, or a truncation), the agent retries that pass. Retries are bounded by max_iterations — after that many failed passes, the agent returns the current draft un-reflected instead of continuing to retry.
Troubleshooting
Persistent parse failures terminate cleanly at max_iterations instead of looping forever.
Configuration Options
ReflectionConfig SDK ReferenceFull parameter reference for ReflectionConfig
Precedence ladder — choose the level you need:
# Level 1: Bool (simplest — enable with defaults)
agent = Agent(reflection=True)
# Level 2: ReflectionConfig (full control)
agent = Agent(reflection=ReflectionConfig(
min_iterations=1,
max_iterations=3,
llm="gpt-4o",
prompt="Evaluate for accuracy, completeness, and clarity.",
))| Option | Type | Default | Description | |
|---|---|---|---|---|
min_iterations | int | 1 | Minimum reflection passes | |
max_iterations | int | 3 | Maximum reflection passes | |
llm | `str \ | None` | None | Model for reflection (defaults to agent model) |
prompt | `str \ | None` | None | Custom evaluation prompt |
Choosing Reflection Strength
Best Practices
Disable for tool-heavy agents
Reflection adds an extra LLM pass per turn. Set reflection=False on agents that call tools frequently to keep latency down.
Pair with a dedicated reflection model
Use ReflectionConfig(llm="gpt-4o") when the main model is fast but you want a stronger critic.
Do not confuse with self_improve
reflection improves this answer within the task. self_improve captures reusable skills for next time — they compose independently.
Set max_iterations for cost control
Cap max_iterations in production to avoid runaway loops on open-ended prompts. It also bounds parse-error retries — if the reflection LLM keeps returning an unparseable response, the agent stops after max_iterations failed passes and returns the current draft un-reflected.