Vercel AI SDK

Vercel AI SDK Core orchestrates LLM calls on the server. AI Transport encodes the resulting stream onto an Ably channel, so the same server code reaches every connected client instead of a single HTTP response.

Vercel AI SDK Core is Vercel's library for orchestrating LLM calls. streamText is the main entry point: pass a model, system prompt, and conversation history, and get back a stream of events. The default deployment writes that stream to an HTTP response over Server-Sent Events. AI Transport writes it to an Ably channel instead, so the channel gives you reconnects, multi-device delivery, and replay from your database.

Install the packages:

npm install @ably/ai-transport @ably/pubsub-server @ably/pubsub-device ai@^7

What the Vercel AI SDK brings

FeatureDescription
Provider systemAbstracts model providers (Anthropic, OpenAI, Google) behind a unified interface. Switching models is a one-line change.
streamTextCalls the model and returns a result object whose stream carries text deltas, tool calls, tool results, reasoning content, and lifecycle events.
UIMessage modelMessages with role and parts (text, reasoning, tool calls, tool results, files, sources). Each part tracks its own streaming state.
Tool callingModels emit calls to tools you define with a schema and an execute function. The SDK runs the tool and feeds the result back to the model.
toUIMessageStreamConverts the model's event stream into a stream of UIMessageChunk events.
readUIMessageStreamMerges a stream of UIMessageChunk events into each fuller state of a UIMessage.

AI Transport composes with each of these rather than replacing them. It carries the chunk stream over a channel instead of a connection.

What AI Transport adds

Everything below comes from AI Transport rather than the framework.

FeatureDescription
Streams that outlive a connectionThe response flows through a channel rather than one HTTP response, so a client can reconnect and receive what it missed.
Multi-device deliveryEvery device subscribed to the conversation receives the same messages in realtime.
History and replayA client that loads the conversation from your database replays the channel from the stored serial, then receives live messages.
Token compactionA client that joins mid-response receives the response's text so far in one delivery, rather than a replay of every token.
Requests matched to responsesHeaders on send() and pipe() tie each response to the prompt it answers.

Where they connect

On the server, AI Transport replaces createUIMessageStreamResponse() with pipe(). The agent should publish the prompt with send, so that every subscribed client receives the prompt as well as the response. The response flows to clients through the channel instead of the HTTP body, and the agent's HTTP endpoint returns once the pipe resolves:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

// Agent code.
// Before: the default HTTP transport.
return createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream }) });

// After: the Ably transport.
const adaptor = vercelAdaptor(channel);
await adaptor.send({ type: 'user-message', message }, { headers: { requestId: message.id } });
const { serial } = await adaptor.pipe(toUIMessageStream({ stream: result.stream }), {
  signal: req.signal,
  headers: { requestId: message.id },
});
return new Response(null, { status: 204 });

On the client, each subscribed client merges the chunks of a response with readUIMessageStream, from the response's start chunk to its finish chunk:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

// Client code.
import { readUIMessageStream } from 'ai';
import { vercelDecoder } from '@ably/ai-transport/vercel';

let response;
const decoder = vercelDecoder();
decoder.onEvent(({ event }) => {
  if (event === undefined || event.type === 'user-message') return;
  if (event.type === 'start') {
    const stream = new ReadableStream({ start: (controller) => (response = controller) });
    (async () => {
      for await (const message of readUIMessageStream({ stream })) render(message);
    })();
  }
  response?.enqueue(event);
  if (event.type === 'finish') response?.close();
});
await channel.subscribe(decoder.receive);

The integration has three pieces:

  1. The Vercel codec, which vercelAdaptor and vercelDecoder from @ably/ai-transport/vercel share, encodes the AI SDK's UIMessageChunk events as Ably messages, and a user-message event for the prompt the agent publishes. Each text, reasoning and tool-input stream appends to one message. The codec encodes on the agent and decodes on every client.
  2. vercelAdaptor wraps the conversation's channel on the agent, and vercelDecoder decodes its messages on the client.
  3. pipe() reads the UIMessageChunk stream, encodes each chunk, and publishes the resulting messages. Passing the request's signal cancels the pipe when the client closes the request.

Pass generateMessageId to toUIMessageStream, so each response has its own message id on its start chunk. Without it, the client merges every response into the same message. The quickstart shows the agent's full HTTP endpoint and client.

If your UI uses useChat, the chat transport passes each response's chunks to useChat in place of the client code above. The agent should then pass the abort signal to streamText only, so toUIMessageStream writes the abort chunk that ends the response.

Scope and trade-offs

The Vercel AI SDK focuses on model orchestration and the message model. AI Transport adds continuity across reconnects, multi-device fan-out and stream resumability. You call streamText with the same model, messages and tool definitions. The stream that went to an HTTP response now goes to an Ably channel.