Laminar
Monitor your AI SDK applications with Laminar
Laminar is the open-source platform for tracing and evaluating AI applications.
Laminar features:
A version of this guide is available in Laminar's docs.
Setup
Laminar's tracing is based on OpenTelemetry. It supports AI SDK telemetry.
Installation
To start with Laminar's tracing, first install the @lmnr-ai/lmnr package.
$ pnpm add @lmnr-ai/lmnr
$ npm install @lmnr-ai/lmnr
$ yarn add @lmnr-ai/lmnr
$ bun add @lmnr-ai/lmnr
Get your project API key and set in the environment
Then, either sign up on Laminar or self-host an instance (github) and create a new project.
In the project settings, create and copy the API key.
In your .env
Next.js
Initialize tracing
In Next.js, Laminar initialization should be done in instrumentation.{ts,js}:
Add @lmnr-ai/lmnr to your next.config
In your next.config.js (.ts / .mjs), add the following lines:
This is because Laminar depends on OpenTelemetry, which uses some Node.js-specific functionality, and we need to inform Next.js about it. Learn more in the Next.js docs.
Tracing AI SDK calls
Then, when you call AI SDK functions in any of your API routes, add the Laminar tracer to the experimental_telemetry option.
This will create spans for ai.generateText. Laminar collects and displays the following information:
- LLM call input and output
- Start and end time
- Duration / latency
- Provider and model used
- Input and output tokens
- Input and output price
- Additional metadata and span attributes
Older versions of Next.js
If you are using 13.4 ≤ Next.js < 15, you will also need to enable the experimental instrumentation hook. Place the following in your next.config.js:
For more information, see Laminar's Next.js guide and Next.js instrumentation docs. You can also learn how to enable all traces for Next.js in the docs.
Usage with @vercel/otel
Laminar can live alongside @vercel/otel and trace AI SDK calls. The default Laminar setup will ensure that
- regular Next.js traces are sent via
@vercel/otelto your Telemetry backend configured with Vercel, - AI SDK and other LLM or browser agent traces are sent via Laminar.
For an advanced configuration that allows you to trace all Next.js traces via Laminar, see an example repo.
Usage with @sentry/node
Laminar can live alongside @sentry/node and trace AI SDK calls. Make sure to initialize Laminar after Sentry.init.
This will ensure that
- Whatever is instrumented by Sentry is sent to your Sentry backend,
- AI SDK and other LLM or browser agent traces are sent via Laminar.
Node.js
Initialize tracing
Then, initialize tracing in your application:
This must be done once in your application, as early as possible, but after other tracing libraries (e.g. @sentry/node) are initialized.
Read more in Laminar docs.
Tracing AI SDK calls
Then, when you call AI SDK functions in any of your API routes, add the Laminar tracer to the experimental_telemetry option.
This will create spans for ai.generateText. Laminar collects and displays the following information:
- LLM call input and output
- Start and end time
- Duration / latency
- Provider and model used
- Input and output tokens
- Input and output price
- Additional metadata and span attributes
Usage with @sentry/node
Laminar can work with @sentry/node to trace AI SDK calls. Make sure to initialize Laminar after Sentry.init:
This will ensure that
- Whatever is instrumented by Sentry is sent to your Sentry backend,
- AI SDK and other LLM or browser agent traces are sent via Laminar.
The two libraries allow for additional advanced configuration, but the default setup above is recommended.
Additional configuration
Span name
If you want to override the default span name, you can set the functionId inside the telemetry option.
Nested spans
If you want to trace not just the AI SDK calls, but also other functions in your application, you can use Laminar's observe wrapper.
This will create a span with the name "my-function" and trace the function call. Inside it, you will see the nested ai.generateText spans.
To trace input arguments of the function that you wrap in observe, pass them to the wrapper as additional arguments. The return value of the function will be returned from the wrapper and traced as the span's output.
Metadata
In Laminar, metadata is set on the trace level. Metadata contains key-value pairs and can be used to filter traces.
This is converted to Laminar's metadata and stored in the trace.
Tags
One of the reserved metadata keys is tags. It can be used to add tags to the span.
Tags can subsequently be used to filter traces in Laminar.
Session ID and User ID
Traces in Laminar can be grouped into sessions or by user ID. These are also reserved metadata keys.