Streaming
Region
Set stream: true. The response is a stream of server-sent events. Each event is a data: line with a chunk; the last is data: [DONE].
Add stream_options: {"include_usage": true} to get one final chunk with usage and an empty choices array.
const stream = await client.chat.completions.create({
model: "deepseek-flash",
messages: [{ role: "user", content: "Hello" }],
stream: true,
stream_options: { include_usage: true },
});
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content;
if (delta) process.stdout.write(delta);
if (chunk.usage) console.log("\n", chunk.usage);
}Events
data: {"id":"req_01J8Z6","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"Hel"}}]}
data: {"id":"req_01J8Z6","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"lo."}}]}
data: {"id":"req_01J8Z6","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"req_01J8Z6","object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":9,"completion_tokens":3,"total_tokens":12}}
data: [DONE]