← Blog

Your SSE stream dies while the model thinks: how to keep it alive

CognitionDesk TeamΒ·Β·5 min read

Does your AI chat return a 504 right on the hardest questions? The model probably didn't fail: it took a while to think, and something along the way got tired of waiting.

1. To a proxy, "nothing yet" means "dead"

A typical streaming server sets its SSE headers with setHeader and waits. But those headers don't go out until the first res.write, and the first write arrives with the first token. If the model takes 90 seconds to start, for 90 seconds your response doesn't exist.

Any guard, load balancer or proxy with a 60 s timeout sees that and cuts the request. The turn was still alive; the silence killed it.

Headers wait for the first tokenNothing sent for 90 sThe proxy cuts at 60 s504 for the userHeaders go out on open200 right awayA ping every 15 sThe turn finishes
The fix isn't a longer timeout: it's making the response exist before the model speaks.

2. Send the headers when the stream opens

export function openSSE(res, { pingMs = 15_000 } = {}) {
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Connection', 'keep-alive');
  res.flushHeaders(); // the 200 goes out now, not with the first token
  // ...
}

flushHeaders() sends the 200 immediately. From then on, any timeout that asks "has it answered yet?" treats the request as served.

Request opensflushHeaders()200 right awayThe model thinksFirst token
With flushHeaders() the response exists from the first millisecond, even before the model says anything.

3. A comment every 15 seconds

  const alive = () => !res.writableEnded && !res.destroyed;
  const timer = setInterval(() => {
    if (alive()) res.write(': ping\n\n');
    else clearInterval(timer);
  }, pingMs);
  res.on('close', () => clearInterval(timer));
  res.on('finish', () => clearInterval(timer));

In SSE, a line starting with : is a comment: the browser ignores it. The user gets nothing, but the connection carries traffic and no intermediary considers it dead.

Turning the ping off matters as much as turning it on: if the client leaves, the interval is cleared instead of writing to a closed socket.

: ping15 s: ping30 s: ping45 sTokens[DONE]ping off
The browser ignores comments; the proxy only sees that the line is still alive.

4. The SDK timeout doesn't measure what you think

Faced with a 90 s turn, the natural move is to raise the model SDK's timeout. But in the most common SDKs the timeout is cleared when fetch resolves, and in streaming fetch resolves as soon as the provider's headers arrive:

const timeout = setTimeout(() => controller.abort(), ms);
return this.fetch(url, fetchOptions).finally(() => clearTimeout(timeout));

It's a connection cap, not a whole-turn cap. What's worth checking in streaming is retries: if your library retries six times by default, the user can spend minutes staring at an empty screen. One or two is enough.

What it coversConnecting to the providerReceiving its headersWhat it doesn'tTime to first tokenThe whole turn
The client timeout kills a connection that never starts; a long but healthy turn is handled by steps 2 and 3.

Test it before a user does: write a test with fake timers and a fake model that takes 70 s to produce its first token. You should get a 200 with text/event-stream, four pings before the first token, a [DONE] at the end and no ping writing afterwards.

Comments and corrections

Build your first AI agent today

RAG over your documents, tools, workflows and a web widget β€” free tier included.

Get started free