Anvia
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

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

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.