AI streams
Text or AG-UI runs that survive a refresh, with approvals and Stop on the same channel.
A stream channel holds a run: plain text, or an agent's AG-UI events with tool calls and approvals. Each event has an offset. Viewers see tokens as they're written; a refreshed tab, a second device or a late joiner replays the run so far and then follows it live. Stop travels back on the same channel.
Using the AI SDK's useChat? See Vercel AI SDK.
Stream text
pipe takes any source of strings (an async iterable or a ReadableStream), starts a run, writes it token by token and finishes the run. Writes are gathered into frames, so writing every token never hits a rate limit.
useStream gives the latest run's text and its status (running, finished, interrupted or error).
- If the source throws, the run ends with an error (
statusiserror) andpiperethrows. - If a viewer presses Stop,
pipecancels the source and ends the run asinterrupted. Passs.signalto your model call so it stops too. - Without
pipe, write text yourself and finish the run:
Write an agent run
For chat with tool calls and approvals, write AG-UI events from your server, token by token. Deltas are gathered into frames too.
Tool calls
Ask the viewer
requestApproval shows an approval card to everyone watching and waits for the answer, which a viewer sends back on the channel. It resolves { approved: false } if the run is stopped first.
Read in the browser
useStream replays the channel on mount and folds the events into chat messages: text, tool calls and approval cards, with the run's status (running, awaiting-approval, finished, interrupted or error).
Or render the whole thread with the optional components: AgentThread for a channel, or AgentMessage for one message.
Stop
interrupt() in the browser asks the agent to stop. On the server, s.signal aborts and s.interrupted turns true, so pass the signal to your model call.
Durability
By default, frames reach viewers as soon as they're written and are stored a moment later. That suits chat: in the rare case the channel restarts mid-run, viewers may have to reload the answer, and nothing is stored twice. durability: "frame" stores every frame before anyone sees it, so nothing a viewer saw can be lost:
- Choose it when a viewer acts on what it sees mid-run: tool calls that drive your UI, a transcript you keep as a record, another system reading the stream.
- Keep the default for chat answers, which a restart only interrupts.
Bring your own AG-UI agent
The channel stores AG-UI events as they are: the event name is the type and the data is the event. (Plain text is a text event with the string as its data.) If your agent already speaks AG-UI, write its events straight through, and any AG-UI client can read the channel.
A stream channel keeps its whole run until 24 hours after its last event, so anyone can replay it in that time. For everything else on a channel, see history and replay.