Topic 4 of 4 · 5 min read
Proxy buffering and dropped-stream pitfalls
Goal: Diagnose why a stream arrives all at once or stops mid-answer and fix the proxy setting or error handling responsible.
Most streaming bugs are not model bugs: a proxy holds your bytes, an idle timeout cuts the connection, or an error arrives after the status line has already said 200. Each has a different fix, so first work out which one you are looking at.
Check yourself · 1 of 3
A streamed chat works on localhost but in production the whole answer appears at once. What is the most likely cause?
Why it matters
A stream that works on your laptop often fails in production behind a load balancer, a CDN or a reverse proxy. The symptoms are confusing: the whole answer appears at once, the answer stops mid-sentence, or the UI shows a normal reply that is cut short.
The three common causes
- Buffering. Reverse proxies such as NGINX collect the upstream response and send it in larger pieces. For NGINX, set
proxy_buffering offfor the route, or have your app send the response headerX-Accel-Buffering: no. Compression can also hold bytes back, so turn off gzip fortext/event-streamroutes or flush after each event. Check every layer: app server, NGINX, CDN, corporate proxy. - Idle timeouts. Proxies and load balancers close a connection that has been quiet for a set time. Models can pause for long periods, for instance while thinking or running a tool. Send a comment line such as
: keep-aliveevery 15 to 30 seconds, and raise the proxy read timeout on this route. - Errors after a 200. The status code is sent before the first token. If the provider is overloaded or something fails mid-answer, you can no longer change the status. Claude sends an
errorevent inside the stream, and a dropped connection has no event at all. Your client must treat "no final event" as a failure.
How to diagnose
Run curl -N against each layer, from the app directly, then through the proxy. -N turns off curl's own buffering. The first layer where output arrives in one lump is the culprit. Log the last event type you saw per request.
Before and after
Weak: the UI marks a message complete when the connection closes.
await readStream(res.body, onDelta);
setStatus("done");
Better: only a real final event completes it.
let finished = false;
await readStream(res.body, (ev) => {
if (ev.type === "error") throw new StreamError(ev.error.type);
if (ev.type === "message_stop") finished = true;
else onDelta(ev);
});
setStatus(finished ? "done" : "interrupted");
What goes wrong
- Fixing the wrong layer. Turning off buffering in the app while the CDN still buffers changes nothing.
- Retrying blindly. A retry after a mid-stream failure starts a new answer, so the user sees text twice unless you reset the buffer.
- Silent truncation. Without an explicit finished flag, a cut-off answer looks complete.
- Keep-alives that break parsing. A client that tries to parse comment lines as JSON fails. Ignore lines that start with a colon.
Examples
Finding the buffering layer
curl -N https://app.example.com/chat/stream -d @req.json
curl -N http://127.0.0.1:3000/chat/stream -d @req.json
The direct call prints tokens one by one. The call through the public host prints everything after eight seconds. The buffering is between the two: the reverse proxy or CDN.
An error after the 200
event: error
data: {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}
The HTTP status was already 200. The client must read this event, show a specific message such as "The model is busy, try again", and keep the partial text clearly marked as incomplete.
Key points
- If a stream arrives in one lump in production only, a proxy, CDN or compression layer is buffering it.
- For NGINX, turn off proxy_buffering for the route or send the X-Accel-Buffering: no response header.
- Idle connections get closed, so send comment keep-alives and raise read timeouts on streaming routes.
- The status code is sent before the first token, so later failures arrive as error events or a dropped connection.
- Only a real final event, such as message_stop, should mark a message complete.
Common mistakes
- Disabling buffering in the app while a CDN or load balancer still buffers.
- Marking an answer complete when the connection closes, with no final event seen.
- Retrying after a mid-stream failure without clearing the text already shown.
- Letting the client parse keep-alive comment lines as JSON.
Check yourself · 2 of 3
What does an NGINX-fronted app send to ask NGINX not to buffer one response?
Check yourself · 3 of 3
A client shows 'Done' as soon as the stream connection closes. A user reports answers that stop mid-sentence marked Done. What is the right fix?
Practice items, not counted towards your mastery.
Start here
- Streaming messages (Claude) (opens in a new tab)DocsFree
The full event sequence, delta types, partial tool JSON and mid-stream error behaviour of a Claude stream.
- Streaming API responses (OpenAI) (opens in a new tab)DocsFree
How another vendor names and orders its stream events, useful for comparing event shapes.
- Using server-sent events (MDN) (opens in a new tab)DocsFree
The wire format: data lines, blank-line separators, event names and the text/event-stream type.
More for this topic
- NGINX proxy module: proxy_buffering (opens in a new tab)DocsFree
Documents proxy_buffering and the X-Accel-Buffering response header that switches buffering off.
- Errors (Claude API) (opens in a new tab)DocsFree
Error types and the note that errors can arrive as events after a 200 status in a stream.
- Server-sent events (HTML Standard) (opens in a new tab)StandardFree
The normative rules for parsing the event stream, including comment lines used as keep-alives.