Streaming

Set stream: true and read tokens as they are produced.

Streaming turns the response into server-sent events. Each event carries a delta with the newest fragment; the stream ends with a [DONE] sentinel. Every text endpoint supports it, and the SDKs expose it as an async iterator.

const stream = await client.chat.completions.create({
  model: "claude-sonnet-5",
  messages: [{ role: "user", content: "Write a haiku" }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

Token counts while streaming

A streamed response does not carry usage by default. Ask for it and a final chunk arrives with the totals, after the content is finished.

JSON
{
  "stream": true,
  "stream_options": { "include_usage": true }
}