Understanding Tasks
Tasks are units of work that agents execute. Each task has a clear goal, input requirements, and expected outputs.
Core Components
Task Definition Clear description of work to be done and expected outputs
Agent Assignment Matching tasks with capable agents
Tool Access Resources available for task completion
Output Handling Managing and formatting task results
Task Configuration
task = Task(
description = "Research AI trends" ,
expected_output = "Summary report" ,
agent = research_agent
)
Understanding Tasks
Task Types
Basic Task
Simple, single-operation tasks with clear inputs and outputs
Decision Task
Tasks involving choices and conditional paths decision_task = Task(
type = "decision" ,
conditions = {
"success" : [ "next_task" ],
"failure" : [ "error_task" ]
}
)
Loop Task
Repetitive operations over collections loop_task = Task(
type = "loop" ,
items = data_list,
operation = "process_item"
)
Task Relationships
Properly managing task dependencies is crucial for complex workflows. Always ensure proper context sharing and error handling.
Context Sharing
task_a = Task( name = "research" )
task_b = Task(
name = "analyze" ,
context = [task_a] # Uses task_a's output
)
Task Dependencies
Relationship Description Example Sequential Tasks run one after another Research → Analysis → Report Parallel Independent tasks run simultaneously Data Collection + Processing Conditional Tasks depend on previous results Success → Next Task, Failure → Retry
Advanced Features
Output Handling
Multiple output formats
Structured data validation
File system integration
Task Control
Execution flow control
Error recovery
Progress tracking
Getting Started
Install PraisonAI
Install the core package: pip install praisonaiagents
Configure Environment
export OPENAI_API_KEY = your_openai_key
Generate 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
Multiple Agents
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent
researcher = Agent(
name = "Researcher" ,
role = "Senior Research Analyst" ,
goal = "Uncover cutting-edge developments in AI" ,
backstory = "You are an expert at a technology research group" ,
verbose = True ,
llm = "gpt-4o"
)
# Define a task
task = Task(
name = "research_task" ,
description = "Analyze 2024's AI advancements" ,
expected_output = "A detailed report" ,
agent = researcher
)
# Run the agents
agents = PraisonAIAgents(
agents = [researcher],
tasks = [task],
verbose = False
)
result = agents.start()
Best Practices
Design tasks to be atomic and focused. Each task should have a single, clear responsibility.
Task Design Principles
Write precise, unambiguous task descriptions that clearly state the task’s purpose and requirements. task = Task(
description = (
"Analyze Q4 2024 sales data "
"and generate growth metrics"
),
expected_output = "Sales growth report"
)
Define measurable success criteria to ensure task completion can be properly evaluated. task = Task(
success_criteria = {
"format" : "markdown" ,
"metrics" : [
"growth_rate" ,
"revenue_change"
],
"min_confidence" : 0.95
}
)
Specify any dependencies or prerequisites for the task. task = Task(
description = "Generate final report" ,
dependencies = [
data_collection_task,
analysis_task
],
wait_for_dependencies = True
)
Resource Management
Implement comprehensive error management strategies. task = Task(
error_strategy = "retry" ,
max_retries = 3 ,
fallback_handler = handler,
error_callbacks = [
log_error,
notify_admin
]
)
Set appropriate resource constraints for optimal performance. task = Task(
memory_limit = "2GB" ,
timeout = 300 ,
max_concurrent = 5 ,
cpu_priority = "high" ,
auto_scale = True
)
Async Task Execution
Async Task Features Basic Configuration
Async Callbacks
Mixed Execution
async_task = Task(
description = "Perform async operation" ,
expected_output = "Async result" ,
agent = async_agent,
async_execution = True , # Enable async execution
callback = async_callback # Optional async callback
)
Enable async execution with async_execution=True
Perfect for I/O-heavy operations
Supports parallel task execution
async def async_callback ( output : TaskOutput):
await asyncio.sleep( 1 ) # Simulate async operation
print ( f "Processed output: { output.raw } " )
task = Task(
description = "Task with async callback" ,
expected_output = "Result" ,
agent = agent,
async_execution = True ,
callback = async_callback
)
Support for async callbacks
Process results asynchronously
Handle task completion events
# Create mixed task types
sync_task = Task(
description = "Synchronous operation" ,
async_execution = False
)
async_task = Task(
description = "Asynchronous operation" ,
async_execution = True
)
# Run with mixed execution
agents = PraisonAIAgents(
agents = [sync_agent, async_agent],
tasks = [sync_task, async_task]
)
result = await agents.astart()
Mix sync and async tasks
Flexible execution patterns
Optimal resource utilization
Pro Tip : Use async execution for tasks that involve I/O operations (like API calls or file operations) to improve overall performance. Keep CPU-intensive tasks synchronous.
Next Steps