Reconnection and recovery

Open in

AI Transport streams survive connection drops automatically. When a client disconnects, Ably handles reconnection. When it comes back, the client resumes from exactly where it left off - no lost tokens, no broken responses, no manual retry logic.

How it works

The durable session (Ably channel) persists independently of any single connection. When a client's connection drops:

  1. The agent continues streaming tokens to the channel - the stream is not tied to the client's connection.
  2. Ably's client SDK automatically reconnects.
  3. On reconnection, the client transport uses untilAttach to load any messages it missed during the gap.
  4. The conversation state is restored seamlessly - the client sees the complete response.

No application code needed. This is built into the transport layer.

Recovery scenarios

There are two recovery paths depending on how long the client was disconnected:

When the disconnection is brief, Ably's connection protocol handles it automatically. The client reconnects and the SDK uses untilAttach to load any messages published while the client was away. There is no gap - the response resumes exactly where it left off.

When the client has been offline for a longer period (beyond the channel's persistence window for live recovery), it loads the full conversation from channel history on reconnect. The client can paginate through history using view.loadOlder() to reconstruct the complete conversation.

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:

  1. Append the next token to the message (normal path).
  2. If the append fails, send a full update with the accumulated content (recovery path).
  3. Continue appending from the recovered state.

This happens automatically inside turn.streamResponse(). The accumulated response is never lost, even if individual append operations fail.

Mid-stream joins

When a client joins a channel while a response is already streaming, the lifecycle tracker ensures it receives the correct sequence of events. Missing lifecycle events (like the stream start) are synthesized so the client can process the in-progress stream correctly.

This means a client can open a second tab while the agent is mid-response and immediately see the streaming content.

Load history on reconnect

The client transport loads conversation history using Ably's untilAttach parameter:

JavaScript

1

const { nodes, hasOlder, loadOlder } = useView(transport, { limit: 30 })

useView automatically loads history on mount. The untilAttach flag ensures no gap between historical messages and live messages - every message is accounted for.

To load older messages beyond the initial window:

JavaScript

1

2

3

4

5

6

7

const { nodes, hasOlder, loadOlder } = useView(transport, { limit: 30 })

// hasOlder indicates whether there are more messages to load
if (hasOlder) {
  await loadOlder()
  // The view window expands internally - nodes updates with the older messages
}