NextJS
Instrument your NextJS app based on App Router with Laminar
1. Install Laminar
npm install @lmnr-ai/lmnr
2. Initialize Laminar
We suggest to disable Next.js OpenTelemetry instrumentation to avoid excessive tracing.
export NEXT_OTEL_FETCH_DISABLED=1 # Disable next.js otel instrumentation to avoid excessive tracing
Then, choose the route you want to instrument and initialize Laminar at the top of your route file.
import { Laminar as L } from '@lmnr-ai/lmnr';
L.initialize({projectApiKey: process.env.LMNR_PROJECT_API_KEY});
Simple initialization may not work for all cases.
The best workaround is to selectively pass instrumentModules
to .initialize()
.
The example below shows how to do this for OpenAI and Anthropic.
import OpenAI from 'openai';
import * as anthropic from '@anthropic-ai/sdk';
import { Laminar as L } from '@lmnr-ai/lmnr';
L.initialize({
projectApiKey: process.env.LMNR_PROJECT_API_KEY,
instrumentModules: { openAI: OpenAI, anthropic: anthropic }
});
If you want to try it quickly without manually importing and instrumenting all modules, you can do simple initialization, however, you will need to import and initialize Laminar before importing LLM client libraries. This is because JavaScript binds the imported module to its name at import time, so introducing instrumentation afterwards statically is not possible.
Auto-instrumentable modules
Here’s the list of modules that you can instrument:
openAI
anthropic
azureOpenAI
cohere
bedrock
google_vertexai
google_aiplatform
pinecone
langchain
:chainsModule
agentsModule
toolsModule
runnablesModule
vectorStoreModule
llamaIndex
chromadb
qdrant
3. Add Laminar initialization to every API route
Similarly to previous step, import and initialize Laminar at the top of every API route.