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:
| Event | Meaning |
|---|---|
text_delta | Assistant text arrived |
reasoning_delta | Reasoning text or summary arrived from a provider that exposes it |
tool_call_delta | Partial tool call data arrived |
tool_call | The model emitted a complete tool call |
message_id | The provider emitted a message id |
final | The model stream completed with a normalized response |
error | The 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_startturn_endtool_resultagent_tool_event- agent
finaloutput
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);
}