Skip to main content
S2 integrates with the Vercel AI SDK in two ways:
  • @s2-dev/resumable-stream — drop-in stream resumption for AI chatbots built with the Chat SDK.
  • TypeScript SDK examples — reference implementations showing agent sessions, chat persistence, and multi-agent patterns using the S2 TypeScript SDK directly.

Chat SDK

S2 can be used as a backend for stream resumption in AI chatbot applications with the @s2-dev/resumable-stream npm package. This is inspired by Vercel’s take on stream resumption.

Prerequisites

npm install @s2-dev/resumable-stream
To use this package, you need to create an S2 access token and a basin to store all your streams.
  • Sign up here, generate an access token and set it as S2_ACCESS_TOKEN in your env.
  • Create a new basin from the Basins tab with the Create Stream on Append option enabled, and set it as S2_BASIN in your env.
The incoming stream is batched and the batch size can be changed by setting S2_BATCH_SIZE.

Integrating with the Chat SDK

Import the Package

route.ts
// Update imports
import {
  createResumableStreamContext,
  ResumableStreamContext,
} from '@s2-dev/resumable-stream';

Stream Creation

route.ts
export async function POST(request: Request) {
  let requestBody: PostRequestBody;

  try {
    // ... Usage for this method remains the same as in the original template

    const streamContext = getStreamContext();

    if (streamContext) {
      return new Response(
        await streamContext.resumableStream(streamId, stream),
      );
    } else {
      return new Response(stream);
    }
  } catch (error) {
    if (error instanceof ChatSDKError) {
      return error.toResponse();
    }
  }
}

Stream Resumption

route.ts
export async function GET(request: Request) {
  const streamContext = getStreamContext();
  // ...

  const emptyDataStream = createDataStream({
    execute: () => {},
  });

  const stream = await streamContext.resumeStream(recentStreamId);
  // ...
  return new Response(stream, { status: 200 });
}
Complete usage example can be found here.

Examples

The S2 TypeScript SDK includes examples that pair S2 with the Vercel AI SDK for common agent and chat patterns.

Agent Session

A stream-per-run pattern that creates an ordered audit trail of everything an agent does — tool calls, LLM responses, and state changes — all on a single S2 stream. Source

Chat Persistence

Multi-turn conversation persistence backed by S2. Each chat gets its own stream, giving you a durable, replayable message history. Source

Dinner Party (Multi-Agent)

Multi-agent coordination where agents communicate over a shared S2 stream (the “bus”) while maintaining per-agent memory streams. Demonstrates how S2’s ordered log naturally solves message ordering in multi-agent systems. Source