Skip to main content
Learn how to implement callbacks to monitor and log AI agent interactions, errors, and task completions.
The user sends a message; your callback logs the interaction while the agent replies.

Quick Start

1

Install Package

First, install the PraisonAI Agents package:
2

Set API Key

Set your OpenAI API key as an environment variable in your terminal:
3

Create a file

Create a new file app.py with the basic setup:
4

Start Agents

Type this in your terminal to run your agents:
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

How It Works

PhaseWhat happens
1. Registerregister_display_callback("interaction", fn) attaches your function to an event type
2. TriggerAgent fires the registered callback at the appropriate lifecycle point
3. ObserveCallback receives message, response, and any extra kwargs for logging or side-effects
4. ContinueAgent execution continues; callbacks are non-blocking observers

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

2. Multiple Callback Types

Complete Example

A full implementation with all callback types and file logging:

Advanced Examples

  • All callback types
  • Comprehensive logging
  • Task callbacks
  • Tool integration
  • Multiple agents

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

Async callbacks are safe to use from inside a running event loop (e.g. FastAPI handlers, Jupyter); the SDK detects the running loop and schedules the coroutine without calling asyncio.run().

Complete Async Example

Async Display Functions

PraisonAI Agents provides several async versions of display functions, prefixed with ‘a’. Here’s the complete list:

adisplay_instruction

Async version for showing instructions.

adisplay_tool_call

Async version for displaying tool calls.

adisplay_error

Async version for error messages.

adisplay_generating

Async version for showing generation progress.

Example Usage

Verbose Mode and Process Orchestration

Verbose UI is now driven by composing on_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 through run_all_tasks() so process="workflow" and process="hierarchical" work correctly with verbose display:

Callback Composition

The composition happens at the PraisonAIAgents / 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 TypeVerbose SupportBehavior
sequentialShows tasks executing in order
workflowShows dependency-based execution
hierarchicalShows 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)
Per-Task callbacks (set on the Task(...) constructor) keep their original 1-argument signature: on_task_complete(task_output).

Best Practices

Avoid blocking I/O or heavy computation in sync handlers — they run on the agent hot path. Offload work to async callbacks or background tasks.
Register with is_async=True inside FastAPI, Jupyter, or other running loops. The SDK schedules coroutines without nesting asyncio.run().
on_task_start(task, task_id) and on_task_complete(task, task_output) at the PraisonAIAgents level; single-argument on_task_complete(task_output) on individual Task objects only.
Wrap callback logic in try/except and log failures. Callback exceptions are swallowed at debug level and must not interrupt agent execution.

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.

Run shell or Python hooks on agent lifecycle events.

Stream structured events to your observability pipeline.