Files
comet-ml--opik/apps/opik-documentation/documentation/fern/docs-v2/integrations/vercel-ai-sdk.mdx
T
wehub-resource-sync 5a558eb09e
TypeScript SDK Compatibility V1.x E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / TypeScript SDK Compatibility V1.x E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
TypeScript SDK E2E Tests / TypeScript SDK E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
Python SDK E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK E2E Tests / Python SDK E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK E2E Tests / build-opik (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Python SDK Compatibility V1.x E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK E2E Tests / build-opik (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer E2E Tests Python ${{matrix.python_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer Integration Smoke Tests (push) Has been cancelled
🐙 Code Quality / detect (push) Has been cancelled
🐙 Code Quality / lint (${{ matrix.leg.name }}) (push) Has been cancelled
🐙 Code Quality / summary (push) Has been cancelled
TypeScript SDK Library Integration Tests / Check Secrets (push) Has been cancelled
TypeScript SDK Library Integration Tests / opik-vercel (Vercel AI SDK / eve) (push) Has been cancelled
SDK Library Integration Tests Runner / Check Secrets (push) Has been cancelled
SDK Library Integration Tests Runner / Missed OpenAI API Key Warning (push) Has been cancelled
SDK Library Integration Tests Runner / Build (push) Has been cancelled
SDK Library Integration Tests Runner / openai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_legacy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / llama_index_tests (push) Has been cancelled
SDK Library Integration Tests Runner / anthropic_tests (push) Has been cancelled
SDK Library Integration Tests Runner / mistral_tests (push) Has been cancelled
SDK Library Integration Tests Runner / groq_tests (push) Has been cancelled
SDK Library Integration Tests Runner / aisuite_tests (push) Has been cancelled
SDK Library Integration Tests Runner / haystack_tests (push) Has been cancelled
SDK Library Integration Tests Runner / dspy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v1_tests (push) Has been cancelled
SDK Library Integration Tests Runner / genai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_legacy_1_3_0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / evaluation_metrics_tests (push) Has been cancelled
SDK Library Integration Tests Runner / bedrock_tests (push) Has been cancelled
SDK Library Integration Tests Runner / litellm_tests (push) Has been cancelled
SDK Library Integration Tests Runner / harbor_tests (push) Has been cancelled
SDK Library Integration Tests Runner / Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / render-equality (push) Has been cancelled
Opik Optimizer - Unit Tests / Opik Optimizer Unit Tests Python ${{matrix.python_version}} (push) Has been cancelled
Python BE E2E Tests / Python BE E2E (push) Has been cancelled
Python Backend Tests / run-python-backend-tests (push) Has been cancelled
Python SDK Unit Tests / Python SDK Unit Tests ${{matrix.python_version}} (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
SDK E2E Libraries Integration Tests / Check Secrets (push) Has been cancelled
SDK E2E Libraries Integration Tests / Missed OpenAI API Key Warning (push) Has been cancelled
SDK E2E Libraries Integration Tests / build-opik (push) Has been cancelled
SDK E2E Libraries Integration Tests / E2E Lib Integration Python ${{matrix.python_version}} (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-gemini) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-langchain) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-openai) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-otel) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-vercel) (push) Has been cancelled
TypeScript SDK Build & Publish / build-and-publish (push) Has been cancelled
TypeScript SDK Unit Tests / Test on Node ${{ matrix.node-version }} (push) Has been cancelled
Backend Tests / discover-tests (push) Has been cancelled
Backend Tests / ${{ matrix.name }} (push) Has been cancelled
Build and Publish SDK / build-and-publish (push) Has been cancelled
Build Opik Docker Images / set-version (push) Has been cancelled
Build Opik Docker Images / build-backend (push) Has been cancelled
Build Opik Docker Images / build-sandbox-executor-python (push) Has been cancelled
Build Opik Docker Images / build-python-backend (push) Has been cancelled
Build Opik Docker Images / build-frontend (push) Has been cancelled
Build Opik Docker Images / create-git-tag (push) Has been cancelled
ClickHouse Migration Cluster Check / validate-clickhouse-migrations (push) Has been cancelled
Docs - Publish / run (push) Has been cancelled
E2E Tests - Post Merge (v2) / 🧪 E2E v2 Tests (${{ github.event.inputs.tier || 't1' }}) (push) Has been cancelled
E2E Tests - Post Merge (v2) / 📢 Slack Notification (push) Has been cancelled
Frontend Unit Tests / Test on Node 20 (push) Has been cancelled
Guardrails E2E Tests / Select Python version matrix (push) Has been cancelled
Guardrails E2E Tests / Guardrails E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Guardrails E2E Tests / 📢 Slack Notification (push) Has been cancelled
Guardrails Backend Unit Tests / Guardrails Backend Unit Tests (push) Has been cancelled
Guardrails Backend Unit Tests / 📢 Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v3.21.0) (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v4.2.0) (push) Has been cancelled
Lint Opik Helm Chart / unittest-helm-chart (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:25:44 +08:00

220 lines
6.6 KiB
Plaintext

---
description: Start here to integrate Opik into your Vercel AI SDK-based genai application
for end-to-end LLM observability, unit testing, and optimization.
headline: Vercel AI SDK | Opik Documentation
og:description: Enable tracing with Vercel AI SDK using OpikExporter for efficient
telemetry collection in your applications.
og:site_name: Opik Documentation
og:title: Vercel AI SDK Setup - Opik Integration
title: Observability for Vercel AI SDK with Opik
---
## Setup
The AI SDK supports tracing via OpenTelemetry. With the `OpikExporter` you can collect these traces in Opik.
While telemetry is experimental ([docs](https://sdk.vercel.ai/docs/ai-sdk-core/telemetry#enabling-telemetry)), you can enable it by setting `experimental_telemetry` on each request that you want to trace.
```ts
const result = await generateText({
model: openai("gpt-4o"),
prompt: "Tell a joke",
experimental_telemetry: { isEnabled: true },
});
```
To collect the traces in Opik, you need to add the `OpikExporter` to your application, first you have to set your environment variables
Intent:
Use `OpikExporter` as the OTEL trace exporter for AI SDK telemetry.
Applies when:
You are using `generateText`/`streamText` with `experimental_telemetry`.
Required fields:
- `OPIK_API_KEY`
Optional fields:
- `OPIK_WORKSPACE` (defaults to `default` for Cloud/Enterprise; set to override)
- `OPIK_PROJECT_NAME` (defaults to "Default Project"; set to override)
- `OPIK_URL_OVERRIDE` (required for non-default deployment URLs)
- `OPIK_LOG_LEVEL`
Deployment endpoint examples:
- Opik Cloud: `OPIK_URL_OVERRIDE=https://www.comet.com/opik/api`
- Enterprise: `OPIK_URL_OVERRIDE=https://<comet-deployment-url>/opik/api`
- Self-hosted: `OPIK_URL_OVERRIDE=http://localhost:5173/api`
```bash filename=".env"
OPIK_API_KEY="<opik-api-key>"
OPIK_URL_OVERRIDE=https://www.comet.com/opik/api # in case you are using the Cloud version
OPIK_PROJECT_NAME="<custom-project-name>"
OPIK_WORKSPACE="<your-workspace>"
OPENAI_API_KEY="<openai-api-key>" # in case you are using an OpenAI model
```
```ts
import { OpikExporter } from "opik-vercel";
new OpikExporter();
```
Now you need to register this exporter via the OpenTelemetry SDK.
### Next.js
Next.js has support for OpenTelemetry instrumentation on the framework level. Learn more about it in the [Next.js OpenTelemetry guide](https://nextjs.org/docs/app/building-your-application/optimizing/open-telemetry).
Install dependencies:
```bash
npm install opik-vercel @vercel/otel @opentelemetry/api-logs @opentelemetry/instrumentation @opentelemetry/sdk-logs
```
Add `OpikExporter` to your instrumentation file:
```ts filename="instrumentation.ts"
import { registerOTel } from "@vercel/otel";
import { OpikExporter } from "opik-vercel";
export function register() {
registerOTel({
serviceName: "opik-vercel-ai-nextjs-example",
traceExporter: new OpikExporter(),
});
}
```
### Node.js
Install dependencies:
```bash
npm install opik-vercel ai @ai-sdk/openai @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node
```
```ts
import { openai } from "@ai-sdk/openai";
import { generateText } from "ai";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import { OpikExporter } from "opik-vercel";
const sdk = new NodeSDK({
traceExporter: new OpikExporter(),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
async function main() {
const result = await generateText({
model: openai("gpt-4o"),
maxTokens: 50,
prompt: "What is love?",
experimental_telemetry: OpikExporter.getSettings({
name: "opik-nodejs-example",
}),
});
console.log(result.text);
await sdk.shutdown(); // Flushes the trace to Opik
}
main().catch(console.error);
```
Done! All traces that contain AI SDK spans are automatically captured in Opik.
## AI SDK versions and Vercel eve
`OpikExporter` works with all current AI SDK versions — **v4, v5, v6, and v7** —
with no extra setup. It also supports **Vercel eve**, the agent framework built
on AI SDK v7, capturing each run as nested LLM and tool spans. The setup shown
above is all you need, and your code keeps working as you upgrade.
### Multi-turn eve conversations
When you run Vercel eve, a multi-turn conversation is grouped into a single Opik
thread automatically, so every turn shows up together — you don't need to set
`threadId` yourself.
### Cached token usage
If your model reports prompt caching, cached tokens are tracked alongside the
usual input and output tokens, so your token and cost metrics stay accurate.
## Configuration
### Custom Tags and Metadata
You can add custom tags and metadata to all traces generated by the OpikExporter:
```ts
const exporter = new OpikExporter({
// Optional: add custom tags to all traces
tags: ["production", "gpt-4o"],
// Optional: add custom metadata to all traces
metadata: {
environment: "production",
version: "1.0.0",
team: "ai-team",
},
// Optional: associate traces with a conversation thread
threadId: "conversation-123",
});
```
Tags are useful for filtering and grouping traces, while metadata adds additional context that can be valuable for debugging and analysis. The `threadId` parameter is useful for tracking multi-turn conversations or grouping related AI interactions.
### Pass Custom Trace name
```ts
const result = await generateText({
model: openai("gpt-4o"),
prompt: "Tell a joke",
experimental_telemetry: OpikExporter.getSettings({
name: "custom-trace-name",
}),
});
```
### Thread ID Support
You can associate traces with conversation threads by setting the `threadId` parameter. This is useful for tracking multi-turn conversations or grouping related AI interactions.
Set `threadId` per request via telemetry metadata (this overrides any exporter-level `threadId`):
```ts
const result = await generateText({
model: openai("gpt-4o"),
prompt: "Continue the conversation",
experimental_telemetry: OpikExporter.getSettings({
name: "chat-message",
metadata: {
threadId: "conversation-456",
},
}),
});
```
## Debugging
Use the logger level to see the more verbose logs of the exporter.
```bash filename=".env"
OPIK_LOG_LEVEL=DEBUG
```
## Validation
1. Run one AI SDK request with `experimental_telemetry` enabled.
2. Confirm `OpikExporter` initializes without auth errors.
3. Verify traces in the target Opik workspace/project.
## Source references
- [Vercel AI SDK telemetry](https://sdk.vercel.ai/docs/ai-sdk-core/telemetry)
- [Next.js OpenTelemetry guide](https://nextjs.org/docs/app/building-your-application/optimizing/open-telemetry)
- [Opik Vercel exporter package](https://www.npmjs.com/package/opik-vercel)