Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Const

Token Streaming

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.

How it works

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.

Syntax

streamText({ model, prompt })

Parameters

NameTypeDescription
modelLanguageModelThe language model that should produce the streamed generation.
promptstringThe input prompt used to request the generation.

Returns

A streamText result containing the generated text stream and support for streaming-related generation results.

Key points

  • streamText is the core function for incrementally receiving model-generated text.
  • The API is designed for experiences where users benefit from seeing output before generation completes.
  • Tool-enabled flows can use streamText beyond plain text to support generated interface components.

Common mistakes

  • Streaming is intended for interactive output; use the streaming API when the application should show results during generation rather than wait for a complete response.

Example: Stream generated text

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.

When to use it

Responsive chat

A chatbot displays the assistant's response progressively as it is generated.

Real-time interfaces

A live application renders model output without waiting for completion.

Generated UI

A tool-enabled model flow produces interface elements during generation.

Interview questions

QWhat is token streaming in the AI SDK?

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.

QWhy use streamText instead of a non-streaming text-generation call?

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.

QHow is streamed output consumed in a TypeScript application?

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.

  • streamText
  • textStream
  • LanguageModel
  • tools

Source: API Reference for streamText. Checked 2026-10-15.