Completions

Streaming Completions

Stream normalized raw model events from a direct completion.

Use createCompletionStream when you want normalized provider stream events without agent runtime events.

import { createCompletionStream } from "@anvia/core";

for await (const event of createCompletionStream(model, {
  input: "Write a short launch note.",
})) {
  if (event.type === "text_delta") {
    process.stdout.write(event.delta);
  }

  if (event.type === "final") {
    console.log(event.response.usage);
  }
}

createCompletionStream returns the model's CompletionStreamEvent values directly.

Event Types

Direct completion streams can include:

EventMeaning
text_deltaAssistant text arrived
reasoning_deltaReasoning text or summary arrived from a provider that exposes it
tool_call_deltaPartial tool call data arrived
tool_callThe model emitted a complete tool call
message_idThe provider emitted a message id
finalThe model stream completed with a normalized response
errorThe stream failed

Raw Model Stream, Not Agent Stream

Direct completion streams do not execute tools and do not emit agent stream events such as:

  • turn_start
  • turn_end
  • tool_result
  • agent_tool_event
  • agent final output

For streams with tool execution and turn events, use agent.prompt(...).stream().

Capability Validation

createCompletionStream throws before returning the model stream if streaming or another requested feature is unsupported:

try {
  const stream = createCompletionStream(model, {
    input: "Write a live status update.",
  });

  for await (const event of stream) {
    console.log(event.type);
  }
} catch (error) {
  console.error(error);
}