Skip to content

Events

Events are the SDK's lightweight observation hook for tool execution.

Use them when your application needs to show "the agent is using web_search", record tool latency, stream status to a UI, or send execution telemetry somewhere else. They are not required for a basic agent. If you only want to run an agent and get a final answer, start with createAgent() from duclaw-cli/sdk.

Events live in the low-level core entrypoint because they are part of runtime composition:

ts
import {
  createCoreAgent,
  createRuntimeEventBus,
  createToolExecutor,
  createToolRegistry,
  type RuntimeEvent,
  type RuntimeEventBus,
} from "duclaw-cli/sdk/core";

Event Types

The built-in tool executor emits these events:

EventWhen it fires
toolUse.startedA tool call is about to run
toolUse.completedA tool call finished successfully
toolUse.failedA tool call threw an error
toolUse.blockedA hook or policy blocked the tool call

Each event includes:

FieldMeaning
eventIdStable id for this runtime event
typeOne of the event types above
occurredAtISO timestamp
toolNameTool name, such as web_search
toolCallIdModel tool-use id when available
inputSummaryJSON summary of the tool input
durationMsTool duration when the call has ended
resultSummaryTruncated result text for completed calls
errorSummaryError or block reason for failed/blocked calls
requestContextRequest metadata such as requestId

Subscribe to Events

ts
const eventBus = createRuntimeEventBus();

eventBus.subscribe("toolUse.completed", (event) => {
  console.log(`${event.toolName} finished in ${event.durationMs}ms`);
});

subscribe() returns an unsubscribe function:

ts
const unsubscribe = eventBus.subscribe("toolUse.failed", (event) => {
  console.error(event.toolName, event.errorSummary);
});

unsubscribe();

Wire Events into Core

ts
const registry = createToolRegistry();
const toolExecutor = createToolExecutor(registry, { eventBus });

const agent = createCoreAgent({
  systemPrompt,
  llm,
  storage,
  tools: Array.from(registry.values()),
  toolExecutor,
});

The friendly createAgent() facade from duclaw-cli/sdk creates the tool executor for you. Use createCoreAgent() from duclaw-cli/sdk/core when your host application needs to own that wiring.

When to Use Events

Use RuntimeEventBus when the host application needs to:

  • render live tool activity in a web or desktop UI;
  • persist execution traces for debugging;
  • measure tool latency and failure rates;
  • forward status updates over WebSocket or SSE;
  • connect SDK runs to product telemetry.

Skip it for simple scripts, tests, or command-line examples where the final agent.run() result is enough.

Released as part of the duclaw-cli package.