A streaming AI chat interface may lose its connection in the middle of a response without losing the server-side job. Treating a reconnect as a new prompt can duplicate work, create two replies, or charge twice for one interaction. Server-Sent Events (SSE) support automatic reconnection and event IDs, but they do not provide exactly-once application processing. The following runbook translates the WHATWG event-stream model into an explicit storage and replay contract for production teams.

Distinguish a transport resume from a new AI job

Separate three identifiers: conversation_id for the ongoing thread, generation_id for the requested model job, and event_id for one emitted chunk or state transition. A browser reconnect concerns the event stream, not authorization to start another generation. The client should retain the existing generation identifier when the network changes and should never silently POST the original prompt again. On the server, record an idempotency key for the initial request and return the existing job if an equivalent authorized retry arrives. An SSE endpoint can then replay already recorded events before continuing live delivery. This design keeps transport retries distinct from costly model execution. It also allows a user to deliberately request a new answer using a different key when the original result is genuinely unsatisfactory.

Interpret event IDs using the actual standard

The WHATWG HTML Living Standard documents the EventSource interface and the text/event-stream format. Its id field sets the client’s last event ID, and reconnects can communicate that value through the Last-Event-ID request header. However, an event ID is meaningful only relative to the application’s event history. Choose monotonically increasing per-generation sequence numbers such as 41, 42, and 43, rather than assuming clock timestamps are unique. Store the payload and sequence atomically before emitting it. If the client reports 41, stream events 42 onward, and include explicit terminal states for completed, failed, and cancelled jobs. Do not infer that event 41 was rendered just because it was sent; UI acknowledgment and transport delivery are different facts.

Design a bounded replay window

Replaying every token ever produced can create unbounded storage and slow reconnects. Give each generation a retention duration and a maximum event count based on product requirements. After expiration, return a structured ‘resume unavailable’ outcome with a link to retrieve the finalized response, not an accidental second generation. A finished answer may be kept as a canonical object separately from per-token stream history. On a mobile handoff, clients that reconnect within the window can deduplicate by generation_id and event_id. For clients returning after a long offline period, a final snapshot is usually safer than thousands of individual token events. Explicitly version the response schema so old clients do not misinterpret a new event type.

Build authorization into every reconnect

An SSE endpoint often lives behind session cookies or another credential. A previously valid event ID does not grant permission to see the message after the user signs out, changes workspace, or loses access to a thread. Recheck the current principal and conversation ACL before reading replay data. Bind the generation to tenant and conversation IDs, and reject guesses at another user’s sequence. Limit concurrent streams per user; browsers can open multiple EventSource connections and an attacker can repeatedly reconnect to amplify load. Log access decisions without placing raw private prompts in application logs. For shared conversation spaces, update the authorization on membership changes, including when a collaborator is removed during a generation.

Exercise failure injections instead of happy-path demos

Use a repeatable matrix: disconnect after the first token, during a UTF-8 boundary, immediately before terminal completion, and after a server deploy; also simulate a duplicate POST, expired replay cursor, and revoked permission. Compare the final concatenated answer with the canonical server artifact. Duplicate event IDs must be dropped by the renderer even if replay and live delivery overlap. Check whether a proxy buffers the stream, whether keepalive comments are delivered, and whether the client recognizes named events. MDN’s SSE guide documents event fields including data, id, event, and retry; the retry value is a reconnect delay, not an instruction to rerun business logic. Include cost and job counts in the assertion suite.

Observe useful reliability metrics and rollout guards

Track disconnects per thousand conversations, successful resume percentage, missing sequence gaps, duplicate model jobs per request key, and the delay from reconnect to first visible resumed chunk. A hypothetical trial with 500 interrupted streams and 470 gap-free resumes yields 94% success, but that is only an example: measure with your own traffic. Add alerts for duplicate executions and unexpectedly large replay reads. Roll out by client version and region, retaining a fallback that retrieves the final response. An EventSource implementation can improve perceived continuity, yet the real reliability boundary is the application’s durable event log plus idempotent job admission, not the stream protocol alone.

Sources

  1. WHATWG — HTML Living Standard, server-sent events (updated 11 November 2026)
  2. MDN — Using server-sent events, living documentation (accessed 12 November 2026)

Editorial analysis. Featured image licensed via Unsplash; relevant rights and usage must be checked for reuse.