Launch Week 3: Five days of launches

Vercel AI SDK

Use Confident AI for LLM observability and evals for Vercel AI SDK on typescript

Overview

The AI SDK by Vercel is a TypeScript framework for building AI apps against any LLM provider. Confident AI lets you trace and evaluate AI SDK apps in a few lines of code using confident-trace, Confident AI's OpenTelemetry-native tracing SDK.

Your generateText and streamText calls stay exactly as they are — every call shows up in the Observatory as a trace with agent, step, model, and tool spans, so you can see what your app did, how many tokens it cost, and run evals on it.

Auto-Instrument

  1. Install Dependencies

    Run the following command to install the required packages. tsx is only needed if you run TypeScript source directly:

    npm install confident-trace 'ai@>=7.0.93 <8' @ai-sdk/openai@4
    npm install -D tsx
  2. Set Your API Keys

    Get your project API key from Confident AI, then set it along with the provider key used by this example:

    export CONFIDENT_API_KEY="<your-confident-project-key>"
    export OPENAI_API_KEY="<your-openai-key>"
  3. Initialize Tracing

    Call init() once when your app starts, before any AI SDK calls. That's it — you don't need to pass a telemetry option or a tracer to generateText; supported AI SDK calls are instrumented automatically.

    src/index.ts
    import { generateText } from "ai";
    import { openai } from "@ai-sdk/openai";
    import { init } from "confident-trace";
    
    const runtime = init();
    
    try {
      const result = await generateText({
        model: openai("gpt-4.1-mini"),
        prompt: "How to make the best coffee?",
      });
      console.log(result.text);
    } finally {
      await runtime.shutdown();
    }
  4. Run Your Application

    Launch your entry-point file with the confident-trace/register preload so the SDK can hook the ai package as Node loads it. init() handles export, the preload handles instrumentation — you need both:

    # Running TypeScript source directly
    node --import tsx --import confident-trace/register src/index.ts
    
    # Running compiled JavaScript
    node --import confident-trace/register dist/index.js

    To make this your normal startup command, add it to your package.json scripts:

    package.json
    {
      "scripts": {
        "start": "node --import confident-trace/register dist/index.js",
        "dev": "node --import tsx --import confident-trace/register src/index.ts"
      }
    }

    Done ✅. You can view the traces on Confident AI's traces page inside the Observatory.

What Gets Captured

Every span carries the Vercel AI SDK integration label and nests under whatever span is active when the call is made:

  • Agent and step spans — one span for the overall generateText / streamText call, plus one per step in multi-step runs.
  • Model calls — LLM spans with the model name and token usage, so cost shows up automatically.
  • Tool calls — tool spans with the tool name, input, and output.
  • Content — prompt and response text, on by default with no size limit unless you configure one. See masking and content controls.

Set Trace Span Properties

Use a trace context to add properties you know before the call starts. It creates no extra span; the trace started by generateText() inherits the tags, metadata, user ID, and customer ID.

src/index.ts
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { init, traceContext } from "confident-trace";

init();

const result = await traceContext(
  {
    tags: ["support"],
    metadata: { release: "2026-09" },
    userId: "user-42",
    customerId: "customer-7",
  },
  () => generateText({
    model: openai("gpt-4.1-mini"),
    prompt: "Explain OpenTelemetry in one sentence.",
  }),
);

See users and customers to set an optional display name alongside each ID, and trace context for every supported trace property and update behavior.

Instrumenting Multi-Turn

You do not need turn() when one Vercel AI SDK entry-point call is already one conversational turn—the integration creates that turn's trace automatically. Use turn() when you want to define the boundary yourself, such as grouping two sequential Vercel AI SDK calls into one turn. Reuse the same thread ID on later turns to group them into one conversation.

src/index.ts
import { init, turn } from "confident-trace";

init();

const answer = await turn({ name: "support-turn", threadId: "chat-42" }, async () => {
  const context = await generateText({ model: openai("gpt-4.1-mini"), prompt: "Find the relevant account details." });
  return generateText({ model: openai("gpt-4.1-mini"), prompt: `Summarize these details: ${context.text}` });
});

See threads for thread I/O, turn IDs, and user IDs.

Disable Vercel AI SDK Instrumentation

Pass init() a list of integration identifiers to opt in to only those integrations. The identifier for Vercel AI SDK is "vercel-ai" in TypeScript; omit it to disable this integration. An empty list disables all automatic instrumentation:

src/index.ts
import { init } from "confident-trace";
init({ instrumentations: [] });
// Use ["vercel-ai"] to opt in; omit "vercel-ai" to disable it.

This turns off Confident AI's automatic instrumentation; calls made after initialization are not instrumented by this integration.

Next Steps

Now that your AI SDK app is traced, dive deeper into:

Need help instrumenting your application?Connect your model calls and agent workflows to Confident AITalk to an expert

Last updated on

Built byConfident AI