Guide

Streaming

Server-sent events for chat.

view as markdown

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, so streaming costs exactly what buffering costs.

Watch the frames arrive

/v1/chat/completions
→ POST /v1/chat/completions
Press run — this replays a real exchange from the docs' own data. No key, no request, no charge.

Reading the stream

node
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 ?? '')
  }
}

Two eroq-specific details: a final chat.completion.usage event carries the meter before [DONE], and if the upstream fails before any content streamed, you get an error event and an automatic refund.