Vercel AI SDK
cetra traces the Vercel AI SDK’s generateText, streamText, generateObject, tool calls and embeddings, with sessions, users and metadata.
Install
Section titled “Install”npm install @cetra-ai/sdk ai @ai-sdk/openaiSet up (AI SDK 4 to 6)
Section titled “Set up (AI SDK 4 to 6)”The AI SDK sends spans to the tracer you give it. aiTelemetry() hands out a tracer from cetra, and adds your session, user, tags and metadata to the telemetry metadata.
import { aiTelemetry, init } from "@cetra-ai/sdk";import { generateText } from "ai";import { openai } from "@ai-sdk/openai";// Pass `telemetry` as `experimental_telemetry` to the Vercel AI SDK; the// tracer it hands out is the cetra one, so AI spans land in cetra.const telemetry = aiTelemetry({ sessionId: "chat-42", functionId: "chat" });telemetry.tracer?.startSpan("ai.chat").end();init({ apiKey: process.env.CETRA_API_KEY });
const result = await generateText({ model: openai("gpt-4o-mini"), prompt: "What is the weather in Paris?", experimental_telemetry: aiTelemetry({ sessionId: "chat-42", functionId: "chat" }),});Set up (AI SDK 7)
Section titled “Set up (AI SDK 7)”AI SDK 7 does not emit spans by itself. The spans come from the @ai-sdk/otel package, passed in telemetry.integrations, and go to the global OpenTelemetry tracer provider. The fixtures for version 7 were captured this way, with a global provider. To use it with cetra, register cetra’s provider as the global one with useGlobalProvider: true.
import { init } from "@cetra-ai/sdk";import { OpenTelemetry } from "@ai-sdk/otel";import { openai } from "@ai-sdk/openai";import { generateText } from "ai";
init({ apiKey: process.env.CETRA_API_KEY, useGlobalProvider: true });
await generateText({ model: openai("gpt-4o-mini"), prompt: "What is the weather in Paris?", telemetry: { isEnabled: true, integrations: new OpenTelemetry() },});What you get
Section titled “What you get”- The outer call (
ai.generateText) is achainspan and each model call under it (doGenerateordoStream) is anllmspan. Tokens and cost are on thellmspans only, so they are never counted twice. - A tool call is a
toolspan with its arguments and result. - Structured output (
generateObject) is stored as JSON. streamTextrecords the time to first token.- Cache read and cache write tokens are recorded for Anthropic models. cetra adjusts the input count where the AI SDK reports it without the cache.
embedandembedManyare traced.- Session ID, user ID and metadata set through the telemetry metadata arrive as the span’s session, user and metadata.
- In version 7 the spans are named
invoke_agent …,step …andchat ….
Limits
Section titled “Limits”- Versions 5, 6 and 7 are covered by captured spans. Version 4 is covered by a hand-written case that checks the older
argsandresultshapes. - Images in the prompt, passed as base64, are extracted as media.
- With
useGlobalProvider, cetra keeps only LLM-related spans from your other OpenTelemetry code. See OpenTelemetry (TypeScript).
- Traces: read the trace as a tree and a timeline.
- Cost and tokens: how cost is computed from the token counts.
- Online rules: score these traces automatically.