docs/Models & Routing

Token Streaming (Server-Sent Events)

Low-latency real-time token streaming with line-buffering and keep-alive support.


Streaming Protocol Specification

When you pass `"stream": true`, the gateway responds with HTTP header `Content-Type: text/event-stream`. Each chunk begins with `data: ` followed by a JSON object containing the partial delta. When the generation finishes, the stream emits `data: [DONE]`. Keep-alive comment lines starting with `:` (such as `: ping`) are emitted during long reasoning operations to prevent intermediate proxy timeouts. Spec-compliant parsers ignore lines starting with `:`.
data: {"id":"gen_101","choices":[{"delta":{"content":"Distributed"}}]}

data: {"id":"gen_101","choices":[{"delta":{"content":" consensus"}}]}

: ping

data: {"id":"gen_101","choices":[{"delta":{"content":" ensures agreement."}}]}

data: [DONE]

Spec-Compliant TypeScript Stream Decoder

Here is how to decode the stream using native Fetch with proper line buffering:
const response = await fetch("https://unlockedai.net/v1/chat/completions", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${process.env.UNLOCKED_API_KEY}`,
  },
  body: JSON.stringify({
    model: "claude-sonnet-5.5",
    stream: true,
    messages: [{ role: "user", content: "Write a quick Rust snippet." }],
  }),
});

if (!response.ok) {
  const err = await response.json();
  throw new Error(err.error?.message || "Stream error");
}

const reader = response.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 lines = buffer.split("\n");
  buffer = lines.pop() || ""; // keep incomplete tail

  for (const line of lines) {
    const trimmed = line.trim();
    if (!trimmed || trimmed.startsWith(":")) continue; // ignore comments / keep-alive
    if (trimmed === "data: [DONE]") return;

    if (trimmed.startsWith("data: ")) {
      const json = JSON.parse(trimmed.slice(6));
      const delta = json.choices[0]?.delta?.content || "";
      process.stdout.write(delta);
    }
  }
}

Need an Unlocked AI Key or Credits?

Generate secret API keys backed by non-expiring pay-as-you-go credits starting at $5. No monthly minimums.