LLM Text Streaming
When to Use This Skill
- Adding token or chunk progress to an LLM-powered step
- Choosing between completed generation and direct stream access
- Using
onChunk, oronEnd/onErroronstreamText()/Agent.stream() - Making stream failures trigger Temporal activity retries
- Streaming Agent responses or persisting streamed conversations
Choose the API
In workflow steps, prefer generateTextWithStreaming() or Agent.generateWithStreaming() when onChunk progress is sufficient. They consume the stream internally, return complete results like generateText() or Agent.generate(), and reject on provider, transport, or abort errors. Rejection allows Temporal to record the failed activity attempt and apply the step retry policy.
streamText() and Agent.stream() remain supported for code that needs direct control over stream consumption.
generateTextWithStreaming()
The result has the same complete response fields as generateText(), including result, text, output, usage, finishReason, and cost. Structured output passed with aiSdk.Output.* is available through result.output.
Agent.generateWithStreaming()
generateWithStreaming() returns a complete Agent response and automatically stores messages when the Agent has a messageStore.
Direct stream error handling
AI SDK streaming delivers provider and transport failures through onError. Iterating textStream does not reliably throw the original error. When using streamText() in a workflow step, capture the error and throw it after consumption:
Registering onError without throwing the captured error can let the step return an empty successful result, preventing Temporal from retrying it. Awaiting a completion property may also produce a generic no-output error instead of the original provider error.
Agent.stream() stores conversation messages in its wrapped onEnd when finishReason is not 'error'. Use Agent.generateWithStreaming() when a complete stored response meets the requirement.
Streaming call arguments: prompt, promptDir, variables, tools, output, toolChoice, stopWhen, abortSignal, plus onChunk (generateTextWithStreaming) or onChunk / onEnd / onError (streamText). Agent methods: messages, abortSignal, toolChoice, plus those same stream callbacks.
Rules
- Prefer the completed streaming APIs in Temporal steps unless direct stream access is required.
- Do not rely on
onErroralone to fail a step usingstreamText(). - Throw the captured error only after stream consumption finishes.
- Keep
onChunkside effects bounded. A Temporal signal per token creates a history event per signal, so batch high-frequency updates. - Do not describe
streamText()orAgent.stream()as deprecated.
Related Skills
output-dev-step-function- Put LLM calls inside Temporal activity stepsoutput-dev-agent-class- Construct and use reusable Agentsoutput-dev-prompt-file- Create prompt files for generationoutput-error-try-catch- Handle step and workflow failures


