Streaming LLM API Responses (2026): SSE in Python & JavaScript

How to stream LLM responses token-by-token for snappy UX — server-sent events explained, with copy-paste Python and JavaScript examples and buffering tips.

Streaming LLM API Responses (2026): SSE in Python & JavaScript

Why Stream

Without streaming, users stare at a spinner until the whole answer is done. With streaming, tokens appear as they're generated — the response feels instant even if total time is the same. It also lets you cut a generation off early. This is table stakes for chat UIs and agents.

How It Works

Streaming APIs return server-sent events (SSE): a text/event-stream where each chunk is a small JSON delta, ending with a done signal. You set "stream": true and read the body incrementally instead of awaiting the full JSON.

Python

import os, requests, json

with requests.post(
    "https://api.linkmodel.ai/v1/chat/completions",
    headers={"Authorization": f"Bearer {os.environ['LINKMODEL_API_KEY']}"},
    json={"model": "claude-sonnet-4-6",
          "messages": [{"role": "user", "content": "Write a haiku about latency."}],
          "stream": True},
    stream=True, timeout=120,
) as r:
    for line in r.iter_lines():
        if not line or not line.startswith(b"data: "):
            continue
        chunk = line[6:]
        if chunk == b"[DONE]":
            break
        delta = json.loads(chunk)
        # print the incremental text (shape varies by provider — check docs)
        print(delta, end="", flush=True)

JavaScript (Browser or Node)

const res = await fetch(URL, {
  method: "POST",
  headers: { Authorization: `Bearer ${KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ model: "gpt-5.6-terra", messages: MSGS, stream: true }),
});
const reader = res.body.getReader();
const dec = new TextDecoder();
while (true) {
  const { value, done } = await reader.read();
  if (done) break;
  const text = dec.decode(value);           // parse "data: {...}" lines
  process.stdout.write(text);               // render deltas as they arrive
}

Render deltas straight into your UI for the typewriter effect.

Tips

  • Parse line-by-line — chunks may split mid-line; buffer partial lines.
  • Handle [DONE] and network drops gracefully; add a timeout.
  • Measure time-to-first-token, not just total — that's what users feel. See AI API latency guide.
  • The exact delta shape varies by model — confirm in the docs.
  • Combine with prompt caching to cut first-token latency on repeated prefixes.

Bottom Line

Set stream: true, read the body incrementally, and render deltas as they arrive — the single biggest perceived-speed win for chat and agents. Works the same across models on one key. Start from the Python or Node.js quickstart.

Start free with a $1 credit and stream your first response.

About the author

Claire Lowe

Claire Lowe

AI and API researcher at LinkModel

Claire Lowe is an AI and API researcher at LinkModel, specializing in generative AI models, API pricing, provider comparisons, and multimodal infrastructure. Her work is grounded in official documentation, primary-source pricing data, and hands-on research, with a focus on helping developers and businesses make informed decisions about AI models and API providers.

Related Posts