Events

Events provide visibility into agent execution and enable reactive behavior. Each event type has specific input structure, output requirements, and use cases.

History Structure

All events provide access to state.history, an array of HistoryStep objects tracking execution flow.

HistoryStep Structure

  • step: Sequential step number

  • type: Step type (TRIGGER_NODE, TOOL_NODE, LLM_NODE, etc.)

  • nodeId: ID of the executed node

  • nodeDisplayName: Human-readable node name

  • raw: Raw data (trigger input, tool output, etc.)

  • messageIds: Associated message IDs

Additional fields:

  • APP_TRIGGER_NODE: appName identifies source application

  • TOOL_NODE: Contains tool input/output information

Example:

agent.on(events.AI_MESSAGE, async ({ state }) => {
  const triggerStep = state.history.find((step) => 
    step.type === 'TRIGGER_NODE' || step.type === 'APP_TRIGGER_NODE'
  );
  const toolSteps = state.history.filter((step) => step.type === 'TOOL_NODE');
  console.log(`Trigger: ${triggerStep?.nodeDisplayName}, Tools used: ${toolSteps.length}`);
});

INIT

Emitted when agent's graph state is initialized (new session begins).

Input:

Return: void (handlers for side effects only)

Example:

Use cases: Session logging, resource initialization, external service setup, debugging

AI_MESSAGE

Emitted when AI generates a message for the user.

Input:

Return: void (handlers for side effects only)

Example:

Use cases: Real-time chat UI, logging, message formatting, notifications, session management

TRIGGER_EVENT

Emitted when a trigger node is executed. Allows qualifying triggers and modifying state before processing.

Input:

Return: Must return { isQualified: boolean }

Important: State is modified by reference (v2.0). Modify state directly, don't return state updates.

Return patterns:

Examples:

Note: goto jump occurs after current invocation completes.

Use cases: Input validation, data transformation, context setting, access control, routing logic

ERROR

Emitted when an error occurs during execution. Control whether error is thrown or caught.

Input:

Return:

Important: State is modified by reference (v2.0).

Example:

Use cases: Error logging, recovery logic, critical error handling, monitoring, session cleanup, analytics

ON_LOGICAL_CONDITION

Emitted before evaluating a logical condition on an edge.

Input:

Return: void (handlers for debugging/logging only)

Example:

Use cases: Debugging flow logic, performance monitoring, analytics, testing, audit logging

ON_LOGICAL_CONDITION_RESULT

Emitted after a logical condition is evaluated, providing result and execution metrics.

Input:

Return: void (handlers for logging/monitoring only)

Example:

Use cases: Performance monitoring, debugging failed conditions, flow analytics, error tracking, optimization

ANALYTICS_EVENT

Emitted when you call trackAnalyticsEvent() to send custom analytics data.

Function signature:

Handler input:

Return: void

Example:

Query analytics: Use the REST API

TURN_END

Emitted when a trigger invocation completes, just before returning the result. Allows customizing the return value.

Input:

Return:

Example:

Use cases: Response formatting, metadata addition, data extraction, API compatibility, result enrichment, response filtering

Last updated