Skip to Content
NineTwo's
Cookbook

Streaming with Node.js

Runnable SSE streaming example using only Node.js built-ins.

A complete example uses fetch, TextDecoderStream, and no external packages.

Key behaviors demonstrated:

  • Sends the required Idempotency-Key and max_completion_tokens.
  • Uses strict stream_options: { include_usage: true }.
  • Captures X-Request-Id from response headers.
  • Reuses NINETWOS_IDEMPOTENCY_KEY when set, so a retry can recover the same logical request.
  • Detects a completed replay returned as JSON even though the original request used stream=true.
  • Reads the response body incrementally and splits SSE frames on blank lines.
  • Handles ChatCompletionChunk, UsageEvent, and [DONE].
  • Treats finish_reason or [DONE] as terminal markers; a transport EOF is not terminal.
  • Renders only delta.content; delta.reasoning_content is an unstable backend passthrough and is not accumulated by the gateway.

See Chat and streaming for the full source and recovery guidance.