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
Install Dependencies
Run the following command to install the required packages.
tsxis only needed if you run TypeScript source directly:npm install confident-trace 'ai@>=7.0.93 <8' @ai-sdk/openai@4 npm install -D tsxSet 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>"Initialize Tracing
Call
init()once when your app starts, before any AI SDK calls. That's it — you don't need to pass atelemetryoption or a tracer togenerateText; 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(); }src/index.ts import { streamText } from "ai"; import { openai } from "@ai-sdk/openai"; import { init } from "confident-trace"; const runtime = init(); try { const result = streamText({ model: openai("gpt-4.1-mini"), prompt: "Invent a new holiday and describe its traditions.", }); for await (const textPart of result.textStream) { process.stdout.write(textPart); } } finally { await runtime.shutdown(); }src/index.ts import { generateText, tool } from "ai"; import { openai } from "@ai-sdk/openai"; import { init } from "confident-trace"; import { z } from "zod"; const runtime = init(); try { const result = await generateText({ model: openai("gpt-4.1-mini"), tools: { weather: tool({ description: "Get the weather in a location", inputSchema: z.object({ location: z.string().describe("The location to get the weather for"), }), execute: async ({ location }) => ({ location, temperature: 72 + Math.floor(Math.random() * 21) - 10, }), }), }, prompt: "What is the weather in San Francisco?", }); console.log(result.text); } finally { await runtime.shutdown(); }Run Your Application
Launch your entry-point file with the
confident-trace/registerpreload so the SDK can hook theaipackage 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.jsTo make this your normal startup command, add it to your
package.jsonscripts: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/streamTextcall, 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.
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.
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:
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:
Instrument Multi-Turn Apps
Group traces into threads with a shared threadId so you can view and
evaluate whole conversations.
Online Evals
Run evaluations on traces, spans, and threads in real-time as they're ingested into Confident AI to monitor AI quality.
Last updated on