# LiveObjects state An agent can react to what the user is doing, such as the page they're on or the record they've selected, the moment it changes. Ably LiveObjects runs on the same channel as the conversation, so agents and clients share live state over the channel they already use. With LiveObjects state, an agent and its clients read and write one shared object on the conversation's [Ably channel](https://ably.com/docs/channels.md). The agent can act on the user's current state as soon as the client writes it, and can report its own status back for every client to see in realtime. The shared object comes from [Ably LiveObjects](https://ably.com/docs/liveobjects.md), through the channel's own `object` API. > **Note** > > The conversation is the stream of messages, what the agent says over time. The shared state is the current value of the things both sides care about, and a client gets it in full when it attaches, rather than by replaying history. Share only what both sides need, and keep the agent's private reasoning on the server. ## How state sync works The transport does not have a LiveObjects API of its own. Get the channel from the client by name with no options, `ably.channels.get(channelName)`, which returns the channel the transport configured, and use its `object` API as you would on any Ably channel. On the agent, get the object once and react to every change: ### Javascript ``` // Agent code: react whenever the user's state changes. const myObject = await ably.channels.get(channelName).object.get(); myObject.subscribe(() => groundNextResponse(myObject.compactJson())); ``` The client works on the same object from the other end. As the user moves around the app, the client writes their current state with `myObject.set(...)`, which fires the agent's subscription. Object state syncs when the channel attaches, so a client that joins late or reloads has the current state straight from the channel. ## Enable LiveObjects LiveObjects is not part of the default channel modes, so it needs an explicit opt-in. Three things must be in place, or object operations throw: - Create the Ably Realtime client with the `LiveObjects` plugin from `ably/liveobjects`. - Request the object channel modes on the transport, by passing `OBJECT_MODES` as the `channelModes` option. - Grant the token the `object-subscribe` and `object-publish` [capabilities](https://ably.com/docs/ai-transport/setup/authentication.md#capabilities). Pass the plugin when you create the client, and the modes when you create the transport: ### OpenAI Javascript ``` // Client code. import * as Ably from 'ably'; import { LiveObjects } from 'ably/liveobjects'; import { createTransport, OBJECT_MODES } from '@ably/ai-transport'; import { openai } from '@ably/ai-transport/openai'; const ably = new Ably.Realtime({ authUrl: '/api/auth/token', plugins: { LiveObjects } }); const transport = createTransport({ client: ably, channelName: 'conversations:42', codec: openai, channelModes: OBJECT_MODES, }); ``` ### Vercel AI SDK Javascript ``` // Client code. import * as Ably from 'ably'; import { LiveObjects } from 'ably/liveobjects'; import { createTransport, OBJECT_MODES } from '@ably/ai-transport'; import { vercel } from '@ably/ai-transport/vercel'; const ably = new Ably.Realtime({ authUrl: '/api/auth/token', plugins: { LiveObjects } }); const transport = createTransport({ client: ably, channelName: 'conversations:42', codec: vercel, channelModes: OBJECT_MODES, }); ``` `OBJECT_MODES` is `['OBJECT_SUBSCRIBE', 'OBJECT_PUBLISH']`. The transport requests these modes on top of the [default channel modes](https://ably.com/docs/channels/options.md#modes), so publishing, subscribing and presence keep working. In React, pass the modes as the `channelModes` prop on [`TransportProvider`](https://ably.com/docs/ai-transport/api/react/transport-provider.md). ## React to state changes on both sides Both sides read and write the same object. The client writes the user's current state as they navigate, and renders whatever the agent reports back: ### Javascript ``` // Client code. const myObject = await ably.channels.get('conversations:42').object.get(); // Publish the user's current view as they navigate. function onNavigate(path) { myObject.set('currentPage', path); } // Render whatever the agent reports back. myObject.subscribe(() => renderAgentStatus(myObject.compactJson()?.agentStatus)); ``` The agent subscribes to the same state and adapts to it. When the user opens a new page, the subscription fires with the new value, and the agent can base its next response on the page they are looking at. It reports its own progress back through the same object: ### OpenAI Javascript ``` // Agent code. import { createTransport, OBJECT_MODES } from '@ably/ai-transport'; import { openai } from '@ably/ai-transport/openai'; const transport = createTransport({ client: ably, channelName, codec: openai, channelModes: OBJECT_MODES }); const myObject = await ably.channels.get(channelName).object.get(); // Adapt to whatever the user is looking at. myObject.subscribe(() => { const { currentPage } = myObject.compactJson() ?? {}; // Base the next response on the page the user moved to. }); // Report progress back, so every subscribed client sees it. await myObject.set('agentStatus', 'searching flights'); ``` ### Vercel AI SDK Javascript ``` // Agent code. import { createTransport, OBJECT_MODES } from '@ably/ai-transport'; import { vercel } from '@ably/ai-transport/vercel'; const transport = createTransport({ client: ably, channelName, codec: vercel, channelModes: OBJECT_MODES }); const myObject = await ably.channels.get(channelName).object.get(); // Adapt to whatever the user is looking at. myObject.subscribe(() => { const { currentPage } = myObject.compactJson() ?? {}; // Base the next response on the page the user moved to. }); // Report progress back, so every subscribed client sees it. await myObject.set('agentStatus', 'searching flights'); ``` Every write reaches all subscribed clients over the same channel. Concurrent writes are safe when the operation commutes: two clients calling `LiveCounter.increment` both count, but a `LiveMap.set` on the same key is last-write-wins. Partition writes by key, so two writers do not race on one `set`. ## Edge cases and unhappy paths - A client created without the `LiveObjects` plugin throws when you use the channel's `object` API. Create the client with `plugins: { LiveObjects }`. - A transport created without `channelModes: OBJECT_MODES` attaches without the object modes, and object operations fail. Pass the modes when you create the transport. - Two transports that share a client and a channel name share the channel, so they must ask for the same modes. - A token missing the `object-subscribe` or `object-publish` capability fails at the operation, rather than when you create the transport. Capability scoping is part of [authentication](https://ably.com/docs/ai-transport/setup/authentication.md#capabilities). - A `LiveMap.set` on a key two clients write at once is last-write-wins, so one write is lost. Partition writes by key, or use a `LiveCounter` where the values need to merge. - `TransportProvider` creates a new transport when its `channelModes` change. It compares the modes by their contents, so an inline array that does not change does not recreate the transport. - `ably/react` exports no LiveObjects hooks, so subscribe in an effect rather than looking for a `useLiveMap` equivalent. ## FAQ ### What belongs in shared state, and what belongs in the conversation? Put the state both sides act on now in the shared object: the page the user is on, the record they have selected, a form in progress, a counter. Leave the conversation itself in the messages, and keep the agent's private reasoning on the server. ### How does the agent react as soon as the state changes? `object.get()` gives you the object, and `subscribe` fires on every change, nested ones included. In the callback, the agent reads the latest value with `compactJson()` and works from that. ### Why do object operations throw? One of the three [requirements](#enable) is missing: the `LiveObjects` plugin on the client, `channelModes: OBJECT_MODES` on the transport, or the `object-subscribe` and `object-publish` capabilities on the token. ### Can both the agent and the client write to the same object? Yes. Both use the same `LiveMap` and `LiveCounter` API on the channel's object. Concurrent writes merge when the operation commutes, as with `LiveCounter.increment`. A `LiveMap.set` on the same key is last-write-wins. Partition writes by key to avoid races. ## Related features - [LiveObjects](https://ably.com/docs/liveobjects.md): the Ably LiveObjects API, including `LiveMap` and `LiveCounter`. - [Agent presence](https://ably.com/docs/ai-transport/channel/agent-presence.md): Ably Presence on the same channel. ## Related Topics - [Agent presence](https://ably.com/docs/ai-transport/channel/agent-presence.md): Show agent status in your AI application with Ably Presence. Display streaming, thinking, idle, and offline states in realtime. - [Push notifications](https://ably.com/docs/ai-transport/channel/push-notifications.md): Notify users when AI agents complete background tasks with Ably Push Notifications. Reach users even when they're offline. ## Documentation Index To discover additional Ably documentation: 1. Fetch [llms.txt](https://ably.com/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.