Docs menu
Core API

Streaming

Set stream: true for token-by-token output over server-sent events — exactly like OpenAI, with the cost receipt delivered at the end.

Turn it on

Add "stream": true. Each event is a chat.completion.chunk; the stream closes with data: [DONE].

const stream = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Count to five." }],
  stream: true,
});

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

Usage & cost in the final chunk

The stream ends with a chunk whose choices is empty and which carries the token usage and the full sluice receipt — so you get the cost without a second request. This is the same shape OpenAI returns with stream_options.include_usage.

{
  "id": "chatcmpl-...",
  "object": "chat.completion.chunk",
  "choices": [],
  "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 },
  "sluice": { "provider": "groq", "cost_usd": 0.000012, "charged_usd": 0.000012, "saved_usd": 0.000031, "cached": false, "attempts": 1 }
}

Headers & failover

x-sluice
Sent immediately with the chosen provider and route; the cost isn't known until the stream finishes, so read it from the final chunk.
Failover
Sluice can fail over to another provider up until the first token arrives. After streaming has begun it's committed.
Mid-stream interruptions
If a provider drops mid-stream after tokens were sent, Sluice can't fail over (you already have partial output). It emits an OpenAI-style error event (code: "stream_interrupted"), which the OpenAI SDKs raise as an API error for you to handle.
Streaming works with tool calling too; tool-call deltas arrive in the chunks exactly as OpenAI sends them.