# History and replay Your users see the conversation when they come back to it. AI Transport loads history from the channel itself, with gapless continuity into the live stream. History comes from the Ably channel itself. Every message (user prompts, agent responses, lifecycle events) persists on the channel. Clients load history on connect and paginate backward through the conversation. No separate database is required. A minimal history-loading hook: #### Javascript ``` const { nodes, hasOlder, loadOlder } = useView({ transport, limit: 30 }); ``` ## How it works The client transport loads history using `view.loadOlder()` with the `untilAttach` parameter. `untilAttach` keeps history and the live subscription gapless: every message between the historical window and the latest live message is accounted for. `loadOlder()` returns `Promise`. It expands the view window internally instead of returning a page; the view's `nodes` array updates with the older messages. ### Javascript ``` const { nodes, hasOlder, loadOlder } = useView({ transport, limit: 30 }); if (hasOlder) { await loadOlder(); } ``` ## Implement scroll-back Load more messages when the user scrolls toward the top of the conversation: ### Javascript ``` const { nodes, hasOlder, loading, loadOlder } = useView({ transport, limit: 30 }); function handleScrollToTop() { if (hasOlder && !loading) { loadOlder(); } } ``` `loading` is `true` while a history page is being fetched. Use it to show a spinner at the top of the conversation. When `hasOlder` is `false`, the user has reached the beginning of the conversation. ## History and branching History includes branch information. Messages carry `parent` and `forkOf` headers that indicate which conversation branch they belong to. When history is loaded, the conversation tree reconstructs branches from these headers and places each message on the correct branch. Loading history does not produce a flat list of messages. It rebuilds the full tree structure, including any points where the conversation forked because of edits, regenerations, or explicit branching. ## Edge cases and unhappy paths - Channel history is bounded by your retention policy. A client connecting after retention expires sees only the live stream. Persist completed turns to your own store if you need longer-term retention. - `loadOlder()` while `loading` is already `true` is a no-op. Guard against double-trigger from rapid scroll events. - A late joiner that arrives mid-stream receives the streamed message in its accumulated form, not as a replay of every token. The view renders it correctly through the lifecycle tracker. - A client without `history` capability cannot load anything beyond the live subscription window. Capability scoping is part of [authentication](https://ably.com/docs/ai-transport/concepts/authentication.md?source=llms.txt). - A regenerated branch shows up in history with its `forkOf` header. The view's branch selection determines which sibling renders. See [conversation branching](https://ably.com/docs/ai-transport/features/branching.md?source=llms.txt). ## FAQ ### Do I need a database for chat history? Not for AI Transport's own behaviour. The channel is the source of truth within retention. Add a database for analytics, search, longer retention, or external integrations. ### How do I retain history longer than the channel keeps it? Persist completed turns to your own store as they end. AI Transport supports hydrating a session from an external store; the channel handles live and in-progress activity. See [sessions](https://ably.com/docs/ai-transport/concepts/sessions.md?source=llms.txt#persistence). ### Why does `loadOlder()` return `Promise` instead of the messages? The view expands its internal window and emits an update. Components subscribed to `nodes` re-render with the older messages. This keeps the view as the single source of truth. ### Does history include cancelled or aborted turns? Yes. Aborted messages keep their partial content and a status of `aborted`. The view renders them in place. ### Can I paginate forward as well as backward? `useView` is backward-only because the live subscription handles forward delivery. New messages arrive in real time without an explicit fetch. ## Related features - [Token streaming](https://ably.com/docs/ai-transport/features/token-streaming.md?source=llms.txt): how streamed responses are persisted and replayed from history. - [Reconnection and recovery](https://ably.com/docs/ai-transport/features/reconnection-and-recovery.md?source=llms.txt): how history loading fits into reconnection. - [Conversation branching](https://ably.com/docs/ai-transport/features/branching.md?source=llms.txt): how branches are created and navigated. ## 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. - [Reconnection and recovery](https://ably.com/docs/ai-transport/features/reconnection-and-recovery.md?source=llms.txt): AI Transport streams survive connection drops automatically. Clients reconnect and resume from where they left off with no lost tokens. - [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. - [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.