# 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](https://ably.com/docs/ai-transport/concepts/sessions.md?source=llms.txt). 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:
1. The agent continues streaming tokens to the channel; the stream is not tied to the client's connection.
2. The Ably SDK reconnects automatically.
3. On reconnect, the client transport uses `untilAttach` to load any messages it missed during the gap.
4. 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:
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 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:
### Javascript
```
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:
### Javascript
```
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](https://ably.com/docs/ai-transport/concepts/authentication.md?source=llms.txt).
- 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](https://ably.com/docs/ai-transport/features/history.md?source=llms.txt) 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](https://ably.com/docs/ai-transport/features/token-streaming.md?source=llms.txt): what gets recovered.
- [Multi-device sessions](https://ably.com/docs/ai-transport/features/multi-device.md?source=llms.txt): the same recovery model across devices.
- [History and replay](https://ably.com/docs/ai-transport/features/history.md?source=llms.txt): loading conversation history.
## Related Topics
- [Token streaming](https://ably.com/docs/ai-transport/features/token-streaming.md?source=llms.txt): Stream AI-generated tokens to clients in realtime using AI Transport, with support for message-per-response and message-per-token patterns.
- [Cancellation](https://ably.com/docs/ai-transport/features/cancellation.md?source=llms.txt): Cancel AI responses mid-stream with Ably AI Transport. Scoped cancel signals, server-side authorization, and graceful abort handling.
- [Multi-device sessions](https://ably.com/docs/ai-transport/features/multi-device.md?source=llms.txt): Share AI conversations across tabs, phones, and laptops with Ably AI Transport. All devices see the same session in real time.
- [History and replay](https://ably.com/docs/ai-transport/features/history.md?source=llms.txt): Load conversation history from Ably channels with AI Transport. Paginated history, gapless continuity, and scroll-back patterns.
- [Conversation branching](https://ably.com/docs/ai-transport/features/branching.md?source=llms.txt): Edit user messages, regenerate AI responses, and navigate branches with Ably AI Transport. The full history is preserved as a tree.
- [Interruption](https://ably.com/docs/ai-transport/features/interruption.md?source=llms.txt): Let users interrupt AI agents mid-stream with Ably AI Transport. Cancel-then-send and send-alongside patterns for responsive AI interactions.
- [Concurrent turns](https://ably.com/docs/ai-transport/features/concurrent-turns.md?source=llms.txt): Run multiple AI turns simultaneously with Ably AI Transport. Independent streams, scoped cancellation, and multi-agent support.
- [Tool calling](https://ably.com/docs/ai-transport/features/tool-calling.md?source=llms.txt): Stream tool invocations and results through Ably AI Transport. Server-executed and client-executed tools with persistent state.
- [Human-in-the-loop](https://ably.com/docs/ai-transport/features/human-in-the-loop.md?source=llms.txt): Add human approval gates to AI agent workflows with Ably AI Transport. Approve tool executions and provide input across devices.
- [Optimistic updates](https://ably.com/docs/ai-transport/features/optimistic-updates.md?source=llms.txt): User messages appear instantly in Ably AI Transport. Optimistic insertion with automatic reconciliation when the server confirms.
- [Agent presence](https://ably.com/docs/ai-transport/features/agent-presence.md?source=llms.txt): Show agent status in your AI application with Ably Presence. Display streaming, thinking, idle, and offline states in real time.
- [Push notifications](https://ably.com/docs/ai-transport/features/push-notifications.md?source=llms.txt): Notify users when AI agents complete background tasks with Ably Push Notifications. Reach users even when they're offline.
- [Chain of thought](https://ably.com/docs/ai-transport/features/chain-of-thought.md?source=llms.txt): Stream reasoning and thinking content alongside responses with Ably AI Transport. Display chain-of-thought in real time.
- [Double texting](https://ably.com/docs/ai-transport/features/double-texting.md?source=llms.txt): Handle users sending multiple messages while the AI is streaming with Ably AI Transport. Queue or run messages concurrently.
## Documentation Index
To discover additional Ably documentation:
1. Fetch [llms.txt](https://ably.com/llms.txt?source=llms.txt) for the canonical list of available pages.
2. Identify relevant URLs from that index.
3. Fetch target pages as needed.
Avoid using assumed or outdated documentation paths.