Responsive chat
A chatbot displays the assistant's response progressively as it is generated.
AI Tools · Const
Token streaming uses streamText to deliver language-model text incrementally. It enables responsive chatbots and other real-time applications, and can also work with tools to generate UI components.
Token streaming uses streamText to expose generated output incrementally through a text stream, allowing an application to display partial results while generation continues. The API is suited to interactive experiences such as chatbots and other real-time applications, and the result can also support tool-driven UI generation.
streamText({ model, prompt })| Name | Type | Description |
|---|---|---|
model | LanguageModel | The language model that should produce the streamed generation. |
prompt | string | The input prompt used to request the generation. |
A streamText result containing the generated text stream and support for streaming-related generation results.
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';
const result = streamText({
model: openai('gpt-4o'),
prompt: 'Explain token streaming in one paragraph.',
});
for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}This runnable TypeScript example streams generated text and writes each arriving text part immediately.
A chatbot displays the assistant's response progressively as it is generated.
A live application renders model output without waiting for completion.
A tool-enabled model flow produces interface elements during generation.
Use streamText with a language model and prompt, then consume the returned textStream incrementally. This lets interactive applications render generated text as it becomes available instead of waiting for the whole generation.
Follow-up: How would you extend the flow beyond plain text? The documentation notes that streamText can also generate UI components with tools, so the application can combine streamed generation with tool interactions.
The AI SDK's streamText function streams text generations from a language model. Its primary benefit is progressive delivery for chatbots and other real-time experiences.
Follow-up: What should be considered when choosing this approach? The key trade-off is between progressive responsiveness and the additional handling required for an ongoing stream, such as consuming chunks and presenting partial output.
Call streamText with the selected model and a prompt, then iterate over result.textStream. Each received text part can be written to a response or UI immediately.
Follow-up: How can the same mechanism support richer applications? The page indicates that tools can be used to generate UI components, allowing streamed text generation to participate in tool-enabled interfaces.
streamTexttextStreamLanguageModeltools