Agent Reflection

Enable agents to evaluate and improve their own responses through self-reflection

Reflection enables agents to critically evaluate their outputs and iteratively improve them before delivering the final response.

Diagram

Reflection Loop

Not good enough

Satisfactory

📥 Input

🧠 Generate

🔍 Evaluate

✅ Output

Quick Start

  1. Enable Reflection
    python
    from praisonaiagents import Agent
    
    agent = Agent(
        name="Reflective Agent",
        instructions="You write high-quality content",
        reflection=True  # Enable self-reflection
    )
    
    agent.start("Write a compelling product description")
  2. With Configuration
    python
    from praisonaiagents import Agent, ReflectionConfig
    
    agent = Agent(
        name="Quality Agent",
        instructions="You produce accurate, well-researched content",
        reflection=ReflectionConfig(
            min_iterations=1,    # Minimum reflection cycles
            max_iterations=3,    # Maximum reflection cycles
            llm="gpt-4o",        # Model for reflection
        )
    )

How Reflection Works

Diagram
ReflectorAgentUserReflectorAgentUserCheck accuracyCheck completenessCheck clarityalt[Response needsimprovement][Response is satisfactory]loop[Reflection Cycle (1 to max_iterations)]RequestGenerate responseEvaluate responseFeedback + suggestionsApprovedFinal response

The Reflection Process

PhaseWhat HappensPurpose
GenerateCreate initial responseFirst attempt
EvaluateAssess quality, accuracy, completenessFind issues
ImproveRefine based on feedbackBetter output
RepeatContinue until satisfactory or max iterationsQuality assurance

Configuration Options

python
from praisonaiagents import ReflectionConfig

config = ReflectionConfig(
    min_iterations=1,     # Minimum reflection cycles
    max_iterations=3,     # Maximum reflection cycles
    llm="gpt-4o",         # LLM for reflection (optional)
    prompt="Evaluate...", # Custom reflection prompt (optional)
)
OptionTypeDefaultDescription
min_iterationsint1Minimum reflection cycles
max_iterationsint3Maximum reflection cycles
llmstrNoneLLM for reflection (uses agent’s LLM if not set)
promptstrNoneCustom reflection prompt

Reflection Depth

Diagram

Iteration Depth

Quick tasks

Most tasks

Critical tasks

1️⃣ Light
min=1, max=1

2️⃣ Standard
min=1, max=3

3️⃣ Thorough
min=2, max=5

⚡ Fast

⚖️ Balanced

🏆 Quality

Light Reflection

python
# Single pass - quick check
agent = Agent(
    instructions="You answer questions",
    reflection=ReflectionConfig(
        min_iterations=1,
        max_iterations=1,
    )
)

Standard Reflection

python
# Default - good balance
agent = Agent(
    instructions="You write articles",
    reflection=True  # Uses defaults: min=1, max=3
)

Thorough Reflection

python
# Deep reflection for critical content
agent = Agent(
    instructions="You write legal documents",
    reflection=ReflectionConfig(
        min_iterations=2,
        max_iterations=5,
        llm="gpt-4o",  # Use powerful model
    )
)

Custom Reflection Prompts

Customize what the agent evaluates:

python
agent = Agent(
    instructions="You write technical documentation",
    reflection=ReflectionConfig(
        prompt="""Evaluate this response for:
1. Technical accuracy - Are all facts correct?
2. Completeness - Is anything missing?
3. Clarity - Is it easy to understand?
4. Code examples - Are they correct and runnable?

If any issues found, provide specific improvements."""
    )
)

When to Use Reflection

Diagram

Yes

No

Yes

No

Yes

No

🤔 Should I use reflection?

Quality critical?

✅ Use Reflection

Complex task?

Time sensitive?

⏭️ Skip Reflection

💡 Light Reflection


Reflection vs Planning

AspectPlanningReflection
WhenBefore executionAfter generation
PurposeDecompose taskImprove output
FocusStrategyQuality
IterationsOnceMultiple
Diagram

Improve

Done

📋 Task

📝 Planning

⚙️ Execute

🔍 Reflection

✅ Output


Best Practices

Set appropriate iteration limits

More iterations = better quality but slower. Use max_iterations=3 for most tasks, increase for critical content.

Use custom prompts for domain-specific evaluation

Generic reflection may miss domain-specific issues. Customize the prompt for your use case.

Combine with planning for complex tasks

Use planning to break down the task, then reflection to ensure quality output.

Can I combine reflection=True with stream=True?

Yes. Only the intermediate reflection passes are forced to non-streaming; the framework handles this for you and the final response still streams when stream=True.


Related