createAgentUIStream
The createAgentUIStream function executes an Agent, consumes an array of UI messages, and streams the agent's output as UI message chunks via an async iterable. This enables real-time, incremental rendering of AI assistant output with full access to tool use, intermediate reasoning, and interactive UI features in your own runtime—perfect for building chat APIs, dashboards, or bots powered by agents.
Import
import { createAgentUIStream } from "ai"
Usage
Parameters
tools and implement .stream({ prompt, ... }).onStepEnd instead. This alias is only used as a fallback when onStepEnd is not provided.messages, isContinuation, and the accumulated responseMessage, making it suitable for persisting intermediate UI message state.Returns
A Promise<AsyncIterableStream<UIMessageChunk>>, where each yielded chunk is a UI message output from the agent (see UIMessage). This can be consumed with any async iterator loop, or piped to a streaming HTTP response, socket, or any other sink.
Example
Persisting the UI Message After Each Step
Use onUIMessageStepEnd when you need the accumulated UIMessage after each agent step:
onStepEnd and onUIMessageStepEnd serve different layers. onStepEnd receives the low-level generation step result, including usage, finish reason, and tool call details. onUIMessageStepEnd runs after that step has been converted to UI message parts and receives the response message accumulated so far.
The callback runs as the UI stream is consumed and is awaited before the finish-step chunk continues through the stream. This delays UI stream delivery; it does not guarantee that the next model call has not started. If it throws, the error is passed to onError and streaming continues. Use idempotent writes because each callback contains a growing snapshot of the same response message. UI messages can contain model output, reasoning, tool inputs, and tool outputs, so apply the same access controls and sensitive-data handling that you use for final conversation persistence.
How It Works
- UI Message Validation: The input
uiMessagesarray is validated and normalized using the agent'stoolsdefinition. Any invalid messages cause an error. - Conversion to Model Messages: The validated UI messages are converted into model-specific message format, as required by the agent.
- Agent Streaming: The agent's
.stream({ prompt, ... })method is invoked with the converted model messages, optional call options, abort signal, experimental_sandbox, and any experimental transforms. - UI Message Stream Building: The result stream is converted and exposed as a streaming async iterable of UI message chunks for you to consume.
Notes
- The agent must implement the
.stream({ prompt, ... })method and define its supportedtoolsproperty. - This utility returns an async iterable for maximal streaming flexibility. For HTTP responses, see
createAgentUIStreamResponse(Web) orpipeAgentUIStreamToResponse(Node.js). - The
uiMessagesparameter is nameduiMessages, not justmessages. - You can provide advanced options via
UIMessageStreamOptions(for example, to include sources or message metadata). - Use
onUIMessageStepEndfor per-step UI message persistence. UseonStepEndfor low-level generation telemetry and step details. - To cancel the stream, pass an
AbortSignalvia theabortSignalparameter. - Pass
experimental_sandboxwhen your agent tools need an experimental sandbox environment during execution.