Zum Inhalt springen
EN DE

Streams konsumieren

streamText gibt ein Result-Objekt zurück, das zwei AsyncIterables bereitstellt: textStream liefert nur Text-Chunks als Strings — ideal für einfache Ausgaben. fullStream liefert alle Stream Part Types inklusive Tool-Calls, Reasoning, Sources und Step-Grenzen.

Zusätzlich bietet das Result aggregierte Promises wie text, usage, totalUsage, finishReason, steps, toolCalls und toolResults. Diese kannst Du awaiten, um die Gesamtdaten nach Abschluss des Streams zu erhalten.

import { streamText } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';
const { textStream } = streamText({
model: anthropic('claude-sonnet-4-5-20250514'),
prompt: 'Erklaere Streaming in einem Absatz.',
});
for await (const chunk of textStream) {
process.stdout.write(chunk);
}
const { fullStream } = streamText({
model: anthropic('claude-sonnet-4-5-20250514'),
prompt: 'Wie ist das Wetter in Berlin?',
tools: { weather: weatherTool },
maxSteps: 3,
});
for await (const part of fullStream) {
switch (part.type) {
case 'text-delta':
console.log('Text:', 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-finish':
console.log('Step done:', part.finishReason);
break;
case 'finish':
console.log('Stream complete:', part.finishReason);
break;
case 'error':
console.error('Stream error:', part.error);
break;
}
}
TypeBeschreibungWichtige Properties
text-deltaText-DeltatextDelta
reasoningReasoning / Chain-of-Thought TexttextDelta
sourceQuellenreferenzsource
tool-callTool-AufruftoolName, args, toolCallId
tool-call-streaming-startStart eines gestreamten Tool-CallstoolCallId, toolName
tool-call-deltaInkrementelle Tool-ArgumenteargsTextDelta
tool-resultTool-ErgebnistoolName, result, toolCallId
step-startNeuer Generierungsschritt beginntmessageId, request, warnings
step-finishSchritt abgeschlossenfinishReason, usage, messageId, response, isContinued
finishStream abgeschlossenfinishReason, usage, providerMetadata
errorFehler im Streamerror
PromiseTypBeschreibung
textPromise<string>Vollstaendiger generierter Text
usagePromise<Usage>Token-Verbrauch des letzten Schritts
totalUsagePromise<Usage>Token-Verbrauch aller Schritte
finishReasonPromise<FinishReason>Grund für das Stoppen
stepsPromise<StepResult[]>Array aller Schritte
toolCallsPromise<ToolCall[]>Alle Tool-Calls des letzten Schritts
toolResultsPromise<ToolResult[]>Alle Tool-Ergebnisse des letzten Schritts

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