File: //opt/PraisonAI/docs/features/selfreflection.mdx
---
title: "Self Reflection AI Agents"
sidebarTitle: "Self Reflection Agents"
description: "Self-reflection enables agents to evaluate and improve their own responses before delivering them."
icon: "brain"
---
```mermaid
flowchart LR
In[In] --> Agent[AI Agent]
Agent --> Reflect[Self Reflection]
Reflect --> Agent
Agent --> Out[Out]
style In fill:#8B0000,color:#fff
style Agent fill:#2E8B57,color:#fff
style Reflect fill:#2E8B57,color:#fff
style Out fill:#8B0000,color:#fff
```
Self-reflection enables agents to evaluate and improve their own responses before delivering them.
## Quick Start
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent with self-reflection
agent = Agent(
role="Senior Research Analyst",
goal="Analyze and provide insights on given topics",
backstory="You are an expert analyst with strong critical thinking skills",
self_reflect=True # Enable self-reflection
)
# Create a task
task = Task(
description="Analyze recent developments in AI",
expected_output="A detailed analysis report",
agent=agent
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[agent],
tasks=[task],
process="sequential",
verbose=2
)
# Start execution
agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: create movie script about cat in mars
roles:
scriptwriter:
backstory: Expert in dialogue and script structure, translating concepts into
scripts.
goal: Write a movie script about a cat in Mars
role: Scriptwriter
self_reflect: true
min_reflect: 1
max_reflect: 2
tasks:
scriptwriting_task:
description: Turn the story concept into a production-ready movie script,
including dialogue and scene details.
expected_output: Final movie script with dialogue and scene details.
tools:
- search_tool
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
</Note>
<div className="relative w-full aspect-video">
<iframe
className="absolute top-0 left-0 w-full h-full"
src="https://www.youtube.com/embed/vLXobEN2Vc8"
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
></iframe>
</div>
## Understanding Self-Reflection
<Card title="What is Self-Reflection?" icon="question">
Self-reflection enables agents to:
- Evaluate their own responses before delivery
- Check for completeness and accuracy
- Improve output quality through iteration
- Ensure task requirements are met
- Make conscious decisions about their responses
</Card>
## Features
<CardGroup cols={2}>
<Card title="Quality Assurance" icon="check-double">
Evaluates and improves response quality through self-review.
</Card>
<Card title="Iterative Improvement" icon="rotate">
Refines responses through multiple reflection cycles.
</Card>
<Card title="Task Validation" icon="list-check">
Ensures all aspects of the task are properly addressed.
</Card>
<Card title="Conscious Decision Making" icon="brain">
Enables thoughtful evaluation of responses.
</Card>
</CardGroup>
## Multi-Agent Self-Reflection
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create first agent with self-reflection
researcher = Agent(
role="Senior Research Analyst",
goal="Research and analyze AI developments",
backstory="Expert analyst specializing in AI trends and impacts",
self_reflect=True,
verbose=True
)
# Create second agent with self-reflection
writer = Agent(
role="Technical Writer",
goal="Transform research into clear documentation",
backstory="Experienced in creating technical content and documentation",
self_reflect=True,
verbose=True
)
# Create first task
research_task = Task(
description="Research and analyze recent AI developments",
expected_output="Comprehensive analysis of AI trends",
agent=researcher
)
# Create second task
documentation_task = Task(
description="Create technical documentation from research findings",
expected_output="Well-structured technical documentation",
agent=writer
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[research_task, documentation_task],
process="sequential"
)
# Start execution
agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai duckduckgo_search
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: create technical documentation about AI trends
roles:
researcher:
backstory: Expert analyst specializing in AI trends and their implications.
goal: Research and analyze current AI developments
role: Senior Research Analyst
self_reflect: true
min_reflect: 1
max_reflect: 2
tasks:
research_task:
description: Research and analyze recent developments in AI technology.
expected_output: Comprehensive analysis of current AI trends.
tools:
- duckduckgo
writer:
backstory: Experienced technical writer skilled in creating clear documentation.
goal: Transform research into accessible documentation
role: Technical Writer
tasks:
documentation_task:
description: Create technical documentation from research findings.
expected_output: Well-structured technical documentation.
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
### Configuration Options
```python
# Create an agent with advanced self-reflection configuration
agent = Agent(
role="Research Analyst",
goal="Provide comprehensive analysis",
backstory="Expert analyst with critical thinking skills",
self_reflect=True, # Enable self-reflection
verbose=True, # Enable detailed logging
llm="gpt-4o", # Language model to use
allow_delegation=True # Allow task delegation
)
```
## Troubleshooting
<CardGroup cols={2}>
<Card title="Response Issues" icon="rotate">
If responses are not meeting expectations:
- Enable verbose mode for debugging
- Review agent configuration
- Check task description clarity
</Card>
<Card title="Quality Issues" icon="triangle-exclamation">
If output quality is insufficient:
- Enable verbose mode
- Review agent role and goal
- Clarify expected output
</Card>
</CardGroup>
## Next Steps
<CardGroup cols={2}>
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
<Note>
For optimal results, configure reflection parameters based on your specific use case requirements.
</Note>