# Streaming

> Server-sent events for chat.

Chat supports server-sent events: set `stream: true` and the reply arrives as OpenAI-style chunks terminated by `data: [DONE]`. Credits are flat per completion — streaming costs the same as buffering.

```javascript
const res = await fetch('https://eroq.ai/v1/chat/completions', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.EROQ_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ model: 'eroq-rp-plus', stream: true, messages }),
})

const reader = res.body.getReader()
const decoder = new TextDecoder()
let buffer = ''
while (true) {
  const { done, value } = await reader.read()
  if (done) break
  buffer += decoder.decode(value, { stream: true })
  const events = buffer.split('\n\n')
  buffer = events.pop() ?? ''
  for (const event of events) {
    const data = event.replace(/^data: /, '')
    if (data === '[DONE]') continue
    const chunk = JSON.parse(data)
    process.stdout.write(chunk.choices?.[0]?.delta?.content ?? '')
  }
}
```

eroq specifics: a final `chat.completion.usage` event carries the meter before `[DONE]`; if the upstream fails before any content streamed you get an `error` event and an automatic refund.

---
Canonical: https://eroq.ai/docs/streaming · Index for agents: https://eroq.ai/llms.txt · OpenAPI: https://eroq.ai/openapi.json
