Call Model Streaming

Use async iteration to render low-latency output in UI and CLIs.

Streaming Iterator

TypeScript
const stream = await client.callModel.stream({ model, input });

for await (const event of stream) {
  if (event.type === "text.delta") process.stdout.write(event.delta);
  if (event.type === "tool_call") console.log("tool:", event.name);
}

Stream Event Types

NameTypeRequiredDescription
text.delta
eventRequiredIncremental token text payload.
tool_call
eventTool call emitted during stream.
usage
eventUsage summary near stream end.
completed
eventRequiredTerminal event with stop reason.

UI Buffering

buffer.ts
let text = "";
for await (const event of stream) {
  if (event.type === "text.delta") text += event.delta;
}
Backpressure
If your consumer is slow, queue events and flush on animation frames to keep the interface responsive.