A real-time analytics platform makes Claude API calls and needs to display streaming responses in a React frontend. The team implements an event stream consumer that processes SSE events. During testing, they observe that when network interruptions occur mid-stream, the EventSource auto-reconnects but the reconnection URL doesn't include the conversation state, causing Claude to start a fresh generation. Users see the response restart from the beginning. What is the correct implementation pattern?
Select an answer to reveal the explanation.
Short Explanation
Here's the deal — use the SSE Last-Event-ID mechanism. You assign incrementing IDs to each event, the client sends the last received ID back on reconnect, and the server resumes from its buffered position instead of making Claude regenerate.
Full Explanation
The correct implementation pattern is to use the SSE Last-Event-ID header mechanism. The server assigns incrementing IDs to each event chunk, the browser automatically tracks and sends the last received ID in the Last-Event-ID header on reconnection, and the server can resume from a buffer rather than regenerating. This requires server-side buffering of the active stream's chunks, but not the full pre-completion buffering required by buffering the entire Claude response server-side before streaming. Buffering the entire Claude response server-side before streaming eliminates mid-stream reconnection issues only by trading the reconnection problem for eliminated streaming UX, because users wait for complete generation before seeing anything. Disabling auto-reconnect and manually replaying the full Claude API request is the described current behavior: Claude regenerates, which is expensive and inconsistent because the regenerated response differs from the interrupted one. Setting SSE reconnect intervals to 30 seconds reduces reconnection frequency but doesn't fix what happens when a reconnection does occur.