Vercel AI SDK Integration

Opik provides seamless integration with the Vercel AI SDK through OpenTelemetry instrumentation. This integration helps you monitor and debug AI-powered applications built with Vercel’s AI tooling.

Installation

Install the required dependencies:

$npm install opik ai @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node

Basic Usage

Here’s a simple example of integrating Opik with the Vercel AI SDK:

1import { openai } from "@ai-sdk/openai";
2import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
3import { NodeSDK } from "@opentelemetry/sdk-node";
4import { generateText } from "ai";
5import { OpikExporter } from "opik/vercel";
6
7// Initialize OpenTelemetry with Opik exporter
8const sdk = new NodeSDK({
9 traceExporter: new OpikExporter(),
10 instrumentations: [getNodeAutoInstrumentations()],
11});
12
13sdk.start();
14
15// Use Vercel AI SDK with Opik telemetry
16const { text } = await generateText({
17 model: openai("gpt-4o-mini"),
18 prompt: "What is love? Describe it in 10 words or less.",
19 experimental_telemetry: OpikExporter.getSettings({
20 name: "ai-sdk-integration",
21 }),
22});
23
24// Shutdown OpenTelemetry SDK before app exits
25await sdk.shutdown();

Features

The Vercel AI SDK integration automatically captures:

  • Input prompts and messages
  • Model responses
  • Token usage statistics
  • Tool calls and their results
  • Timing information
  • Error states

All this telemetry data is automatically sent to your Opik project for analysis and monitoring.

Advanced Configuration

Custom Tags and Metadata

You can add custom tags and metadata to all traces generated by the OpikExporter:

1const exporter = new OpikExporter({
2 // Optional: add custom tags to all traces
3 tags: ["production", "gpt-4o"],
4 // Optional: add custom metadata to all traces
5 metadata: {
6 environment: "production",
7 version: "1.0.0",
8 team: "ai-team",
9 },
10});

Tags can be used for filtering and grouping traces, while metadata adds additional context to your traces that can be useful for debugging and analysis.