Events
BindAI provides event and callback mechanisms for observing and extending agent execution. There are two related but distinct event mechanisms:- Agent callbacks —
Agent.on()andAgent.emit()provide lightweight named callbacks directly on an agent. - Core events —
EventBusprovides a structured event-publishing mechanism used by BindAI runtime components and integrations.
- Logging
- Monitoring
- Analytics
- Notifications
- Debugging
- Auditing
- Custom integrations
- Automation triggers
- Application-specific execution behavior
Agent Events
TheAgent class provides a lightweight named-event API.
Register a callback with:
emit().
Agent events are primarily useful for application-level callbacks and lightweight integrations.
Registering Callbacks
UseAgent.on() to register a callback for a named event.
Emitting Events
UseAgent.emit() to emit a named event.
Custom Events
Agent event names are strings, allowing applications to define their own event names.Event Arguments
Agent.emit() accepts positional arguments after the event name.
For example:
Core EventBus
BindAI also provides a coreEventBus abstraction.
Event objects.
Create an event bus:
Agent.on() / Agent.emit() callback API.
The event bus is intended for communication between BindAI components and integrations that need a structured event boundary.
Event Objects
Core events are represented byEvent objects.
Subscribing to EventBus Events
UseEventBus.subscribe() to register an event handler.
Event object.
Unlike Agent.on(), the callback does not receive arbitrary positional arguments supplied by the publisher.
Instead, it receives the structured event.
Publishing EventBus Events
Publish an event withEventBus.publish().
Unsubscribing
EventBus handlers can be removed withunsubscribe().
EventBus Handler Isolation
The coreEventBus isolates handler failures from the publishing component.
If a handler raises an exception, the event bus logs the failure rather than allowing the handler exception to interrupt the remaining event handling.
Conceptually:
Agent EventBus
EachAgent has an event bus available through:
Agent Callbacks vs EventBus
The two mechanisms serve different purposes.
A useful rule is:
- Use
Agent.on()/Agent.emit()for lightweight application-level callbacks. - Use
EventBuswhen components need to communicate through structured BindAI events. - Use automation triggers when an event should initiate an automation action.
Agent Lifecycle
Agent execution also has lifecycle behavior. The execution engine publishes framework events such as agent-started and agent-finished events through the core event infrastructure. Agent execution also exposes lifecycle callback methods:Tool Events
Tool execution can also produce core events. The agent publishes aToolExecutedEvent through its event bus after a tool execution.
For example:
- Logging
- Auditing
- Metrics
- Monitoring
- Automation
- Debugging
Events and Automation
The core event system provides the foundation for event-driven automation. BindAI’s Automation package providesEventTrigger, which can listen to an EventBus and invoke a target when a matching event is published.
The relationship is:
Enabling and Disabling Automation Triggers
EventTrigger supports temporary enable/disable behavior.
Detaching Automation Triggers
A trigger can be detached from its event source:Trigger Registry
BindAI Automation also providesTriggerRegistry.
Event-Driven Architecture
The event and automation layers can be combined into a larger architecture:Events and Workflows
Events can also act as boundaries around larger application workflows. For example:Events and External Integrations
Events can connect BindAI execution to external application services. For example:Keeping Event Handlers Lightweight
Event handlers should generally perform small, focused operations. Good event handlers include:- Logging
- Updating counters
- Recording metrics
- Creating audit records
- Triggering lightweight notifications
- Scheduling background work
- Passing work to another application component
Error Handling in Event Handlers
Event handlers are application code and can introduce their own errors. ForEventBus handlers, BindAI isolates handler exceptions so that one failing handler does not prevent the event bus from continuing to other handlers.
Applications should still implement appropriate logging and error handling within handlers when the operation itself is important.
For example:
Event Naming
Use descriptive event names. Framework event names should come from the corresponding BindAI event types. For application-specific agent events, use names that clearly describe what happened:EventBus events, prefer stable event names that identify the event type.
Avoid ambiguous names that make it difficult for subscribers to understand what the event represents.
Hooks and Callbacks
Events are not the only extension mechanism available to agents. Agents also expose hooks and lifecycle callbacks. Thehook() method allows application code to register hook behavior:
Events vs Hooks
Events and hooks serve related but different purposes.
A simple rule is:
- Use agent callbacks for lightweight local callbacks.
- Use core events for structured communication between components.
- Use automation triggers when events should initiate actions.
- Use hooks for reusable execution-related behavior.
- Use workflows for explicit multi-step orchestration.
Testing Events
Event behavior should be tested independently from external services. A simple agent callback test can verify that a callback receives emitted values:- Event registration
- Event emission
- EventBus subscription
- EventBus publication
- Event arguments or payloads
- Multiple subscribers
- Handler isolation
- Trigger attachment
- Trigger detachment
- Trigger enable/disable behavior
Events and Observability
Events provide useful building blocks for observability. Applications can connect events to:- Logging
- Metrics
- Tracing
- Auditing
- Error monitoring
- Persistent event storage
- Distributed tracing
- Metrics aggregation
- Dashboards
- Alerting
- Background processing
Event-Driven Applications
As an application grows, events can provide boundaries between independent components. For example:Current Event Architecture
The current BindAI event architecture can be summarized as:Best Practices
- Use descriptive event names.
- Keep application event contracts consistent.
- Document callback arguments for custom agent events.
- Use structured
Eventobjects withEventBus. - Prefer framework event types when consuming BindAI core events.
- Keep event handlers small and focused.
- Keep heavy business logic outside event handlers.
- Handle important handler failures appropriately.
- Use
EventTriggerwhen an event should initiate automation. - Detach triggers when they are no longer needed.
- Use hooks for reusable execution-related behavior.
- Use lifecycle callbacks for lifecycle-specific behavior.
- Use workflows for complex orchestration.
- Test event mechanisms independently from external services.
- Do not assume undocumented lifecycle event names or callback signatures.
- Do not treat the event system as a complete observability platform.
Summary
BindAI provides several complementary mechanisms for reacting to agent activity. At the agent level:EventBus for structured event communication, automation triggers for event-driven actions, and workflows for complex orchestration.