Streaming
Receive tokens incrementally with server-sent events.
Set stream: true to receive server-sent events (SSE) as tokens are generated — the standard approach for chat interfaces.
Request
curl -N https://www.tokenmoo.com/v1/chat/completions \
-H "Authorization: Bearer sk-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"stream": true,
"messages": [{ "role": "user", "content": "Count to five" }]
}'Event format
Each event is a data: line containing a chunk with a delta:
data: {"choices":[{"delta":{"content":"1"}}]}
data: {"choices":[{"delta":{"content":", 2"}}]}
data: [DONE]The stream ends with data: [DONE]. Accumulate choices[0].delta.content until then.
Handling it in code
const response = await fetch('https://www.tokenmoo.com/v1/chat/completions', {
method: 'POST',
headers: {
Authorization: 'Bearer sk-xxx',
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-4o-mini',
stream: true,
messages: [{ role: 'user', content: 'Count to five' }],
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
process.stdout.write(decoder.decode(value));
}If your reverse proxy buffers responses, the stream arrives all at once instead of incrementally. Disable buffering (for Nginx: proxy_buffering off;) for this endpoint.
Usage accounting
Streaming responses include usage in the final chunks (or after [DONE], depending on the upstream model), so your logs still show token counts.