November 4, 2026•5 min read

Your Stream Dies at 125 Seconds and Nothing in Your Code Says 125

Streaming moves the wait into a connection that outlives the request. Four layers sit between the model and the browser, their idle timeouts disagree, and the smallest one ends your stream.

The text stops mid-sentence. The client shows a network error, the handler logs nothing, and the endpoint that streamed fine on localhost dies in production after a suspiciously round interval. Nothing in your code threw. An idle timeout in a layer you did not configure closed the connection.

Streaming makes the wait feel shorter, but the wait did not disappear. It moved into a connection that must stay open while the work finishes. Every layer between your model and your browser decides how long that connection may stay quiet.

Key takeaways

  • Streaming removes the wait before the first token, not the work. The connection stays open from the first token to the last.
  • Four layers sit between the model and the browser and their idle timeouts disagree: nginx reads 60 seconds, Cloudflare 125, Node 300, and Go has none by default.
  • nginx times out between two successive reads, not over the whole response, so the longest silence in your stream kills it, not the total duration.
  • SSE gives you reconnection and a Last-Event-ID header. A WebSocket adds a channel back to the server and loses both.
  • When a run takes minutes, persist the events first and stream them second, because a reconnect has nothing to replay if the events only existed inside one response.

The only number streaming moves

Time to first token is the time between sending a request and receiving the first token. For reasoning models, that is the first reasoning token, not the first visible word (Artificial Analysis). Streaming improves that number, but the total time does not change. Between the first token and the last, the stream is mostly silence, and the model decides how often it breaks.

If your provider sends events every 40 milliseconds, an idle timeout of 60 seconds is irrelevant. If your agent loop spends 90 seconds inside a tool call before the next token, that same timeout ends the run.

Four layers, six timeouts, no two agree

These are the defaults between a model call and a browser, read from primary documentation on 2026-11-03.

Layer Setting Default What it does
nginx proxy_read_timeout 60s Closes the upstream connection if nothing arrives between two reads
nginx proxy_buffering on Holds the response in buffers, so early events can arrive at the client late or in a burst
Cloudflare Proxy Read Timeout 125s Returns 524 if the origin sends no response in that window
Cloudflare Proxy Write Timeout 30s Returns 524 if the write to the origin does not finish in time
Node server.requestTimeout 300s Answers 408 if the whole request is not received in time
Node server.timeout 0 No socket inactivity timeout at all
Go Server.WriteTimeout 0 No write timeout at all

Sources: the nginx proxy module documentation, Cloudflare's connection limits reference, the Node HTTP documentation, and the Go net/http documentation.

Whichever clock in the table fires first ends your stream, and it is rarely the one you configured.

In nginx, proxy_read_timeout is set "only between two successive read operations, not for the transmission of the whole response" (nginx docs). A stream that runs for three minutes and emits a token every five seconds outlives a 60 second timeout indefinitely. A stream that runs for 90 seconds and thinks for 61 of them does not. The risk is the longest silence, not the duration.

The Cloudflare figure is 125, not the 100 seconds quoted in older write-ups. Only the read timeout is adjustable, and only on Enterprise zones, up to 6,000 seconds (error 524).

This is not a WebSocket problem

A WebSocket is not a more durable connection. It has a different lifecycle, inherits the same timers and load balancer, and still drops when a deploy drains a node.

A WebSocket buys a channel back to the server while the response is open. That matters when the reader has to interrupt, steer, or talk during generation. Token streaming goes one way and does not need it.

Server-sent events are cheaper and give you two things people rebuild badly by hand. An EventSource reconnects on its own after the connection drops (MDN). On that reconnect the browser sends a Last-Event-ID header carrying the id of the last event it received (HTML spec). Number your events and resumption is a lookup.

The reconnect delay is implementation-defined, "probably in the region of a few seconds" in the spec's own words. The server can override it with the retry field. That matters when a proxy kills your stream: a client that waits 3 seconds to ask again, on a four minute run, reconnects every time the proxy idles.

Over HTTP/2 the number of simultaneous streams is negotiated between client and server and defaults to 100 (MDN). Every open SSE stream is one of those, which is fine until one page opens a stream per widget.

When the run takes four minutes, the stream is not the transport

A four minute agent loop is not a slow request. It is a job: it can be retried, it can outlive a browser tab, and it can be cancelled halfway. The response then stops being the record of what happened and becomes a view of a record that has to exist somewhere else. Cloudflare's own 524 page recommends status polling for long running HTTP processes.

The design that survives is boring. Accept the job, return an id, append events to a log with a sequence number, and let the stream read from that log. A reconnect with Last-Event-ID becomes a request for "everything after 412" instead of a rerun of the work. The client can close the tab because the run never depended on it.

Cancelling is a decision on the server

Closing the socket is a client action. It says nothing to the model call that keeps generating tokens into a buffer nobody will read. Unless the abort is propagated, the stop button stops the pixels, not the bill.

In a streaming handler the fix is a few lines: listen for the close event and cancel the request to the provider with the same signal. In a job-shaped design it is a message to the worker, plus a rule for what a cancelled job leaves behind. To test it, run the probe below with an abort time and compare the token usage your provider reports against a run you let finish.

Measure your own number

The table lists other people's defaults. The number that matters is the longest silence your stream can produce, which is a property of your loop, not of nginx.

Save this as sse-probe.mjs and point it at your endpoint. It reports time to first token, total time, the largest gap between two chunks, and how many event boundaries arrived.

JS
// usage: node sse-probe.mjs <url> [abortAfterMs]
const url = process.argv[2];
const abortAfter = Number(process.argv[3] || 0);
const start = performance.now();
const ac = new AbortController();
if (abortAfter) setTimeout(() => ac.abort(), abortAfter);

let first = null, last = null, gap = 0, events = 0, bytes = 0;
try {
  const res = await fetch(url, { signal: ac.signal });
  const reader = res.body.getReader();
  const dec = new TextDecoder();
  let buf = "";
  for (;;) {
    const { value, done } = await reader.read();
    if (done) break;
    const now = performance.now();
    bytes += value.byteLength;
    if (first === null) first = now;
    if (last !== null) gap = Math.max(gap, now - last);
    last = now;
    buf += dec.decode(value, { stream: true });
    events = (buf.match(/\n\n/g) || []).length;
  }
} catch (err) {
  console.log(`aborted at ${Math.round(performance.now() - start)}ms (${err.name})`);
}
console.log({
  ttftMs: first && Math.round(first - start),
  totalMs: Math.round((last || start) - start),
  largestGapMs: Math.round(gap),
  events,
  bytes,
});

Compare largestGapMs against the smallest timeout in your stack, not against totalMs. If the gap loses, the stream dies in production on exactly the runs where the model thinks hardest.

What this does not cover

The numbers are documentation defaults read on 2026-11-03, not measurements from your stack. HTTP/3, gRPC streaming, and provider-side caps on how long a response may stay open are not covered.

If your p95 run duration is longer than the shortest timeout in your stack, the stream is a view over a log, not the transport of truth. Build the log first.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all
LlmWhat a Prompt Cache Actually Remembers

Four providers cache the same bytes under four different rulebooks. The question that decides your bill: when the second request is longer than the first, does it reuse the whole prefix or only the part that was already written?

For developers who learn by struggling

© 2026 Inva.dev.