SuperPenguin Docs
SDKsTypeScript

Vercel AI SDK

Track generateText and streamText with SuperPenguin helpers for the Vercel AI SDK.

Use trackGenerateText and trackStreamText when you call the Vercel AI SDK instead of a native provider client.

generateText

import { init, trackGenerateText } from "@superpenguin/js";
import { generateText } from "ai";

init({ apiKey: process.env.SP_API_KEY });

const trackedGenerateText = trackGenerateText(generateText, {
  provider: "vercel_ai_gateway",
  functionId: "support-reply",
  metadata: {
    environment: "production",
    feature: "support_agent",
  },
});

const result = await trackedGenerateText({
  model: "openai/gpt-4o",
  prompt: "Write a concise reply.",
  metadata: { customer_id: "cust_acme_123" },
  telemetry: { functionId: "support-reply" },
});

streamText

import { init, trackStreamText } from "@superpenguin/js";
import { streamText } from "ai";

init({ apiKey: process.env.SP_API_KEY });

const trackedStreamText = trackStreamText(streamText, {
  provider: "vercel_ai_gateway",
  functionId: "chat-stream",
});

const result = trackedStreamText({
  model: "openai/gpt-4o",
  messages,
  metadata: { customer_id: "cust_acme_123", feature: "chat" },
});

The wrapper waits for the Vercel AI SDK usage promise in the background and submits one attribution event when usage is available. For Vercel AI Gateway, SuperPenguin captures billed cost on the call. You do not pass region or deploymentType.

For Vercel AI Gateway, set AI_GATEWAY_API_KEY in the same server environment. telemetry.functionId and related telemetry metadata become custom_tags such as function_id.

Multi-step agents (0.11.0 and later)

Version 0.11.0 emits one event per result step when step details are available. For agents, use the model middleware and dynamic metadata scope to capture each invocation, including models selected during the loop. Use a single capture path per model call to prevent duplicate spend. These APIs are not available in 0.9.1 or 0.10.0.

Content capture (0.16.0 and later)

When your organization has content capture on, sampled calls store the whole model call: the options the call received (messages or prompt, system, tools, settings) and the full result, including streamed text and tool-call inputs of any length (nothing is truncated). Processing runs in the background after the call; in a serverless route handler, await flushContentCapture() before returning.

Set session_id (one conversation) and turn_id (one turn) so samples group into conversations:

  • Text helpers (trackGenerateText, trackStreamText): in the call's metadata option. Ids in the call's telemetry.metadata or experimental_telemetry.metadata are also used for samples.
  • Model middleware (superpenguinMiddleware): the middleware does not see the generateText options, so use withMetadata around the call (or the middleware's metadata defaults).
import { generateText, wrapLanguageModel } from "ai";
import { openai } from "@ai-sdk/openai";
import { flushContentCapture, superpenguinMiddleware, withMetadata } from "@superpenguin/js";

const model = wrapLanguageModel({
  model: openai("gpt-4o"),
  middleware: superpenguinMiddleware({ specificationVersion: "v3" }), // "v4" for AI SDK 7
});

export async function POST(request: Request) {
  const { chatId, messageId, messages } = await request.json();
  const result = await withMetadata({ session_id: chatId, turn_id: messageId }, () =>
    generateText({ model, messages }),
  );
  await flushContentCapture();
  return Response.json({ text: result.text });
}

Use one capture path per call: the middleware, a text helper, a wrapped provider client or trackUsage, never two for the same call.