Skip to main content

Overview

Agents emit observability events for state changes, RPC calls, connections, schedules, workflows, MCP operations, and emails. Events are published to diagnostic channels and can be consumed via subscribers or Tail Workers.

Event Channels

Events are published to named diagnostic channels:
Channel
State updates (state:update)
Channel
RPC method calls (rpc, rpc:error)
Channel
WebSocket messages, tool calls (message:*, tool:*)
Channel
Scheduled tasks and queues (schedule:*, queue:*)
Channel
Connection lifecycle (connect, disconnect, destroy)
Channel
Workflow events (workflow:*)
Channel
MCP operations (mcp:*)
Channel
Email routing (email:*)

subscribe()

Subscribe to a typed observability channel.
keyof ChannelEventMap
required
Channel name (“rpc”, “state”, “lifecycle”, etc.)
(event: ChannelEventMap[K]) => void
required
Callback to handle events
Returns: () => void - Function to unsubscribe

Event Types

State Events

state:update

Emitted when Agent state changes.
Payload: (none)

RPC Events

rpc

Emitted when an RPC method is called.
Payload:
  • method: string - Method name
  • streaming?: boolean - Whether the method is streaming

rpc:error

Emitted when an RPC call fails.
Payload:
  • method: string - Method name
  • error: string - Error message

Lifecycle Events

connect

Emitted when a WebSocket connection is established.
Payload:
  • connectionId: string - Connection ID

disconnect

Emitted when a WebSocket connection closes.
Payload:
  • connectionId: string - Connection ID
  • code: number - Close code
  • reason: string - Close reason

Schedule Events

schedule:execute

Emitted when a scheduled task executes.
Payload:
  • callback: string - Callback name
  • scheduleId: string - Schedule ID
  • type: string - Schedule type (“cron”, “delayed”, etc.)

queue:execute

Emitted when a queued task executes.
Payload:
  • callback: string - Callback name

Workflow Events

workflow:start

Emitted when a workflow is started.
Payload:
  • workflowName: string - Workflow binding name
  • instanceId: string - Workflow instance ID

workflow:progress

Emitted when a workflow reports progress.
Payload:
  • workflowId: string - Workflow instance ID
  • progress: unknown - Progress data

workflow:complete

Emitted when a workflow completes.
Payload:
  • workflowId: string - Workflow instance ID
  • result: unknown - Workflow result

workflow:error

Emitted when a workflow errors.
Payload:
  • workflowId: string - Workflow instance ID
  • error: string - Error message

MCP Events

mcp:client:connect

Emitted when connecting to an MCP server.
Payload:
  • url: string - Server URL
  • transport: string - Transport type
  • state: string - Connection state
  • error?: string - Error message (if failed)

mcp:client:discover

Emitted when discovering MCP server capabilities.

Email Events

email:receive

Emitted when an email is received.
Payload:
  • from: string - Sender address
  • to: string - Recipient address
  • subject?: string - Email subject

email:reply

Emitted when a reply is sent.
Payload:
  • from: string - Sender address
  • to: string - Recipient address
  • subject?: string - Reply subject

Custom Observability

Override observability on the Agent to use a custom implementation:

Tail Workers

In production, events are automatically forwarded to Tail Workers via event.diagnosticsChannelEvents:

Event Structure

All events have the same base structure:

Best Practices

Subscribe Early

Filter Events

Aggregate Metrics

Use Type Narrowing