Skip to main content
Use streaming when your application needs live output — chat UIs, progress indicators, or AG-UI-compatible frontends. Agent.Stream returns an AgentStream handle. Call Events for the live channel. For a final result without live tokens, use Run instead.

AgentStream methods

Status / Cancel / Done / Get match AgentRun. Streaming adds Events.

Enable streaming

Create an agent, then call Stream. Stream enables LLM token deltas and delivers AG-UI lifecycle events: LLM token deltas (TEXT_MESSAGE_CONTENT) are emitted only when the underlying LLMClient reports IsStreamSupported() as true (OpenAI, Anthropic, Gemini, DeepSeek, and Ollama do). If the provider does not support streaming, you still get lifecycle and tool events — the assistant text arrives as a complete message rather than partial deltas.
Then call Stream:
Always check if ev == nil { continue } before calling ev.Type(). The channel can deliver a nil sentinel to signal the end of a batch.

Event guarantees

On all runtimes, the event channel is guaranteed to close only after a terminal RUN_FINISHED or RUN_ERROR event is delivered when the run itself ends. This holds even on Temporal if:
  • Your process crashes during streaming
  • The stream connection fails transiently
  • You cancel the Events context (subscriber disconnect) — the agent run continues; reconnect with GetAgentStream
Cancelling the Stream context cancels the agent run and surfaces a terminal event on active subscribers. If you reconnect via GetAgentStream with a saved offset, you will not miss the terminal event (offsets may redeliver — discard duplicates at or below the saved offset). For in-process streaming, a stream error (rare) will still deliver a terminal event before channel close.
When using Temporal for crash-recovery, always save both the run ID and offset before processing each event. If your process crashes between reading an event and saving its offset, only that one event will replay on reconnect.

Disable LLM token streaming

By default, Stream enables LLM token streaming — the model emits partial text as TEXT_MESSAGE_CONTENT deltas. Lifecycle events (tools, approvals, RUN_FINISHED, …) still stream either way. To turn off token deltas and receive a single complete assistant message instead, set DisableTokenStreaming on AgentStreamOptions:
Use this when you want AG-UI / tool lifecycle events without partial token delivery (for example, lower provider stream overhead or a UI that only renders the final message).

Event types

Stream events are typed AgentEvent values. Use ev.Type() and type-assert to the concrete struct to access fields.

Lifecycle

Text output

Tool calls

Reasoning

Custom (approvals and delegation)

Parse approval events with ParseCustomEventApproval / ParseCustomEventDelegation, then call AgentStream.Approve with the ApprovalToken. See Approvals for the full flow.

Displaying streamed text

TEXT_MESSAGE_CONTENT deltas and AgentRunFinishedEvent.Result.Content carry the same text. Do not print both. If you rendered deltas live, skip printing result.Content from RUN_FINISHED.
When sub-agents are configured, events from delegated runs fan in to the parent stream. Use AgentName on typed events to identify which agent emitted them. Multiple RUN_FINISHED events may appear before the root run finishes — each corresponds to one completed sub-agent run.

Token usage from Stream

Aggregated token counts are on Result.LLMUsage inside AgentRunFinishedEvent:
See Token Usage for field details.

Reconnect with GetAgentStream

On a durable runtime (Temporal or Restate), persist agentStream.ID() and each event’s offset before handling the event. After a process crash, reconnect and resume — the run keeps executing server-side:
The offset is opaque and is attached only to events that support reconnect (lifecycle, text, tools, custom events all carry it). Cancelling GetAgentStream or Events does not stop the run — use AgentStream.Cancel for that. After reconnect, WithTimeout (if set) starts fresh. Full cancel/timeout rules: Timeouts & Modes. LocalRuntime does not support crash reconnect — non-zero offsets return ErrStreamOffsetNotSupported. Full protocol: Durable Execution. Runnable demo: Reconnect.

Streaming with conversation history

Pass ConversationOptions on AgentStreamOptions to share history across turns while streaming:
See Conversation for backend options and lifecycle management.

AG-UI protocol

Stream events follow the AG-UI open protocol. Serialize any event with event.ToJSON() and forward over SSE or WebSocket to a compatible frontend. See AG-UI Protocol.

Examples

Stream example

Partial tokens, tool events, and RUN_FINISHED handling

Reconnect

Resume a stream from a saved offset after a crash

AG-UI

SSE server + CopilotKit UI over AG-UI events

Run

AgentRun — Get, Done, Status, Cancel