Streaming responses
Stream chat completion tokens from EUrouter using server-sent events.
Set stream to true to receive tokens as they are generated. The endpoint
returns an SSE stream and ends it with data: [DONE].
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.EUROUTER_API_KEY,
baseURL: "https://api.eurouter.ai/api/v1",
});
const stream = await client.chat.completions.create({
model: "mistral-large-3",
messages: [{ role: "user", content: "Explain EU AI regulation briefly." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}Use --no-buffer so cURL prints each event immediately:
curl --no-buffer https://api.eurouter.ai/api/v1/chat/completions \
-H "Authorization: Bearer $EUROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "mistral-large-3",
"messages": [{"role": "user", "content": "Explain EU AI regulation briefly."}],
"stream": true
}'Production considerations
- Stop reading after the
[DONE]event. - Handle a client disconnect by aborting the upstream request.
- Apply a connection timeout and an inactivity timeout separately.
- Retry only if no response tokens have been emitted. Once a stream starts, replaying the request can duplicate output.