Vercel AI SDK is a library that allows you 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,
    });
  }
}
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);
}

Laminar skips saving thorough traces created by Next.js by default and only keeps AI SDK traces. Learn more about tracing in Next.js.

4

Results

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