Reconnection and recovery
Your users never see a half-streamed response disappear. AI Transport keeps the agent publishing through the session, and the client resumes from the exact point of disconnection.
Streams survive connection drops automatically. When a client disconnects, the agent keeps streaming to the session. When the client reconnects, it resumes from the exact point it left off, with no lost tokens, no broken responses, and no manual retry code.
This is built into the transport layer. Application code does not need to do anything.
How it works
The durable session on the Ably channel persists independently of any single connection. When a client's connection drops:
- The agent continues streaming tokens to the channel; the stream is not tied to the client's connection.
- The Ably SDK reconnects automatically.
- On reconnect, the client transport uses
untilAttachto load any messages it missed during the gap. - The conversation state is restored. The client sees the complete response.
Recovery scenarios
Two recovery paths exist depending on how long the client was disconnected.
When the disconnection is brief, Ably's connection protocol handles it. The client reconnects and the SDK uses untilAttach to load any messages published during the gap. There is no missing window; the response resumes exactly where it left off.
When the client has been offline for longer than the live recovery window, it loads the full conversation from channel history on reconnect. Pagination through history uses view.loadOlder() to reconstruct the rest of the conversation.
Server-side encoder recovery
On the server side, the encoder handles transient failures during streaming. If an append operation fails, for example due to a network blip between the server and Ably, the encoder falls back to a full message update:
- Append the next token to the message (normal path).
- If the append fails, send a full update with the accumulated content (recovery path).
- Continue appending from the recovered state.
This happens inside turn.streamResponse(). The accumulated response is never lost, even when individual append operations fail.
Mid-stream joins
When a client joins a channel while a response is already streaming, the lifecycle tracker delivers the correct sequence of events. Missing lifecycle events (such as the stream start) are synthesised so the client processes the in-progress stream correctly.
A second tab opened during streaming sees the streaming content immediately.
Load history on reconnect
The client transport loads conversation history using Ably's untilAttach parameter:
1
const { nodes, hasOlder, loadOlder } = useView({ transport, limit: 30 });useView loads history on mount. The untilAttach flag prevents a gap between historical messages and live messages; every message is accounted for.
To load older messages beyond the initial window:
1
2
3
4
5
const { nodes, hasOlder, loadOlder } = useView({ transport, limit: 30 });
if (hasOlder) {
await loadOlder();
}Edge cases and unhappy paths
- A client that drops mid-stream and reconnects after the live recovery window does not get every individual token replayed. It receives the accumulated content of the message up to the latest append. The user-visible result is the same.
- A client without channel history capability cannot reconnect after the live recovery window. Capability scoping is part of authentication.
- An agent that crashes mid-stream leaves the partial message with status
aborted. A retry creates a new message, not a continuation of the aborted one. - The encoder fallback to a full message update is invisible to subscribers. If you log channel operations, you see periodic updates between appends.
- A client clock drift does not affect recovery. Reconnection uses the channel's serial, not wall-clock time.
FAQ
What is the live recovery window?
It is the period during which Ably can replay messages without falling back to history. The duration depends on the connection state and the channel configuration. After that window, the SDK switches to history-based recovery transparently.
Does the user see the agent pause when they reconnect?
No. The view emits the accumulated content of the streamed message on reconnect, then continues with any further appends. The user sees the response as continuous.
How long is the response retained after the agent ends the turn?
For the channel history retention period. Configure this through the channel's persistence settings. See history and replay for the recovery patterns.
What if the agent process dies before the stream finishes?
The partial message stays on the channel with status aborted. The session is intact. A new turn restarts the work; AI Transport does not automatically retry the LLM call.
Does the client need special code to handle reconnection?
No. The transport handles reconnection internally. useView exposes hasOlder and loadOlder for explicit history pagination, which is the only application-visible recovery primitive.
Related features
- Token streaming: what gets recovered.
- Multi-device sessions: the same recovery model across devices.
- History and replay: loading conversation history.