Agents
Streaming
Stream agent output, tool calls, and normalized runtime events.
Use .stream() when the UI needs incremental text or when you want to observe tool calls as they happen.
Basic Stream
for await (const event of agent.prompt("Write a short reply.").stream()) {
if (event.type === "text_delta") {
process.stdout.write(event.delta);
}
}The agent model must support streaming. If the selected completion model does not support streaming, Anvia throws before the run starts.
Event Types
| Event | Meaning |
|---|---|
turn_start | A model turn is starting |
text_delta | Assistant text arrived |
reasoning_delta | Reasoning text arrived from a provider that exposes it |
tool_call | The model requested a tool |
tool_result | Anvia ran a tool and produced a result |
tool_approval_request | A guarded tool needs approval |
tool_approval_result | Approval was resolved |
turn_end | A model turn ended |
final | The agent run completed |
error | The stream failed |
Streaming With Tools
Tool workflows can span multiple model turns.
for await (const event of agent.prompt("Where is order A-100?").stream()) {
if (event.type === "tool_call") {
console.log("tool", event.toolCall.function.name);
}
if (event.type === "tool_result") {
console.log("result", event.result);
}
if (event.type === "final") {
console.log(event.output);
}
}The final event contains the same shape you get from .send(): output, usage, messages, and trace metadata.
Readable Streams
Convert async iterable events to newline-delimited JSON when you need a web stream.
import { toReadableStream } from "@anvia/core";
const stream = toReadableStream(agent.prompt("Draft a reply.").stream());This is useful for HTTP responses, server functions, and UI frameworks that consume ReadableStream.
