Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Generated Response Streaming and Cancel State

Last updated: 4 Oct 20268 min read
tutorial
IntermediateBy AITrove Editorial

Streaming can improve perceived latency, but a partial answer is not a completed answer. A transport chunk may split a character, a line, or an application event. The server must frame events; the browser must decode bytes incrementally and preserve unfinished frames until more bytes arrive. The UI needs separate states for connecting, receiving, completed, canceled, and failed. Any partial text shown before completion should carry an incomplete label. A cancellation request may race with the last output event, so the server operation record decides the final state rather than the browser assuming that closing a connection undoes work.

Working case

The pump station summary begins arriving while the reviewer reads the case. The browser displays three paragraphs, then the reviewer switches to a different case. A stale stream continues to arrive and could append pump station 29 text to the new case page. The client binds each stream to a case revision and operation ID; when the route changes, it aborts the read and ignores events for the old identity. The server stops work when it can, records whether completion already happened, and bills or reports consumed work accurately. The partial answer remains labeled incomplete if the reviewer returns to its operation page.

Implementation boundary

javascript
function acceptGeneratedEvent(view, event) {
  if (event.operationId !== view.operationId || event.sequence <= view.sequence) return false;
  view.sequence = event.sequence;
  if (event.type === 'complete') view.complete = true;
  return true;
}
console.log(acceptGeneratedEvent({ operationId: 'sum-47', sequence: 29, complete: false }, { operationId: 'sum-63', sequence: 30, type: 'text' }));
// Output: false

Choose a frame format with explicit event type, operation ID, monotonically increasing sequence, and a terminal marker. Decode with a streaming text decoder. Reject oversized frames and cap accumulated displayed text. Treat transport close without a terminal event as interrupted. Put output into text nodes or another safe rendering path instead of assigning arbitrary generated markup. When cancellation is requested, send an abort signal to both the browser fetch and server-side provider call where supported, but keep the operation status authoritative. Protect reconnect from replaying already rendered sequences twice. If resume is unsupported, start a new operation explicitly instead of splicing two outputs together. An accessible status message should announce completion or failure without speaking every arriving token.

Cost and boundaries

Parsing B streamed bytes is O(B); a bounded frame buffer uses O(F) memory for maximum frame size F, while retained answer text uses O(A) for accepted answer size A. Rendering each tiny fragment can create avoidable layout work, so batch DOM updates at a sensible interval. An open connection consumes server and proxy resources even when few bytes move. Timeouts, idle detection, and concurrent-stream caps should reflect that cost. Stopping a client read does not guarantee provider billing stops instantly. Measure first-byte time, terminal-event rate, canceled work, orphaned operations, client buffer size, and user-visible incomplete results.

Failure trace

Split a multibyte character and an event delimiter across network chunks. The browser must reconstruct both without replacement characters or duplicate text. Close the connection before the terminal event and label the result interrupted. Deliver the final event just as cancel is pressed; resolve the visible state from the persisted operation. Switch to another case while bytes still arrive and prove the old stream cannot modify the new page. Send one frame larger than the cap and stop reading safely. Pause the client reader and verify the server does not accumulate an unlimited output queue. Test keyboard and screen-reader behavior while tokens arrive quickly.

Verification

  • Split frames and UTF-8 bytes decode correctly.
  • Connection loss cannot look like success.
  • Old operation events cannot update a new route.

Practice drill

Stream a 6 KiB summary for case 47 in frames numbered 1 through 83. Cut frame 29 into three network chunks, then cancel at frame 47. Record whether the server acknowledged cancellation before the terminal event. Reopen the operation with the same ID and show either a completed answer or a clearly partial canceled state. Switch to case 63 during a second run and inspect the DOM for mixed case text. Measure peak buffer bytes and update frequency.

Decision note

A stream is a transport for progress; the terminal event and persisted operation state decide whether an answer is complete.

Common Mistakes

  • Treating transport close as a completion marker.
  • Rendering arbitrary model output as HTML.
  • Assuming browser abort reverses a completed provider operation.

Related lessons

Model-Backed Web Application Boundaries; Model Gateway Identity and Request Budgets; Retrieved Content, Instructions, and Tool Permission; Model Output Evaluation and Release Control; Incremental Response Framing and UTF-8; Stream Cancellation and Partial-Result Contract.

Connected practice

Build Project: permission-bound maintenance summary and review Web Development: model-backed application decisions quiz.

web-tech
web-development
Storage details