Skip to content

Commit ad56cca

Browse files
[Weave] Vercel integration (#2414)
## Description Resolves [WBDOCS-1734](https://coreweave.atlassian.net/browse/WBDOCS-1734). This adds a new Weave integration doc demonstrating how to use Vercel's native OTel instrumentation to emit trace to Weave. [WBDOCS-1734]: https://wandb.atlassian.net/browse/WBDOCS-1734?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Anastasia Guspan <happyguspan@gmail.com>
1 parent e91bf80 commit ad56cca

3 files changed

Lines changed: 146 additions & 0 deletions

File tree

docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@
894894
"weave/guides/integrations/smolagents",
895895
"weave/guides/integrations/pydantic_ai",
896896
"weave/guides/integrations/google_adk",
897+
"weave/guides/integrations/vercel_ai_sdk",
897898
"weave/guides/integrations/agno",
898899
"weave/guides/integrations/koog",
899900
"weave/guides/integrations/autogen",

weave/guides/integrations.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Frameworks help orchestrate the actual execution pipelines in AI applications. T
5151
- **[AutoGen](/weave/guides/integrations/autogen)**
5252
- **[Verdict](/weave/guides/integrations/verdict)**
5353
- **[TypeScript SDK](/weave/guides/integrations/js)**
54+
- **[Vercel AI SDK](/weave/guides/integrations/vercel_ai_sdk)**
5455
- **[Agno](/weave/guides/integrations/agno)**
5556
- **[Koog](/weave/guides/integrations/koog)**
5657

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
title: "Vercel AI SDK"
3+
description: "Trace Vercel AI SDK calls in Weave using OpenTelemetry"
4+
---
5+
6+
You can trace [Vercel AI SDK](https://ai-sdk.dev/) calls in Weave using [OpenTelemetry (OTel)](https://opentelemetry.io/). The Vercel AI SDK is a TypeScript toolkit for building AI-powered applications with framework support for Next.js, Nuxt, SvelteKit, and other frameworks. It has built-in OpenTelemetry support through its `experimental_telemetry` option.
7+
8+
This guide shows you how to configure OTel to send traces from the Vercel AI SDK to Weave. You can use the AI SDK with Next.js or as a standalone Node.js application.
9+
10+
For more information on OTel tracing in Weave, see [Send OTel traces to Weave](../tracking/otel).
11+
12+
### Prerequisites
13+
14+
Both of the Next.js and Node.js examples in this guide require the same dependencies. To start:
15+
16+
1. Install the following Vercel and OTel libraries:
17+
18+
```bash
19+
npm install ai @ai-sdk/openai @opentelemetry/api @opentelemetry/sdk-trace-node @opentelemetry/sdk-trace-base @opentelemetry/exporter-trace-otlp-proto @opentelemetry/resources zod
20+
```
21+
22+
2. Set the following environment variables:
23+
24+
```bash
25+
export WANDB_API_KEY="your-wandb-api-key"
26+
export OPENAI_API_KEY="your-openai-api-key"
27+
```
28+
29+
You can get your W&B API key from [User Settings](https://wandb.ai/settings).
30+
31+
## Configure OTel tracing for Next.js
32+
33+
This section demonstrates how to configure Weave in a Next.js app. The example does not contain a full app, just the instrumentation configuration and how to invoke the instrumentation on a Vercel AI SDK function, in this a case, a simple call to OpenAI.
34+
35+
### Configure instrumentation
36+
37+
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.
38+
39+
To integrate Weave with Vercel's OTeL's 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:
40+
41+
```typescript instrumentation.ts lines {17-19}
42+
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
43+
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
44+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
45+
import { resourceFromAttributes } from "@opentelemetry/resources";
46+
47+
export function register() {
48+
const WANDB_API_KEY = process.env.WANDB_API_KEY!;
49+
50+
// Configure OTel exporter to use W&B Weave endpoint.
51+
const exporter = new OTLPTraceExporter({
52+
url: "https://trace.wandb.ai/otel/v1/traces",
53+
headers: { "wandb-api-key": WANDB_API_KEY },
54+
});
55+
56+
// Configure your W&B credentials.
57+
const provider = new NodeTracerProvider({
58+
resource: resourceFromAttributes({
59+
"wandb.entity": "<your-team-name>",
60+
"wandb.project": "<your-project-name>",
61+
}),
62+
spanProcessors: [new BatchSpanProcessor(exporter)],
63+
});
64+
65+
provider.register();
66+
}
67+
```
68+
69+
This creates an OTLP exporter configured to send trace data to Weave's OTel endpoint, authenticating with your W&B API key.
70+
71+
### Configure telemetry on a function
72+
73+
Once you've added the instrumentation, use Vercel's `experimental_telemetry` option on any AI SDK function call to emit OTel spans:
74+
75+
```typescript route.ts lines {10}
76+
import { openai } from "@ai-sdk/openai";
77+
import { generateText } from "ai";
78+
79+
export async function POST(req: Request) {
80+
const { prompt } = await req.json();
81+
82+
// Add experiment_telemetry field to function.
83+
const result = await generateText({
84+
model: openai("gpt-4o-mini"),
85+
prompt,
86+
experimental_telemetry: { isEnabled: true },
87+
});
88+
89+
return Response.json({ text: result.text });
90+
}
91+
```
92+
93+
All `generateText` calls with telemetry enabled produce OTel spans that are exported to Weave.
94+
95+
## Configure OTel tracing for Node.js
96+
97+
For standalone Node.js applications (without Next.js), configure the tracer provider at the top of your entry file before any AI SDK calls.
98+
99+
After meeting the prerequisites, you can run this example and generate spans without any additional configuration.
100+
101+
```typescript lines {11-14,18-19,31} title="test-app.ts"
102+
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
103+
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
104+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
105+
import { resourceFromAttributes } from "@opentelemetry/resources";
106+
import { openai } from "@ai-sdk/openai";
107+
import { generateText } from "ai";
108+
109+
const WANDB_API_KEY = process.env.WANDB_API_KEY!;
110+
111+
// Configure OTel exporter to use W&B Weave endpoint.
112+
const exporter = new OTLPTraceExporter({
113+
url: "https://trace.wandb.ai/otel/v1/traces",
114+
headers: { "wandb-api-key": WANDB_API_KEY },
115+
});
116+
117+
// Configure your W&B credentials.
118+
const provider = new NodeTracerProvider({
119+
resource: resourceFromAttributes({
120+
"wandb.entity": "<your-team-name>",
121+
"wandb.project": "<your-project-name>",
122+
}),
123+
spanProcessors: [new BatchSpanProcessor(exporter)],
124+
});
125+
126+
provider.register();
127+
128+
// Add experiment_telemetry field to function.
129+
async function main() {
130+
const result = await generateText({
131+
model: openai("gpt-4o-mini"),
132+
prompt: "Explain OpenTelemetry in one sentence.",
133+
experimental_telemetry: { isEnabled: true },
134+
});
135+
136+
console.log(result.text);
137+
138+
await provider.shutdown();
139+
}
140+
141+
main();
142+
```
143+
144+
`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 is not necessary.

0 commit comments

Comments
 (0)