Async Self-Reflection

Self-reflection now works in async agents using the unified dispatch path

Self-reflection enables agents to evaluate and improve their responses in async execution, bringing full feature parity with sync agents.

python
import asyncio
from praisonaiagents import Agent

agent = Agent(
    name="Writer",
    instructions="Write concise product blurbs.",
    self_reflect=True,
    min_reflect=1,
    max_reflect=3,
)

async def main():
    await agent.achat("Write a one-line blurb for a smart mug.")

asyncio.run(main())

The user asks for copy; the agent drafts, self-reflects, and improves the answer before replying.

Diagram

Async Self-Reflection

Not good enough

Satisfactory

📋 Async Request

🤖 Agent

🧠 Generate Response

🔍 Self-Reflect

✅ Final Response

Quick Start

  1. Basic Async Reflection

    Enable reflection in async agents with simple boolean flag:

    python
    import asyncio
    from praisonaiagents import Agent
    
    agent = Agent(
        name="Writer",
        instructions="Write concise product blurbs.",
        self_reflect=True,
        min_reflect=1,
        max_reflect=3,
    )
    
    async def main():
        result = await agent.achat("Write a one-line blurb for a smart mug.")
        print(result)
    
    asyncio.run(main())
  2. Custom Reflection Criteria

    Configure reflection with custom criteria for async agents:

    python
    import asyncio
    from praisonaiagents import Agent, ReflectionConfig
    
    agent = Agent(
        name="Technical Writer",
        instructions="Create technical documentation.",
        self_reflect=ReflectionConfig(
            min_reflect=2,
            max_reflect=5,
            criteria="Is the response clear, accurate, and complete?"
        )
    )
    
    async def main():
        result = await agent.achat("Explain async/await in Python.")
        print(result)
    
    asyncio.run(main())

How It Works

Self-reflection in async agents follows the same process as sync agents:

Diagram
LLMReflectionAgentUserLLMReflectionAgentUserawait agent.achat()Generate initial responseResponseEvaluate responseNot good enoughGenerate improved responseBetter responseEvaluate againSatisfactoryFinal response
ComponentPurposeAsync Behavior
Initial GenerationCreate first responseUses await agent.achat()
Self-EvaluationJudge response qualityAsync LLM call for evaluation
Iterative ImprovementRefine based on feedbackMultiple async iterations
Final OutputDeliver best responseReturns when criteria met

Feature Parity

Prior to PR #1704, async agents had limited reflection support:

FeatureSync AgentsAsync Agents (Before #1704)Async Agents (After #1704)
Self-reflection✅ Full support❌ Limited/bypassed✅ Full parity
Custom criteria✅ Supported❌ Not available✅ Supported
Min/max reflect✅ Configurable❌ Not configurable✅ Configurable
Unified dispatch✅ Works❌ Bypassed reflection✅ Full support

Configuration Options

All reflection configuration options work identically in async agents:

OptionTypeDefaultDescription
self_reflectboolFalseEnable/disable self-reflection
min_reflectint1Minimum reflection iterations
max_reflectint3Maximum reflection iterations
criteriastrAuto-generatedCustom evaluation criteria

Best Practices

Choose Appropriate Limits

Set reasonable min_reflect and max_reflect values to balance quality with performance. Async reflection still involves multiple LLM calls.

Custom Criteria

Provide specific evaluation criteria for better reflection quality. Generic criteria like “Is this good?” are less effective than specific requirements.

Monitor Performance

Reflection increases response time and token usage. Monitor these metrics in production async applications.

Error Handling

Wrap async reflection calls in try-catch blocks to handle potential timeout or API errors gracefully.


Related