Events
BindAI includes an event system that allows applications to observe and react to important actions during execution. Events make it easy to build:- Logging
- Monitoring
- Analytics
- Auditing
- Metrics
- Notifications
- Debugging tools
Why Events?
Every AI request involves multiple steps. For example:Event Bus
Every agent contains its own event bus.Built-in Events
BindAI publishes several built-in events.
Additional events may be introduced as new framework features are added.
Subscribing to Events
Register a listener using the event bus.Example
Model Events
Two of the most useful events occur around model generation.ModelRequestEvent
Published immediately before the provider receives the request. Useful for:- request logging
- prompt inspection
- debugging
- analytics
ModelResponseEvent
Published after the language model returns a response. Useful for:- latency measurement
- response logging
- output validation
- monitoring
Tool Events
Whenever an agent executes a tool, BindAI publishes:- measure tool usage
- collect statistics
- audit external API calls
- monitor execution frequency
Agent Lifecycle Events
Every execution publishes lifecycle events.- timing execution
- measuring throughput
- tracking active requests
- monitoring production systems
Event Flow
A typical execution looks like this.ToolExecutedEvent is only published if the language model invokes a tool.
Custom Events
Applications can publish their own events. Example:Common Use Cases
The event system is useful for many production scenarios. Examples include:- Logging every prompt
- Measuring response times
- Counting model usage
- Monitoring tool execution
- Recording audit trails
- Sending notifications
- Exporting metrics to monitoring platforms
Events vs Hooks
BindAI provides both events and hooks.
Events are primarily intended for observing execution.
Hooks are intended for extending agent behavior.
Best Practices
- Keep event handlers lightweight.
- Avoid blocking execution inside subscribers.
- Use events for logging and monitoring.
- Use hooks or middleware when execution behavior needs to change.
- Keep event listeners independent of business logic.
