Quick Start
- Code
- No Code
Requirements
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key here. Use Other models using this guide.
- Basic understanding of Python functions
Understanding Callbacks
What are Callbacks?
Callbacks are functions that get called automatically when specific events occur in your AI agents:
- Interactions between user and agent
- Error messages
- Tool calls
- Self-reflection moments
- Task completion
- Generation progress
Features
Interaction Callback
Triggered when the agent interacts with users
Error Callback
Called when errors occur
Tool Call Callback
Activated when tools are used
LLM Start Callback
Triggered when AI model call begins (thinking/responding)
Self Reflection Callback
Triggered during agent self-reflection
Instruction Callback
Called when instructions are processed
Generating Callback
Activated during content generation
Basic Implementation
1. Simple Logging Callback
- Code
- No Code
2. Multiple Callback Types
- Code
- No Code
Complete Example
Here’s a full implementation showing all callback types and proper logging:- Code
- No Code
Advanced Examples
- All callback types
- Comprehensive logging
- Task callbacks
- Tool integration
- Multiple agents
- Code
- No Code
Async Callbacks
Async callbacks allow you to handle events asynchronously, which is particularly useful for long-running operations or when dealing with multiple agents simultaneously.Basic Async Callback Implementation
- Code
- No Code
asyncio.run().Complete Async Example
- Code
- No Code
Async Display Functions
PraisonAI Agents provides several async versions of display functions, prefixed with ‘a’. Here’s the complete list:adisplay_instruction
adisplay_tool_call
adisplay_error
adisplay_generating
Example Usage
Verbose Mode and Process Orchestration
Verbose UI is now driven by composingon_task_start and on_task_complete callbacks, ensuring workflow processes work correctly with verbose=True:
How It Works
Integration with Process Orchestration
Previously, verbose mode bypassed workflow and hierarchical processes. Now the execution goes throughrun_all_tasks() so process="workflow" and process="hierarchical" work correctly with verbose display:
Callback Composition
The composition happens at thePraisonAIAgents / AgentTeam level when user hooks are supplied via MultiAgentHooksConfig with verbose=True:
- User hooks execute first with correct signatures:
(task, task_id)and(task, task_output) - Verbose callbacks execute second for UI display
- Errors in either user or verbose callback are logged at debug level and do not interrupt the task
- This callback signature mismatch was fixed in PR #1740 for proper verbose mode composition
Process Types Support
Verbose mode now supports all process orchestration types:| Process Type | Verbose Support | Behavior |
|---|---|---|
sequential | ✅ | Shows tasks executing in order |
workflow | ✅ | Shows dependency-based execution |
hierarchical | ✅ | Shows manager-agent interactions |
Troubleshooting: TypeError with Hook Signatures
Error:
TypeError: ... takes N positional arguments but M were givenIf you see this when calling PraisonAIAgents.start() with verbose=True and a custom on_task_start / on_task_complete hook, your hook signature does not match the multi-agent contract. Use:on_task_start(task, task_id)on_task_complete(task, task_output)
Task callbacks (set on the Task(...) constructor) keep their original 1-argument signature: on_task_complete(task_output).Best Practices
Error Handling
- Implement proper error handling
- Use try-catch blocks
- Log errors appropriately
- Handle edge cases
Performance
- Keep callbacks lightweight
- Avoid blocking operations
- Use async when appropriate
- Monitor resource usage
Next Steps
AutoAgents
Learn about automatically created and managed AI agents
Mini Agents
Explore lightweight, focused AI agents
Remember to handle callbacks efficiently and implement proper error handling to ensure smooth agent operations.

