Vercel AI SDK is a great library to add LLM features to your JS/TS applications. It supports tracing using OpenTelemetry.

Laminar tracing is based on OpenTelemetry, so it is fully compatible with Vercel AI SDK tracing and you can start sending Vercel AI SDK traces to Laminar right away.

This guide will show you how to send Vercel AI SDK traces to Laminar if you are using NextJS app router.

1

Initialize Laminar

  1. Add instrumentation.ts to the root of your NextJS project. Learn more about instrumentation.ts here.

  2. Inside of instrumentation.ts, initialize Laminar with your project API key as follows:

instrumentations.ts
import { Laminar } from '@lmnr-ai/lmnr';

export async function register() {
  // prevent this from running in the edge runtime
  if (process.env.NEXT_RUNTIME === 'nodejs') {

    const { Laminar } = await import('@lmnr-ai/lmnr');
    Laminar.initialize({
      projectApiKey: process.env.LMNR_API_KEY,
    });
  }
}
  1. By default, Laminar will track all OpenTelemetry traces. To prevent tracking all API calls, set the following environment variable:
export NEXT_OTEL_FETCH_DISABLED=1
2

Enable Vercel AI SDK tracing

Enable experimental_telemetry in the Vercel AI SDK configuration.

Generate text
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

await generateText({
    model: openai( 'gpt-4o-mini'), messages: [
        {
            role: 'user',
            content: 'What is the capital of France?'
        }
    ],
    experimental_telemetry: {
        isEnabled: true
    }
});
3

NextJS

NextJS route handler example:

NextJS
import { Laminar } from '@lmnr-ai/lmnr';
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

export async function GET() {

    const response = await generateText({
        model: openai('gpt-4o-mini'), messages: [
            {
                role: 'user',
                content: 'What is the capital of France?'
            }
        ],
        experimental_telemetry: {
            isEnabled: true
        }
    });

    return Response.json(response);
}
4

Results

And that’s it! You should now see traces in Laminar.