Skip to main content

Why Events?

Your agent does a lot behind the scenes. Events let you hook into every step:
  • Log every model call for debugging
  • Track action success/failure for analytics
  • Trigger workflows when messages arrive
  • Monitor agent health in real-time
Events are fire-and-forget. Handlers run asynchronously and don’t block the main flow.

Event System

The event system enables reactive programming patterns, allowing plugins and services to respond to runtime activities. Events flow through the system providing hooks for custom logic and integrations.

Event Architecture

Event Types

Core Event Types

Event Payloads

Payload Interfaces

Each event type has a specific payload structure:

Event Handlers

Handler Registration

Event handlers are registered during plugin initialization:

Handler Implementation

Event Emission

Emitting Events

Event Listeners

Adding Event Listeners

Removing Event Listeners

Event Patterns

Request-Response Pattern

Event Chaining

Event Aggregation

Custom Events

Defining Custom Events

Using Custom Events

Event Middleware

Creating Event Middleware

Error Handling

Event Error Handling

Performance Considerations

Event Batching

Event Throttling

Best Practices

Event Design

  • Specific Events: Create specific events rather than generic ones
  • Consistent Payloads: Use consistent payload structures
  • Event Naming: Use clear, hierarchical naming (domain:action:status)
  • Documentation: Document event types and payloads
  • Versioning: Version events when making breaking changes

Performance

  • Async Handlers: Always use async handlers
  • Non-Blocking: Don’t block the event loop
  • Batching: Batch high-frequency events
  • Throttling: Throttle rapid events
  • Cleanup: Remove unused listeners

Error Handling

  • Graceful Failures: Don’t crash on handler errors
  • Error Events: Emit error events for monitoring
  • Timeouts: Set timeouts for long operations
  • Retries: Implement retry logic for transient failures
  • Logging: Log errors with context

See Also

Providers

Learn how providers use events

Models

Explore AI model management

Services

Build services that emit and handle events

Messaging

Understand real-time event streaming