We always recommend using the instrumentModules parameter to specify which modules to instrument. Automatic instrumentation of imported modules may work, but it depends on many factors, such as the bundler you are using.

Instrument all available libraries

This method is not recommended in JavaScript. It may not work for all setups. Please read the section on specific modules instrumentation.

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

Laminar.initialize({
  projectApiKey: process.env.LMNR_PROJECT_API_KEY
});

// and then import the instrumentable modules
// ...
import { OpenAI } from 'openai';
import * as anthropic from '@anthropic-ai/sdk';

See all available auto-instrumentable modules here.

Disable automatic instrumentation

initialize() accepts an optional instrumentModules parameter. If you explicitly pass an empty object, no automatic instrumentations will be applied.

import { Laminar } from '@lmnr-ai/lmnr';
import { OpenAI } from 'openai';

Laminar.initialize({
  projectApiKey: process.env.LMNR_PROJECT_API_KEY,
  instrumentModules: {}
});

// When you call OpenAI, it will NOT be instrumented

Instrument specific modules only

You can also enable instrumentation for specific modules only.

This is useful if you want more control over what is being instrumented.

For many JavaScript setups, this may be required. For example, in TS, we’ve seen this required for tsx, but not for ts-node.

Let’s say, for example, we call OpenAI and Anthropic models to perform the same task, and we only want to instrument the Anthropic calls, but not OpenAI.

initialize() accepts an optional instrumentModules parameter. It takes in an object with string keys and values are the entire modules you have imported. In this case we only want to pass anthropic.

If you are using AI SDK and/or Next.js, you may need additional configuration. Please refer to the Next.js guide for more details.

See available instruments in the next subsection.

import { OpenAI } from 'openai';
import * as anthropic from '@anthropic-ai/sdk';
import { Laminar } from '@lmnr-ai/lmnr';

Laminar.initialize(
  { projectApiKey: process.env.LMNR_PROJECT_API_KEY,
    instrumentModules: {
      anthropic: anthropic // only enable anthropic, not openai
    }
  }
);

const openAIClient = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const anthropicClient = new anthropic.Anthropic({
    apiKey: process.env.ANTHROPIC_API_KEY
});

const poemWriter = async (topic = "turbulence") => {
  const prompt = `write a poem about ${topic}`;
  const messages = [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: prompt }
  ];

  // openai calls are NOT instrumented
  const openAIResponse = await openAIClient.chat.completions.create({
    model: "gpt-4o",
    messages,
  });
  
  // anthropic calls are instrumented
  const anthropicResponse = await anthropicClient.messages.create({
    max_tokens: 1024,
    messages,
    model: "claude-3-5-sonnet-20240620",
  })

  const openAIPoem = response.choices[0].message.content;
  const anthropicPoem = anthropicResponse.content[0].text;
  return {o: openAIPoem, a: anthropicPoem};
}

Available instruments