Streaming
Streaming Events
Stream normalized runtime events from Anvia agents.
Use .stream() when your UI, worker, or API route needs incremental updates instead of waiting for the final response.
1. Start a 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's completion model must support streaming. If the model does not support streaming, Anvia throws before the run starts.
2. Handle the Core Events
for await (const event of agent.prompt("Where is order A-100?").stream()) {
switch (event.type) {
case "text_delta":
process.stdout.write(event.delta);
break;
case "tool_call":
console.log("tool_call", event.toolCall.function.name);
break;
case "tool_result":
console.log("tool_result", event.toolName, event.result);
break;
case "final":
console.log("done", event.output);
break;
}
}3. Know the Event Order
Most text-only runs look like this:
turn_start
text_delta
text_delta
turn_end
finalTool runs can span multiple turns:
turn_start
tool_call
turn_end
tool_result
turn_start
text_delta
turn_end
final4. 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 |
The final event contains the same important data as .send(): output, usage, messages, and optional trace.
