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.

python
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.

Diagram

Self-Reflection Loop

improve

satisfied

Input

Agent

Draft answer

Reflect

Output

Quick Start

  1. 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")
  2. 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

Diagram
Reflection passAgentUserReflection passAgentUserloop[min to maxiterations]PromptGenerate draftEvaluate draftImprove or acceptFinal answer

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 Reference

Full parameter reference for ReflectionConfig

Precedence ladder — choose the level you need:

python
# 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.",
))
OptionTypeDefaultDescription
min_iterationsint1Minimum reflection passes
max_iterationsint3Maximum reflection passes
llm`str \None`NoneModel for reflection (defaults to agent model)
prompt`str \None`NoneCustom evaluation prompt

Choosing Reflection Strength

Diagram

No

Yes

Tight

Flexible

Need higher quality?

reflection=False
fastest

Latency budget?

reflection=True
defaults 1–3

ReflectionConfig
max_iterations=5


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.


Related