This reference summarizes every trace/streaming event emitted from src/core/engine.ts. Events are delivered through the onEvent callback and the runStream async generator.
| Event | Trigger | Highlights |
|---|---|---|
run_start |
Beginning of run() |
Includes run/trace IDs, initial context, first message set |
llm_call_start |
Immediately before calling model provider | Full request context: agent, model, messages, tool schemas |
llm_call_end |
After model provider returns | Attaches raw provider response, prompt echo, token usage snapshot |
token_usage |
When the model response exposes usage numbers | Normalized token counts keyed by phase + model name |
assistant_message |
Whenever assistant output is available | Streams partial outputs during SSE; final message emitted if no stream |
tool_requests |
When tool calls are requested or resumed | Parsed arguments per call; fired for fresh and resumed calls |
tool_call_start |
Before each tool execution | Carries parsed args, tool schema metadata, agent + trace context |
tool_call_end |
After each tool execution finishes or errors | Includes serialized result, optional ToolResult payload, timing + error data |
tool_results_to_llm |
When tool outputs are appended to the transcript | Provides tool messages batched for next LLM turn |
handoff |
Tool signals handoff_to another agent and is allowed |
Names current agent and destination agent |
handoff_denied |
Tool attempts handoff to disallowed agent | Adds rejection reason |
guardrail_violation |
Input/output guardrail fails | Specifies stage (input/output) and reason |
decode_error |
Agent output codec fails to parse LLM content | Emits zod issues from the failed parse |
turn_start |
Start of each agent turn (before LLM call) | 1-based turn counter + agent name |
turn_end |
End of turn (on completion, error, or handoff) | Same turn counter + agent name |
final_output |
Just before returning a successful result | Emits decoded or raw final output payload |
run_end |
Terminal event for run() |
Contains RunResult.outcome (completed / interrupted / error) |
- Emitted from: Top of
run()before any work is performed. - When: Immediately after entering the engine with the supplied
initialState. - Payload:
runId: Unique run identifier from state.traceId: Correlation ID for downstream events.context: The full runtime context object.userId: Convenience copy ofcontext.userId(if present).sessionId:context.sessionIdorcontext.conversationIdfallback.messages: Initial conversation history array.
- Notes: If memory loading is enabled, this fires before any history is merged in, so collectors can see the raw starting point.
- Emitted from:
runInternal()prior to invoking the model provider. - When: After guardrails pass and the current agent is resolved.
- Payload:
agentName: Name of the agent making the request.model: Selected model name (honorsconfig.modelOverride).traceId,runId: Identifiers for correlation.messages: Full message list passed to the provider.tools: Array of tool schemas (name, description, parameters) available to the model.modelConfig: Agent model configuration plus any override metadata.turnCount: Prior turn count (0-based before increment).context: Current execution context forwarded to the provider.
- Notes: Mirrors exactly what the provider receives, enabling request replay/debugging.
- Emitted from:
runInternal()after the provider yields a response (streaming or standard). - Payload:
choice: Alias of the full provider response.fullResponse: Same aschoice(maintained for compatibility).prompt: Provider echo of the prompt request when available.traceId,runId,agentName,model.usage: Optional token usage snapshot{ prompt_tokens, completion_tokens, total_tokens }when present on the provider response.
- Notes: Fires even if the model result is malformed; downstream logic uses it before throwing errors.
- Emitted from:
runInternal()immediately afterllm_call_end. - When: Only if the provider response exposes token counts.
- Payload:
prompt,completion,total: Token counts by phase.model: Model name associated with the counts.
- Notes: Provides a stable shape regardless of provider-specific field names.
- Emitted from:
- Streaming loop inside
runInternal()for each delta chunk. - After
llm_call_endwhen streaming is unavailable or fails.
- Streaming loop inside
- Payload:
message: Standard assistantMessageobject containing the aggregated text and any tool call descriptors gathered so far.
- Notes:
- During streaming, multiple incremental events fire with growing
message.contentand evolvingtool_callsentries. - When streaming is not used, a single event fires with the final assistant message.
- Collectors should treat later payloads as supersets of earlier ones.
- During streaming, multiple incremental events fire with growing
- Emitted from:
runInternal()after an assistant message includestool_calls.tryResumePendingToolCalls()when resuming previously requested tools.
- Payload:
toolCalls: Array of{ id, name, args }whereargsis JSON-parsed when possible, otherwise the raw string.
- Notes: Useful for approval workflows and tool auditing before execution begins.
- Emitted from:
executeToolCalls()before invoking each tool. - Payload:
toolName,args: Tool identifier and parsed arguments.traceId,runId,agentName.toolSchema: Optional metadata (name,description,parameters) when the tool definition is found.context: Current run context.executionTimeis not present here (only ontool_call_end).
- Notes: Fires even if the tool cannot be found; the subsequent
tool_call_endwill carry the failure details.
- Emitted from:
executeToolCalls()after tool completion or failure. - Payload:
toolName,result: Serialized string result that is appended to the transcript.traceId,runId.toolResult: Original ToolResult object when the tool used the ToolResult API; otherwiseundefined.status:'success','error', or a ToolResult-specific status value.executionTime: Milliseconds taken by the tool call.metadata: Diagnostic bundle includingagentName,parsedArgs,context, andresultType('string'or'object').error: Present when execution fails or the tool is missing/invalid, includingtype,message, and optionaldetails/stack.
- Notes: Always fires, even on validation errors or missing tools, ensuring collectors can log end states symmetrically with starts.
- Emitted from:
runInternal()after successful tool executions.tryResumePendingToolCalls()when pending tool calls are resumed and executed.
- Payload:
results: Array of toolMessageobjects that are appended to the conversation before the next LLM turn.
- Notes: Signals what the next LLM invocation will see; useful for replaying agent transcripts.
- Emitted from:
runInternal()when a tool result includeshandoff_toand the target agent is permitted. - Payload:
from: Current agent name.to: Target agent name.
- Notes: Fired before the engine switches
currentAgentNameand continues the run with the new agent.
- Emitted from:
runInternal()when a tool returnshandoff_tobut the current agent is not allowed to hand off to that target. - Payload:
from,to: Agent names.reason: Human-readable explanation of the denial.
- Notes: The run transitions into an error outcome after this event.
- Emitted from: Guardrail checks in
runInternal(). - When:
- Stage
'input': Initial user message failsinitialInputGuardrailsbefore the first turn. - Stage
'output': Final output failsfinalOutputGuardrails.
- Stage
- Payload:
stage:'input' | 'output'.reason: Message supplied by the failing guardrail.
- Notes: Immediately followed by error termination for the current run.
- Emitted from:
runInternal()when an agentoutputCodeccannot parse the assistant response. - Payload:
errors: Array ofz.ZodIssueobjects from the failedsafeParse.
- Notes: Leads directly to an error outcome; no
final_outputevent follows.
- Emitted from:
runInternal()right before each LLM invocation. - Payload:
turn: 1-based turn number (incremented before emission).agentName: Agent executing the turn.
- Notes: Fires even for turns that later end with errors or interruptions.
- Emitted from: Multiple points in
runInternal()whenever a turn concludes. - When: After tool processing, guardrail failures, decode errors, or final result generation.
- Payload:
turn: Same 1-based counter as the matchingturn_start.agentName: Agent that just completed the turn.
- Notes: May fire multiple times per run; always paired with a prior
turn_start.
- Emitted from:
runInternal()just before returning a successfulRunResult. - Payload:
output: Parsed value from the agent (codec result or raw string content).
- Notes: Only emitted on completed runs; skipped for interruptions and errors.
- Emitted from:
- Normal completion path in
run(). - Error catch block in
run()if an exception escapes.
- Normal completion path in
- Payload:
outcome: TheRunResult['outcome']object (completed,interrupted, orerror).traceId,runId.
- Notes: Guaranteed final event for every run, even when upstream execution throws synchronously.
While not part of the requested list, engine.ts also emits an agent_processing event before each turn with extended diagnostic data (message stats, model config, available tools, handoff options). Collectors that need richer telemetry can subscribe to it alongside the events above.
- The
runStream()helper wrapsrun()and emits the sameTraceEventpayloads through an async iterator, making it suitable for Server-Sent Events or WebSockets. - Events are pushed in the order they occur; consumers should handle multiple
assistant_messageevents per turn and rely onturn_end/run_endto detect completion.