content/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx
createAgentUIStreamThe 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.
<Snippet text={import { createAgentUIStream } from "ai"} prompt={false} />
import { ToolLoopAgent, createAgentUIStream } from 'ai';
__PROVIDER_IMPORT__;
const agent = new ToolLoopAgent({
model: __MODEL__,
instructions: 'You are a helpful assistant.',
tools: { weather: weatherTool, calculator: calculatorTool },
});
export async function* streamAgent(
uiMessages: unknown[],
abortSignal?: AbortSignal,
) {
const stream = await createAgentUIStream({
agent,
uiMessages,
abortSignal,
// ...other options (see below)
});
for await (const chunk of stream) {
yield chunk; // Each chunk is a UI message output from the agent.
}
}
<PropertiesTable
content={[
{
name: 'agent',
type: 'Agent',
isRequired: true,
description:
'The agent to run. Must define its tools and implement .stream({ prompt, ... }).',
},
{
name: 'uiMessages',
type: 'unknown[]',
isRequired: true,
description:
'Array of input UI message objects (e.g., user/assistant/chat history). These will be validated and converted for the agent.',
},
{
name: 'abortSignal',
type: 'AbortSignal',
isRequired: false,
description:
'Optional abort signal to cancel the stream early (for example, if the client disconnects).',
},
{
name: 'timeout',
type: 'number | { totalMs?: number }',
isRequired: false,
description:
'Timeout in milliseconds. Can be specified as a number or as an object with a totalMs property. The call will be aborted if it takes longer than the specified timeout. Can be used alongside abortSignal.',
},
{
name: 'options',
type: 'CALL_OPTIONS',
isRequired: false,
description:
'Optional agent call options, only needed if your agent expects extra configuration (see agent generic parameters).',
},
{
name: 'experimental_transform',
type: 'StreamTextTransform | StreamTextTransform[]',
isRequired: false,
description:
'Optional transformations to apply to the agent output stream (experimental).',
},
{
name: 'onStepFinish',
type: 'ToolLoopAgentOnStepFinishCallback',
isRequired: false,
description:
'Callback invoked after each agent step (LLM/tool call) completes. Useful for tracking token usage or logging intermediate steps.',
},
{
name: '...UIMessageStreamOptions',
type: 'UIMessageStreamOptions',
isRequired: false,
description:
'Additional options to control the output stream, such as including sources or usage data.',
},
]}
/>
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.
import { createAgentUIStream } from 'ai';
const controller = new AbortController();
const stream = await createAgentUIStream({
agent,
uiMessages: [{ role: 'user', content: 'What is the weather in SF today?' }],
abortSignal: controller.signal,
sendStart: true,
// ...other UIMessageStreamOptions
});
for await (const chunk of stream) {
// Each chunk is a UI message update — stream it to your client, dashboard, logs, etc.
console.log(chunk);
}
// Call controller.abort() to cancel the agent operation early.
uiMessages array is validated and normalized using the agent's tools definition. Any invalid messages cause an error..stream({ prompt, ... }) method is invoked with the converted model messages, optional call options, abort signal, and any experimental transforms..stream({ prompt, ... }) method and define its supported tools property.createAgentUIStreamResponse (Web) or pipeAgentUIStreamToResponse (Node.js).uiMessages parameter is named uiMessages, not just messages.UIMessageStreamOptions (for example, to include sources or usage).AbortSignal via the abortSignal parameter.