A user closes the tab halfway through a streamed response. What happens on the server, and what should happen?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
By default, more than you would like: unless cancellation is propagated all the way from the HTTP connection down to the engine scheduler, the model keeps generating tokens nobody will ever read, holding a KV slot and burning GPU time for the full max_tokens. On a self-hosted server that is wasted capacity and worse P99 for everyone else. Against a provider API, you are typically still billed for tokens generated before the abort actually lands, so an undetected disconnect is a silent cost line.
Full worked answer with diagram
By default, more than you would like: unless cancellation is propagated all the way from the HTTP connection down to the engine scheduler, the model keeps generating tokens nobody will ever read, holding a KV slot and burning GPU time for the full max_tokens. On a self-hosted server that is wasted capacity and worse P99 for everyone else. Against a provider API, you are typically still billed for tokens generated before the abort actually lands, so an undetected disconnect is a silent cost line.
Mechanically: a client disconnect on an SSE stream shows up as a write failure or a disconnect event on the connection. The web framework has to notice it, close the generator, and the engine has to abort the request. Two things commonly break this chain. First, proxies: nginx, load balancers and CDNs buffer responses, so the server may not see the disconnect promptly, or at all, until it tries to flush. Disable proxy buffering for streaming routes and send periodic heartbeats so the socket is exercised. Second, client SDKs: you have to actually cancel the request or context, not just stop reading.
There is also a correctness angle. If the generation has side effects (tool calls, writes), being killed mid-flight leaves partial state, so steps need to be idempotent and checkpointed.
The better design for anything long-running: do not tie the generation's lifetime to the browser connection at all. Generate server-side into a durable stream that the client subscribes to and can resume after a reconnect (a stream with a resume token). Then a refresh does not lose the answer, and cancellation happens because the user pressed stop, not because a socket dropped.
What to measure: aborted-request rate, and tokens generated after client disconnect.
Worth sketching. The value is in showing the whole chain, because every hop is somewhere the cancellation can silently fail to land.
sequenceDiagram
participant U as Browser
participant P as Proxy
participant S as App server
participant E as Engine
U->>P: tab closed, socket drops
P->>S: disconnect surfaces only on next flush
S->>E: abort the request
E->>E: free the KV slot, stop decoding
E->>E: chain broken means generation to max_tokens, still billed
What a good answer covers
- By default, more than you would like
- On a self-hosted server that is wasted capacity and worse P99 for everyone else
- Against a provider API, you are typically still billed for tokens generated before the abort actually…
- Mechanically: a client disconnect on an SSE stream shows up as a write failure or a disconnect event on…
- The web framework has to notice it, close the generator, and the engine has to abort the request
Interviewer follow-ups to expect
How would you implement a resumable stream?
What is the cost signature in your metrics of disconnects that are never detected?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep