• English
  • Events

    Every run yields a typed AgentEvent stream — the single channel for streaming text to a UI, showing tool activity, and servicing approvals and user questions. Events are observe only: handling them never changes agent behavior, so you can build any rendering or logging layer on top without touching the agent.

    You already consume the stream whenever you iterate query():

    import { query } from "@lite-agent/sdk";
    import { anthropic } from "@lite-agent/provider";
    
    for await (const ev of query({
      prompt: "Summarize this project.",
      model: anthropic(),
      modelName: "claude-sonnet-4-6",
      cwd: process.cwd(),
    })) {
      if (ev.type === "text_delta") process.stdout.write(ev.text);
    }

    Events forwarded from a subagent carry an agentId; the main agent's events don't.

    Rendering text

    Text arrives incrementally as text_delta chunks — write them through for a streaming UI. When a turn's full assistant message is ready, a message event carries it; when the whole run finishes, done carries the final RunResult:

    for await (const ev of query({ /* ... */ })) {
      switch (ev.type) {
        case "text_delta":
          process.stdout.write(ev.text);       // stream chunks as they arrive
          break;
        case "tool_call_start":
          console.error(`\n[tool] ${ev.call.name}`);
          break;
        case "error":
          if (ev.fatal) console.error(ev.error);
          break;
      }
    }

    Interactive events: approvals and user input

    Two event pairs correspond to points where the run suspends and waits for your handler:

    • approval_requestapproval_resolved — the permission gate returned ask for a tool call; your onApproval handler answers "allow" or "deny". See Permissions.
    • input_requestinput_resolved — the model called ask_user; your onAskUser handler returns the answer string.

    Use the events to render the prompt in your UI (e.g. show which tool call is awaiting approval); use the handlers to actually answer. The run blocks until the handler resolves.

    Full event reference

    EventPayloadEmitted when
    turn_startturnA turn begins.
    model_call_startturn, modelA model call starts.
    model_call_endturn, model, durationMs, usage?, error?A model call finishes (or fails).
    text_deltatextA streamed text chunk arrives.
    messagemessageThe full assistant message for a turn is complete.
    tool_usecallThe model requested a tool call.
    tool_call_startcall, turnA tool call starts executing.
    tool_call_endid, name, turn, durationMs, isErrorA tool call finishes.
    tool_recoveredid, name, turnAn interrupted call is closed on resume (safe crash recovery).
    tool_resultresultA tool result is fed back into the conversation.
    permission_decisioncall, decision, ruleId?, reason?, simulated?, byThe permission layer decides allow / deny / ask (by: policy / user / auto).
    approval_requestcall, reason?An ask verdict suspends the call for approval.
    approval_resolvedid, decision, byThe approval handler answered.
    input_requestcall, questionThe model asks the user a question (ask_user).
    input_resolvedid, answerThe input handler answered.
    steermessagesInput was injected mid-run via a SteerController.
    compactionkind, before, after, phase?Context compaction starts / finishes (micro / auto / manual).
    context_statussessionId, level, reason, beforeTokens, afterTokens, generation, plannerUsed, plannerFallback, plannerLatencyMs, archiveRefs, retryThe ContextEngine reports automatic context management.
    background_completedcompletionA background task finished.
    diagnosticlevel, code, messageNon-fatal diagnostics (info / warning / error).
    turn_endturn, stopReasonA turn ends (stop / tool_use / max_tokens).
    errorerror, fatalAn AgentError occurs; fatal ends the run.
    donereason, resultThe run finishes (stop / aborted / max_turns) with the final RunResult.

    See also

    • Agent loop — the five steps that produce these events.
    • Sessions — events are what gets persisted per session.
    • Permissions — the approval flow behind approval_request / approval_resolved.
    • Core strategiesApprovalHandler and InputHandler interfaces.