The AI SDK supports tracing via OpenTelemetry. With the OpikExporter you can collect these traces in Opik.
While telemetry is experimental (docs), you can enable it by setting experimental_telemetry on each request that you want to trace.
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_KEYOptional 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_LEVELDeployment endpoint examples:
OPIK_URL_OVERRIDE=https://www.comet.com/opik/apiOPIK_URL_OVERRIDE=https://<comet-deployment-url>/opik/apiOPIK_URL_OVERRIDE=http://localhost:5173/apiNow you need to register this exporter via the OpenTelemetry SDK.
Next.js has support for OpenTelemetry instrumentation on the framework level. Learn more about it in the Next.js OpenTelemetry guide.
Install dependencies:
Add OpikExporter to your instrumentation file:
Install dependencies:
Done! All traces that contain AI SDK spans are automatically captured in Opik.
You can add custom tags and metadata to all traces generated by the OpikExporter:
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.
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):
Use the logger level to see the more verbose logs of the exporter.
experimental_telemetry enabled.OpikExporter initializes without auth errors.