Skip to main content

withOpenBat wrapper

The withOpenBat function wraps your Vercel AI SDK streaming response. It captures user and assistant messages in the background and returns the stream unchanged.
withOpenBat has zero impact on streaming latency. It processes messages after the stream completes and calls recordMessages non-blocking in the background.
app/api/chat/route.ts
import { streamText } from 'ai';
import { withOpenBat } from 'openbat';

export async function POST(req: Request) {
  const { messages, conversationId, user } = await req.json();

  const result = streamText({
    model: yourModel,
    messages
  });

  return withOpenBat(result.toUIMessageStreamResponse(), {
    conversationId,
    user,
    messages
  });
}

How it works

  1. Wraps the Response object returned by toUIMessageStreamResponse()
  2. Extracts user and assistant messages after streaming completes
  3. Calls recordMessages non-blocking in the background
  4. Returns the original response unchanged to the client
You can pass any of the standard SDK options to the second argument, including user, organization, session, and custom metadata.

Server-side usage without Vercel AI SDK

If you are not using the Vercel AI SDK streaming pattern, use the OpenBat client directly to record messages after generating a response.
app/api/chat/route.ts
import { OpenBat } from 'openbat';

const bat = new OpenBat();

export async function POST(req: Request) {
  const { messages, metadata } = await req.json();
  const response = await generateResponse(messages);

  bat.recordMessages({
    conversationId: metadata.conversationId,
    user: metadata.user,
    organization: metadata.organization,
    messages: [
      ...messages,
      { role: "assistant", content: response.content }
    ],
    headers: Object.fromEntries(req.headers)
  });

  return Response.json({ content: response.content });
}
Pass headers from the incoming request to enable automatic geo and device detection. OpenBat reads Cloudflare headers like cf-ipcountry and user-agent to enrich your conversation data.