Skip to content
GitHubLog inStart free
cetra is not released yet. These docs describe the development version.

Vercel AI SDK

cetra traces the Vercel AI SDK’s generateText, streamText, generateObject, tool calls and embeddings, with sessions, users and metadata.

Terminal window
npm install @cetra-ai/sdk ai @ai-sdk/openai

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" }),
});

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() },
});
  • The outer call (ai.generateText) is a chain span and each model call under it (doGenerate or doStream) is an llm span. Tokens and cost are on the llm spans only, so they are never counted twice.
  • A tool call is a tool span with its arguments and result.
  • Structured output (generateObject) is stored as JSON.
  • streamText records 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.
  • embed and embedMany are 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 … and chat ….
  • Versions 5, 6 and 7 are covered by captured spans. Version 4 is covered by a hand-written case that checks the older args and result shapes.
  • 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).