Reconnecting to a LangGraph Agent Stream After Page Reload or Network Drop
Problem / Context
A streaming LangGraph agent loses its connection to the frontend on page reload or network drop. The user loses the current response and must restart the conversation, which is especially painful for long-running agent tasks.
Solution
Implement stream reconnection using LangGraph's thread-based checkpointing. Every streaming response is associated with a thread_id stored in the browser (localStorage or URL param). When the page loads, check if an active thread_id exists. If it does, call graph.getState(config) to retrieve the last checkpoint and determine if the agent is still running. If the agent is still in progress, call graph.stream() with the existing thread_id and resume_from_checkpoint=True to re-attach to the live stream. If the agent completed while disconnected, fetch the final state from the checkpoint and render the complete response. Display a reconnecting indicator while the stream re-attaches. Test this by killing the browser tab mid-stream and reopening it; the stream should resume from the last checkpoint rather than restarting. Handle the case where the agent finished during disconnect by showing the completed output immediately.
Result
Agent UI survives page reloads and network drops without losing session state. Stream re-attaches within 2 seconds of page reload. Users no longer lose in-progress responses on disconnect.