Skip to content
EN DE

Consuming Streams

streamText returns a result object with two async iterables for consuming the stream: textStream (text chunks only) and fullStream (all part types). Which one you use depends on whether you need just the text or the full picture including tool calls, reasoning, and metadata.

The result also exposes aggregated promises that resolve when the stream completes: text, usage, totalUsage, finishReason, and steps. These let you await final values without manually accumulating stream chunks.

Both textStream and fullStream are consumed with for await...of. Once consumed, the stream is exhausted — you cannot iterate it again. If you need both text streaming and final aggregates, use the promises alongside the stream.

import { streamText } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';
const result = streamText({
model: anthropic('claude-sonnet-4-5-20250514'),
prompt: 'Explain streaming in 3 sentences.',
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
for await (const part of result.fullStream) {
switch (part.type) {
case 'text-delta':
process.stdout.write(part.textDelta);
break;
case 'reasoning':
console.log('Reasoning:', part.textDelta);
break;
case 'tool-call':
console.log('Tool:', part.toolName, part.args);
break;
case 'tool-result':
console.log('Result:', part.result);
break;
case 'step-start':
console.log('Step started');
break;
case 'step-finish':
console.log('Step done:', part.finishReason);
break;
}
}
TypeKey PropertiesDescription
text-deltatextDeltaIncremental text chunk
reasoningtextDeltaChain-of-thought reasoning chunk
sourcesourceSource citation URL
tool-calltoolName, argsComplete tool invocation
tool-call-streaming-starttoolCallId, toolNameTool call begins streaming
tool-call-deltatoolCallId, argsTextDeltaIncremental tool call arguments
tool-resulttoolName, resultTool execution result
step-startGeneration step begins
step-finishfinishReason, usage, isContinuedGeneration step completes
const result = streamText({ model, prompt });
// Consume the stream first
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
// Then await aggregated values
const finalText = await result.text;
const usage = await result.usage;
const totalUsage = await result.totalUsage;
const reason = await result.finishReason;
const steps = await result.steps;

Part of AI Learning — free courses from prompt to production. Jan on LinkedIn