Vercel AI SDK (tracing)

The AI SDK (by Vercel) is a TypeScript toolkit for building AI-powered applications with Next.js, React, and other frameworks. It provides unified APIs for text generation, streaming, tool use, and structured outputs across multiple LLM providers. Respan captures model calls, tool execution, token usage, and multi-step workflow structure when AI SDK telemetry is enabled.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See Vercel AI SDK gateway setup to route this integration through the Respan gateway.

Setup

Supported AI SDK range: ai >=4 <8 (AI SDK 4.x through 7.x). AI SDK 7.x requires @respan/instrumentation-vercel 1.0.10 or later for modern gen_ai.* span support and AI SDK telemetry auto-registration, plus the optional peer package @ai-sdk/otel. AI SDK 4.x-6.x use the legacy experimental_telemetry option and do not require @ai-sdk/otel.

1

Install packages

$npm install ai@^7 @ai-sdk/otel @ai-sdk/openai @respan/respan @respan/instrumentation-vercel@^1.0.10
2

Set environment variables

$export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

OPENAI_API_KEY is used for LLM requests. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize Respan

Create instrumentation.ts in your project root (same level as package.json). Next.js calls register() automatically at startup.

instrumentation.ts
1import { Respan } from "@respan/respan";
2import { VercelAIInstrumentor } from "@respan/instrumentation-vercel";
3
4export async function register() {
5 const respan = new Respan({
6 apiKey: process.env.RESPAN_API_KEY,
7 instrumentations: [new VercelAIInstrumentor()],
8 });
9 await respan.initialize();
10}

Then add serverExternalPackages to next.config.ts:

next.config.ts
1import type { NextConfig } from "next";
2
3const nextConfig: NextConfig = {
4 serverExternalPackages: [
5 "@respan/respan",
6 "@respan/instrumentation-vercel",
7 "ai",
8 "@ai-sdk/otel",
9 ],
10};
11
12export default nextConfig;

VercelAIInstrumentor automatically registers the AI SDK 7 OpenTelemetry bridge when @ai-sdk/otel is installed. If the optional peer is missing, initialization logs a warning and AI SDK 7 calls do not emit the required spans.

4

Initialize and run

1import { generateText } from "ai";
2import { openai } from "@ai-sdk/openai";
3
4const result = await generateText({
5 model: openai("gpt-4.1-nano"),
6 prompt: "Tell me a joke about AI",
7 telemetry: { isEnabled: true },
8});
9
10console.log(result.text);
5

View your trace

Open the Traces page to see your AI calls with input/output, token usage, and tool calls.

Configuration

ParameterTypeDefaultDescription
apiKeystring | undefinedRESPAN_API_KEY env varRespan API key.
baseURLstring | undefined"https://api.respan.ai"API base URL.
instrumentationsRespanInstrumentation[][]Plugin instrumentations to activate.

Attributes

With app-managed AI SDK 7 OpenTelemetry attributes

For global custom attributes, register a configured bridge before Respan initializes and disable the instrumentor’s automatic registration. Register the bridge only once during application startup.

1import { OpenTelemetry } from "@ai-sdk/otel";
2import { VercelAIInstrumentor } from "@respan/instrumentation-vercel";
3import { Respan } from "@respan/respan";
4import { registerTelemetry } from "ai";
5
6registerTelemetry(
7 new OpenTelemetry({
8 enrichSpan: () => ({
9 "ai.telemetry.metadata.customer_identifier": "user-123",
10 "ai.telemetry.metadata.thread_identifier": "thread-abc",
11 "ai.telemetry.metadata.trace_group_identifier": "onboarding-flow",
12 }),
13 })
14);
15
16const respan = new Respan({
17 instrumentations: [
18 new VercelAIInstrumentor({ autoRegisterAISDKTelemetry: false }),
19 ],
20});
21await respan.initialize();

With propagateAttributes

Override per-request using a context scope. All AI SDK calls within the scope inherit these attributes.

1import { Respan } from "@respan/respan";
2import { VercelAIInstrumentor } from "@respan/instrumentation-vercel";
3
4const respan = new Respan({
5 instrumentations: [new VercelAIInstrumentor()],
6});
7await respan.initialize();
8
9async function handleRequest(userId: string, message: string) {
10 return respan.propagateAttributes(
11 {
12 customer_identifier: userId,
13 thread_identifier: "conv_abc_123",
14 metadata: { plan: "pro" },
15 },
16 async () => {
17 const result = await generateText({
18 model: openai("gpt-4.1-nano"),
19 prompt: message,
20 telemetry: { isEnabled: true },
21 });
22 return result.text;
23 }
24 );
25}
AttributeTypeDescription
customer_identifierstringIdentifies the end user in Respan analytics.
thread_identifierstringGroups related messages into a conversation.
metadataRecord<string, string>Custom key-value pairs attached to spans.

Decorators (optional)

Decorators are not required. After the AI SDK 7 OpenTelemetry bridge is registered and request telemetry is enabled, model and tool operations emit spans automatically. Use withWorkflow and withTask to group AI calls into named workflows with nested tasks.

1import { generateText } from "ai";
2import { openai } from "@ai-sdk/openai";
3import { Respan, withWorkflow, withTask } from "@respan/respan";
4import { VercelAIInstrumentor } from "@respan/instrumentation-vercel";
5
6const respan = new Respan({
7 apiKey: process.env.RESPAN_API_KEY,
8 instrumentations: [new VercelAIInstrumentor()],
9});
10await respan.initialize();
11
12await withWorkflow({ name: "joke_pipeline" }, async () => {
13 const intent = await withTask({ name: "classify_intent" }, () =>
14 generateText({
15 model: openai("gpt-4.1-nano"),
16 prompt: 'Classify this intent in one word: "Tell me a joke"',
17 telemetry: { isEnabled: true },
18 })
19 );
20
21 const joke = await withTask({ name: "generate_joke" }, () =>
22 generateText({
23 model: openai("gpt-4.1-nano"),
24 prompt: `The intent is "${intent.text}". Tell a short joke.`,
25 telemetry: { isEnabled: true },
26 })
27 );
28
29 console.log(joke.text);
30});

Examples

Streaming with tools

1import { stepCountIs, streamText, tool } from "ai";
2import { openai } from "@ai-sdk/openai";
3import { z } from "zod";
4
5const result = streamText({
6 model: openai("gpt-4.1-nano"),
7 messages: [{ role: "user", content: "What's the weather in Paris?" }],
8 tools: {
9 getWeather: tool({
10 description: "Get weather for a city",
11 inputSchema: z.object({ city: z.string() }),
12 execute: async ({ city }) => `${city}: sunny, 72F`,
13 }),
14 },
15 stopWhen: stepCountIs(5),
16 telemetry: { isEnabled: true },
17});
18
19for await (const chunk of result.textStream) {
20 process.stdout.write(chunk);
21}

AI SDK 4.x-6.x

Keep @respan/instrumentation-vercel current, omit @ai-sdk/otel, and enable the legacy request telemetry option:

1const result = await generateText({
2 model: openai("gpt-4.1-nano"),
3 prompt: "Tell me a joke about AI",
4 experimental_telemetry: {
5 isEnabled: true,
6 metadata: { customer_identifier: "user-123" },
7 },
8});

The debug message Total instrumentations ready for SDK: 0 refers to legacy Traceloop auto-discovery. It does not mean VercelAIInstrumentor failed to load; that plugin is registered explicitly in Respan({ instrumentations: [...] }).