Skip to main content

Vercel AI SDK

The @caesura-io/ai-sdk package provides asynchronous, non-blocking recommendation injection for the Vercel AI SDK.

It plugs in as a standard language model middleware. Caesura listens to your agent's dialogue and pushes short, real-time recommendations into the model's context before the next call—without blocking the conversation.

warning

Status: Early development. API is not yet stable.

Install

Install the Caesura adapter alongside the Vercel AI SDK and your preferred provider (e.g., Anthropic):

npm i @caesura-io/ai-sdk ai @ai-sdk/anthropic

Quick Start

Wrap your existing model with caesuraMiddleware. The only changes to your code are the highlighted lines below.

import { wrapLanguageModel, generateText } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';
// 1. Import the middleware
import { caesuraMiddleware } from '@caesura-io/ai-sdk';

// 2. Wrap your model
const model = wrapLanguageModel({
model: anthropic('claude-sonnet-4-6'),
middleware: caesuraMiddleware({
baseUrl: 'https://dev.caesura.io',
// apiKey is auto-read from process.env.CAESURA_API_KEY if omitted
}),
});

const sessionId = 'unique-session-id';
const conversation = [{ role: 'user', content: 'Hello!' }];

// 3. Make your call, passing the conversationId in providerOptions
const result = await generateText({
model,
messages: conversation,
providerOptions: { caesura: { conversationId: sessionId } },
});

Tracking Credit Usage

You can request credit-usage metadata on every analysis call and receive the reported value via the onCreditUsage callback.

import { wrapLanguageModel } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';
import { caesuraMiddleware, createCreditMeter } from '@caesura-io/ai-sdk';

// Initialize a credit meter
const meter = createCreditMeter();

const model = wrapLanguageModel({
model: anthropic('claude-sonnet-4-6'),
middleware: caesuraMiddleware({
baseUrl: 'https://dev.caesura.io',
// Pass the meter's record function to the callback
onCreditUsage: meter.record,
}),
});

// ... run your generation tasks ...

// Query credit metrics later
console.log('Total credits consumed:', meter.total());
console.log('Credits by conversation:', meter.breakdown());
console.log('Retained credit events:', meter.events());
note

In async mode, the onCreditUsage callback fires out-of-band as soon as the asynchronous analyze call completes. This is decoupled from the synchronous generateText response.

Debug Logging

If you want to observe the internal lifecycle events (e.g., when Caesura requests an analysis, when it buffers a recommendation, or when it injects one), you can use the debug logger.

import { caesuraMiddleware, createDebugLogger } from '@caesura-io/ai-sdk';

const logger = createDebugLogger();

const middleware = caesuraMiddleware({
baseUrl: 'https://dev.caesura.io',
onEvent: logger.log,
});