Streaming ​
Streaming exposes model or agent progress before the complete response is available. Anvia provides two levels:
Completion stream: one provider request -> normalized provider events
Agent stream: runtime turns -> tools -> final agent resultUse the lowest level that matches the workflow. A direct completion stream is simpler when one model call is enough. An agent stream is required when Anvia should execute local tools and continue the model loop.
1. Stream one completion ​
import { streamCompletion } from '@anvia/core'
const events = streamCompletion({
prompt: 'Write a short launch note for Anvia.',
model,
instructions: 'Write clearly and concisely.'
})
for await (const event of events) {
if (event.type === 'text_delta') {
process.stdout.write(event.delta)
}
}Direct completion streams normalize provider output but do not execute local tool calls.
2. Stream an agent run ​
const events = agent.stream({
prompt: 'Check the account and explain the current status.'
})
for await (const event of events) {
if (event.type === 'tool_result') {
console.log('Completed:', event.toolName)
}
if (event.type === 'text_delta') {
process.stdout.write(event.delta)
}
if (event.type === 'response' || event.type === 'interaction' || event.type === 'blocked') {
console.log('\nTokens:', event.usage.totalTokens)
}
}An agent stream covers the complete runtime loop and ends with a direct response, interaction, or blocked outcome. On failure it yields an error event with cumulative usage and the iterator completes normally; the same failure rejects the stream.result promise. See Errors and cancellation.
3. Continue through the section ​
Treat stream events as workflow state, not only text. Decide which progress, errors, usage, approvals, and tool activity belong in each product surface. Keep raw reasoning, tool arguments, tool results, and provider metadata off public transports unless they have been explicitly reviewed.