Skip to main content

Overview

Laminar automatically instruments the official OpenAI package with a single line of code, allowing you to trace and monitor all your OpenAI API calls without modifying your existing code. This provides complete visibility into your AI application’s performance, costs, and behavior.

Getting Started

1. Install Laminar and OpenAI

npm install @lmnr-ai/lmnr openai

2. Set up your environment variables

Store your API keys in a .env file:
# .env file
LMNR_PROJECT_API_KEY=your-laminar-project-api-key
OPENAI_API_KEY=your-openai-api-key
Then load them in your application using a package like dotenv.
If you are using OpenAI with Next.js, please follow the Next.js integration guide for best practices and setup instructions.

3. Initialize Laminar

Just add a single line at the start of your application or file to instrument OpenAI with Laminar.
import { Laminar } from '@lmnr-ai/lmnr';
import OpenAI from 'openai';
import 'dotenv/config'; // Load environment variables

// This single line instruments all OpenAI API calls
Laminar.initialize({
  instrumentModules: { OpenAI: OpenAI }
});

// Initialize OpenAI client as usual
const openai = new OpenAI();
It is important to pass OpenAI to instrumentModules as a named export.

4. Use OpenAI as usual

// Make API calls to OpenAI as you normally would
const response = await openai.chat.completions.create({
  model: "gpt-4.1-mini",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "Hello, how are you?" }
  ],
});

console.log(response.choices[0].message.content);
All OpenAI API calls are now automatically traced in Laminar.
These features allow you to build more structured traces, add context to your LLM calls, and gain deeper insights into your AI application’s performance.

Monitoring Your OpenAI Usage

After instrumenting your OpenAI calls with Laminar, you’ll be able to:
  1. View detailed traces of each OpenAI API call, including request and response
  2. Track token usage and cost across different models
  3. Monitor latency and performance metrics
  4. Open LLM span in Playground for prompt engineering
  5. Debug issues with failed API calls or unexpected model outputs
Visit your Laminar dashboard to view your OpenAI traces and analytics.

Advanced Features

  • Enrich traces with sessions, user IDs, metadata, and tags via the SDK reference.
  • Wrap custom functions with observe to capture business logic alongside model calls.
  • Images sent to vision-capable models are captured automatically — see Tracing Images.

Vision inputs (images)

Laminar automatically traces image inputs (data URLs and external URLs) sent to vision-capable OpenAI models.
const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Describe this product and its key features.' },
        {
          type: 'image_url',
          image_url: {
            url: 'https://example.com/product-image.jpg',
            detail: 'high',
          },
        },
      ],
    },
  ],
});

console.log(response.choices[0].message.content);