Basics
Stream completion
Stream normalized completion events from a model.
Use createCompletionStream when you want direct model streaming before adding agent runtime events.
When to use this
Stream direct completions when your UI or CLI should show text while the model is still generating, but you do not need tools, memory, or multi-turn agent behavior.
Prerequisites
Complete the direct completion step first so you know your provider model works.
Stream text
import { createCompletionStream } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
const client = new OpenAIClient({
apiKey: process.env.OPENAI_API_KEY,
});
const model = client.completionModel("gpt-5");
for await (const event of createCompletionStream(model, {
input: "Write a short launch note.",
})) {
if (event.type === "text_delta") {
process.stdout.write(event.delta);
}
}
What happens
The stream yields normalized completion events from the provider model. The most common event for simple text output is text_delta.
This is lower level than agent streaming. Agent streams include runtime events such as tool calls, tool results, turns, and final run metadata.
Check yourself
Run the example and confirm text appears incrementally instead of only after the model finishes.
Next
Return to the main path and build your first agent.
Need validated data from a direct model call? Continue to Structured output.
