Feeef Cloud
AI API

Streaming

Consume SSE while preserving request identity and billing status.

Set stream: true on synchronous Chat Completions or Responses requests. Read the response as server-sent events, rather than parsing the entire body as JSON.

const requestKey = crypto.randomUUID()
const stream = await ai.chat.completions.create({
  model: 'MODEL_ID_FROM_CATALOG',
  messages: [{ role: 'user', content: 'Explain streaming in one paragraph.' }],
  max_completion_tokens: 256,
  stream: true,
}, { headers: { 'Idempotency-Key': requestKey } })
for await (const event of stream) {
  process.stdout.write(event.choices[0]?.delta?.content ?? '')
}

The ai client uses the configuration in the quickstart. SSE network chunks do not correspond to whole events; buffer incomplete frames and decode UTF-8 incrementally in a raw HTTP client.

Capture X-AI-Request-Id as soon as response headers arrive. Streaming text is not final billing evidence. Read the task after completion; missing usage or an uncertain outcome may require reconciliation. Cloud may send a billing-pending event rather than invent a usage total.

Closing the browser or disconnecting does not guarantee cancellation of upstream paid work. Reuse the original idempotency key for recovery and inspect the task first. Durable task requests do not stream.