Agents
Understanding Agents in PraisonAI
Understanding Agents
Agents are the core building blocks of PraisonAI. Each agent is an autonomous AI entity with specific roles, goals, and capabilities.
Single Agent vs Multi-Agent
| Use Case | Class | Code |
|---|---|---|
| One agent, one task | Agent | Agent(instructions="...").start() |
| Multiple agents working together | AgentTeam | AgentTeam(agents=[...]).start() |
Agent Architecture
Key Components
Defines the agent’s purpose and objectives through role definition and specific goals
CapabilitiesTools and functions available to the agent for task execution
MemoryContext retention and learning capabilities across interactions
Language ModelThe underlying AI model powering the agent’s intelligence
Component Details
Role and Goal
| Component | Description | Example |
|---|---|---|
| Role | Agent’s function and expertise | Research Analyst, Code Developer |
| Goal | Specific objectives to achieve | Analyze market trends, Generate reports |
| Backstory | Contextual background | Expert with 10 years of experience |
Capabilities
agent = Agent(
name="Researcher",
role="Senior Research Analyst", # Part of System Prompt
goal="Uncover cutting-edge developments in AI", # Part of System Prompt
backstory="You are an expert at a technology research group", # Part of System Prompt
llm="gpt-4o"
)- Install PraisonAI
Install the core package:
Terminal pip install praisonaiagents - Configure Environment
Terminal export OPENAI_API_KEY=your_openai_keyGenerate your OpenAI API key from OpenAI Use other LLM providers like Ollama, Anthropic, Groq, Google, etc. Please refer to the Models for more information.
- Create Agent
Create
app.py:Single Agent from praisonaiagents import Agent agent = Agent(instructions="Your are a helpful AI assistant") # System Prompt: defines WHO the agent is agent.start("Write a movie script about a robot in Mars") # User Prompt: defines WHAT to doMulti Agents from praisonaiagents import Agent, AgentTeam research_agent = Agent(instructions="Research about AI") # System Prompt summarise_agent = Agent(instructions="Summarise research agent's findings") # System Prompt agents = AgentTeam(agents=[research_agent, summarise_agent]) agents.start() # User Prompt (uses task descriptions) - Start Agents
Execute your script:
Terminal python app.pyYou should see: – Agent initialization – Agents progress – Final results – Generated report
Agent Types
Overview
Perfect for straightforward tasks and direct interactions
Features
- Single-purpose focus
- Direct user interaction
- Limited tool set
- Ideal for simple tasks
Overview
Experts in specific domains with advanced capabilities
Features
- Domain expertise
- Advanced capabilities
- Custom tools
- Deep knowledge base
Overview
Designed for team-based operations and complex workflows
Features
- Team interaction
- Task delegation
- Shared context
- Coordinated actions
Best Practices
- Role Definition
Define clear, specific roles for each agent
- Goal Setting
Set specific, measurable goals
- Tool Selection
Choose relevant tools for the task
- Memory Setup
Configure appropriate memory settings
- Communication
Establish clear communication protocols
- Delegation
Define explicit delegation rules
- Error Handling
Implement robust error handling
- Resource Management
Set up efficient resource allocation
Async Capabilities
Advanced Features
- Short-term conversation memory
- Long-term knowledge retention
- Context preservation
- Custom tool development
- External API integration
- Resource access control
Async Support
Agents now support asynchronous operations through the following methods:
achat: Async version of the chat methodastart: Async version of start methodaexecute_task: Async version of execute_task methodarun_task: Async version of run_task methodarun_all_tasks: Async version of run_all_tasks method
Example Usage:
import asyncio
from praisonaiagents import Agent, Task, AgentTeam
async def main():
# Create an async agent
async_agent = Agent(
name="AsyncAgent",
role="Async Task Specialist",
goal="Perform async operations",
backstory="Expert in async operations",
tools=[async_search_tool], # Your async tool
)
# Create an async task
async_task = Task(
description="Perform async operation",
expected_output="Async result",
agent=async_agent,
async_execution=True # Enable async execution
)
# Create and start agents with async support
agents = AgentTeam(
agents=[async_agent],
tasks=[async_task],
)
# Start async execution
result = await agents.astart()
print(result)
# Run the async main function
if __name__ == "__main__":
asyncio.run(main())Key Features:
- Full async/await support
- Parallel task execution
- Async tool integration
- Async callback support
- Mixed sync/async operations