Skip to main content
Use agent.stream() when your application needs progress before the final answer: a chat interface can display text, a service can observe tool calls, and a caller can cancel work. The returned AgentStream is an async iterator with session and cancellation controls. Use the TypeScript example setup for the complete example below.

Stream text as it arrives

Partial messages are opt-in. A part event identifies each part’s type; a partial event carries its delta. This example prints only text parts, so reasoning deltas do not become user-visible answer text.
The final result.text is authoritative for the final assistant message. Earlier assistant text can include intermediate commentary before tool calls. For a transcript UI, group text by messageId and partId; do not treat every assistant message as the final answer. Do not concatenate complete assistant.text snapshots with partial deltas into the same answer buffer: they describe overlapping text. If your application only needs complete assistant messages, omit includePartialMessages and handle message.type === "assistant" instead.

Observe work and results

Every event is part of the discriminated AgentMessage union. Check type before reading its fields. The SDK also emits user, todo, checkpoint, rewind, and raw event messages. See AgentMessage for the full field definitions. A successful tool result does not by itself mean the overall task succeeded; check the terminal result.

Cancellation and cleanup

Pass a caller-owned AbortSignal or an AbortSignal.timeout(...) deadline. For an explicit cancel button, create an AbortController, pass controller.signal, and call controller.abort() from the button handler. Cancellation can produce an error_aborted result; startup or transport failures can also throw. Keep cleanup in finally. Dropping a reference to an iterator without returning, closing, or aborting it is not cancellation. Closing the example’s explicitly spawned Agent stops its dedicated runtime; a default shared runtime instead requires shutdown() at application teardown.

Continue a conversation

session.stream(prompt, { includePartialMessages: true }) streams a turn within an existing session. It yields the same AgentMessage shapes, but closing the iterator does not transfer ownership of the Session or its client. Keep the Session for the next turn and close it when the conversation is done. Await or cancel each turn before starting another on that session. In v2, Session stream options do not accept signal; use session.interrupt() for an active Session turn. The signal option in this guide’s complete example is provided by the higher-level Agent.stream() wrapper.

Feed input in order

PromptInput accepts a string, a { text, parts } turn, or an async iterable of turns. An iterable submits turns sequentially in one session; it does not create concurrent queries. Using the configured Agent from the complete example, replace its stream/consumption block with:
When streaming an iterable input, each completed input turn can yield its own terminal result event. agent.run() returns the terminal result from the last submitted turn. For interactive applications, explicit sessions are usually simpler.