Anvia
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
final

Tool runs can span multiple turns:

turn_start
tool_call
turn_end
tool_result
turn_start
text_delta
turn_end
final

4. Event Types

EventMeaning
turn_startA model turn is starting
text_deltaAssistant text arrived
reasoning_deltaReasoning text arrived from a provider that exposes it
tool_callThe model requested a tool
tool_resultAnvia ran a tool and produced a result
tool_approval_requestA guarded tool needs approval
tool_approval_resultApproval was resolved
turn_endA model turn ended
finalThe agent run completed
errorThe stream failed

The final event contains the same important data as .send(): output, usage, messages, and optional trace.