Event Listeners
Tap into CrewAI events to build custom integrations and monitoring
Event Listeners
CrewAI provides a powerful event system that allows you to listen for and react to various events that occur during the execution of your Crew. This feature enables you to build custom integrations, monitoring solutions, logging systems, or any other functionality that needs to be triggered based on CrewAI’s internal events.
How It Works
CrewAI uses an event bus architecture to emit events throughout the execution lifecycle. The event system is built on the following components:
- CrewAIEventsBus: A singleton event bus that manages event registration and emission
- CrewEvent: Base class for all events in the system
- BaseEventListener: Abstract base class for creating custom event listeners
When specific actions occur in CrewAI (like a Crew starting execution, an Agent completing a task, or a tool being used), the system emits corresponding events. You can register handlers for these events to execute custom code when they occur.
Creating a Custom Event Listener
To create a custom event listener, you need to:
- Create a class that inherits from
BaseEventListener
- Implement the
setup_listeners
method - Register handlers for the events you’re interested in
- Create an instance of your listener in the appropriate file
Here’s a simple example of a custom event listener class:
Properly Registering Your Listener
Simply defining your listener class isn’t enough. You need to create an instance of it and ensure it’s imported in your application. This ensures that:
- The event handlers are registered with the event bus
- The listener instance remains in memory (not garbage collected)
- The listener is active when events are emitted
Option 1: Import and Instantiate in Your Crew or Flow Implementation
The most important thing is to create an instance of your listener in the file where your Crew or Flow is defined and executed:
For Crew-based Applications
Create and import your listener at the top of your Crew implementation file:
For Flow-based Applications
Create and import your listener at the top of your Flow implementation file:
This ensures that your listener is loaded and active when your Crew or Flow is executed.
Option 2: Create a Package for Your Listeners
For a more structured approach, especially if you have multiple listeners:
- Create a package for your listeners:
- In
my_custom_listener.py
, define your listener class and create an instance:
- In
__init__.py
, import the listener instances to ensure they’re loaded:
- Import your listeners package in your Crew or Flow file:
This is exactly how CrewAI’s built-in agentops_listener
is registered. In the CrewAI codebase, you’ll find:
This ensures the agentops_listener
is loaded when the crewai.utilities.events
package is imported.
Available Event Types
CrewAI provides a wide range of events that you can listen for:
Crew Events
- CrewKickoffStartedEvent: Emitted when a Crew starts execution
- CrewKickoffCompletedEvent: Emitted when a Crew completes execution
- CrewKickoffFailedEvent: Emitted when a Crew fails to complete execution
- CrewTestStartedEvent: Emitted when a Crew starts testing
- CrewTestCompletedEvent: Emitted when a Crew completes testing
- CrewTestFailedEvent: Emitted when a Crew fails to complete testing
- CrewTrainStartedEvent: Emitted when a Crew starts training
- CrewTrainCompletedEvent: Emitted when a Crew completes training
- CrewTrainFailedEvent: Emitted when a Crew fails to complete training
Agent Events
- AgentExecutionStartedEvent: Emitted when an Agent starts executing a task
- AgentExecutionCompletedEvent: Emitted when an Agent completes executing a task
- AgentExecutionErrorEvent: Emitted when an Agent encounters an error during execution
Task Events
- TaskStartedEvent: Emitted when a Task starts execution
- TaskCompletedEvent: Emitted when a Task completes execution
- TaskFailedEvent: Emitted when a Task fails to complete execution
- TaskEvaluationEvent: Emitted when a Task is evaluated
Tool Usage Events
- ToolUsageStartedEvent: Emitted when a tool execution is started
- ToolUsageFinishedEvent: Emitted when a tool execution is completed
- ToolUsageErrorEvent: Emitted when a tool execution encounters an error
- ToolValidateInputErrorEvent: Emitted when a tool input validation encounters an error
- ToolExecutionErrorEvent: Emitted when a tool execution encounters an error
- ToolSelectionErrorEvent: Emitted when there’s an error selecting a tool
Flow Events
- FlowCreatedEvent: Emitted when a Flow is created
- FlowStartedEvent: Emitted when a Flow starts execution
- FlowFinishedEvent: Emitted when a Flow completes execution
- FlowPlotEvent: Emitted when a Flow is plotted
- MethodExecutionStartedEvent: Emitted when a Flow method starts execution
- MethodExecutionFinishedEvent: Emitted when a Flow method completes execution
- MethodExecutionFailedEvent: Emitted when a Flow method fails to complete execution
LLM Events
- LLMCallStartedEvent: Emitted when an LLM call starts
- LLMCallCompletedEvent: Emitted when an LLM call completes
- LLMCallFailedEvent: Emitted when an LLM call fails
- LLMStreamChunkEvent: Emitted for each chunk received during streaming LLM responses
Event Handler Structure
Each event handler receives two parameters:
- source: The object that emitted the event
- event: The event instance, containing event-specific data
The structure of the event object depends on the event type, but all events inherit from CrewEvent
and include:
- timestamp: The time when the event was emitted
- type: A string identifier for the event type
Additional fields vary by event type. For example, CrewKickoffCompletedEvent
includes crew_name
and output
fields.
Real-World Example: Integration with AgentOps
CrewAI includes an example of a third-party integration with AgentOps, a monitoring and observability platform for AI agents. Here’s how it’s implemented:
This listener initializes an AgentOps session when a Crew starts, registers agents with AgentOps, tracks tool usage, and ends the session when the Crew completes.
The AgentOps listener is registered in CrewAI’s event system through the import in src/crewai/utilities/events/third_party/__init__.py
:
This ensures the agentops_listener
is loaded when the crewai.utilities.events
package is imported.
Advanced Usage: Scoped Handlers
For temporary event handling (useful for testing or specific operations), you can use the scoped_handlers
context manager:
Use Cases
Event listeners can be used for a variety of purposes:
- Logging and Monitoring: Track the execution of your Crew and log important events
- Analytics: Collect data about your Crew’s performance and behavior
- Debugging: Set up temporary listeners to debug specific issues
- Integration: Connect CrewAI with external systems like monitoring platforms, databases, or notification services
- Custom Behavior: Trigger custom actions based on specific events
Best Practices
- Keep Handlers Light: Event handlers should be lightweight and avoid blocking operations
- Error Handling: Include proper error handling in your event handlers to prevent exceptions from affecting the main execution
- Cleanup: If your listener allocates resources, ensure they’re properly cleaned up
- Selective Listening: Only listen for events you actually need to handle
- Testing: Test your event listeners in isolation to ensure they behave as expected
By leveraging CrewAI’s event system, you can extend its functionality and integrate it seamlessly with your existing infrastructure.
Was this page helpful?