Skip to main content
You can trace Vercel AI SDK calls in Weave using OpenTelemetry (OTel). The Vercel AI SDK is a TypeScript toolkit for building AI-powered applications, with framework support for Next.js, Nuxt, SvelteKit, and others. It has built-in OpenTelemetry support through its experimental_telemetry option. This guide shows you how to configure OTel to send traces from the Vercel AI SDK to Weave so that you can observe and debug your AI SDK calls in the Weave UI. You can use the AI SDK with Next.js or as a standalone Node.js application. This guide is intended for developers building AI-powered applications with the Vercel AI SDK who want to add tracing to an existing app. For more information about OTel tracing in Weave, see Send OTel traces to Weave.

Prerequisites

Before you begin, complete the following setup. Both the Next.js and Node.js examples in this guide require the same dependencies:
  1. Install the following Vercel and OTel libraries:
  2. Set the following environment variables:
    You can get your W&B API key from User Settings.

Configure OTel tracing for Next.js

This section demonstrates how to configure Weave in a Next.js app. The example doesn’t contain a full app, only the instrumentation configuration and how to invoke the instrumentation on a Vercel AI SDK function (in this case, a call to OpenAI).

Configure instrumentation

Next.js applications use an instrumentation.ts file to set up OTel. This file runs once when the server starts and configures the tracer provider that the AI SDK uses. To integrate Weave with Vercel’s OTel functionality, create an instrumentation.ts file in your project root and add the following code to it, updating the resourceFromAttributes() function with your team and project names:
instrumentation.ts
This creates an OTLP exporter configured to send trace data to Weave’s OTel endpoint, authenticating with your W&B API key.

Configure telemetry on a function

After you’ve added the instrumentation, use Vercel’s experimental_telemetry option on any AI SDK function call to emit OTel spans:
route.ts
All generateText calls with telemetry enabled produce OTel spans that are exported to Weave. Your Next.js app is now configured to send Vercel AI SDK traces to Weave.

Configure OTel tracing for Node.js

If you’re not using Next.js, you can configure OTel directly in a standalone Node.js application. For standalone Node.js applications, configure the tracer provider at the top of your entry file before any AI SDK calls so that subsequent AI SDK calls are captured. After meeting the prerequisites, you can run the following example and generate spans without any additional configuration.
test-app.ts
BatchSpanProcessor flushes spans asynchronously. In short-lived processes like standalone scripts, serverless functions, or CLI tools, call provider.shutdown() before the process exits to ensure all spans are sent to Weave. For long-running servers (like a Next.js dev server started through instrumentation.ts), this isn’t necessary.