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
- TypeScript
- Python
1. Install Laminar and OpenAI
2. Set up your environment variables
Store your API keys in a.env file:Using OpenAI inside Next.js? The Vercel AI SDK page covers the Next.js-specific setup (
serverExternalPackages, instrumentation.ts, and Laminar.patch for direct SDK clients).3. Initialize Laminar
Just add a single line at the start of your application or file to instrument OpenAI with Laminar.import OpenAI from 'openai' imports the module. Pass it to
instrumentModules, then create the client with new OpenAI.OpenAI().4. Use OpenAI as usual
Monitoring OpenAI usage
After instrumenting your OpenAI calls with Laminar, you’ll be able to:- View detailed traces of each OpenAI API call, including request and response
- Track token usage and cost across different models
- Monitor latency and performance metrics
- Open LLM span in Playground for prompt engineering
- Debug issues with failed API calls or unexpected model outputs
Enrich your OpenAI traces
- Attach sessions, user IDs, metadata, and tags to OpenAI spans via the SDK reference.
- Wrap the functions around your OpenAI calls with
observeand mark them as TOOL spans so they show up in the transcript. - Images you send to vision-capable OpenAI models are captured automatically (Tracing Images).
Vision inputs (images)
Laminar automatically traces image inputs (data URLs and external URLs) sent to vision-capable OpenAI models.- TypeScript
- Python
Decisions API
The Decisions API answers a list of typed questions about a piece of input in one call: predicates (yes or no, with a probability), choices (one value from a fixed list), and scores (a level on a scale). Use it for routing, guardrails, and grading steps in your agent. In TypeScript, call Decisions through the Vercel AI SDK v7experimental_decide function. Laminar traces it through LaminarAiSdkTelemetry, which needs @lmnr-ai/lmnr 0.8.50 or later. In Python, Laminar traces every client.decisions.create call in the OpenAI SDK, sync and async, with no extra setup. That needs lmnr 0.7.65 or later and openai 3.26.0 or later.
- TypeScript
- Python
ai.decide in TypeScript (ai.decide triage-ticket when you set telemetry.functionId) and openai.decision in Python. The span carries:
- The model, and the input you sent as the span input. A string becomes a user message. Objects, part arrays, and message lists (including image parts) are recorded as you passed them.
- The full question set, with instructions, choices, and levels for every question, in the span’s output schema.
- The typed answers as the span output, with probabilities intact.
- Token counts, including cached input tokens.
- The user ID:
runtimeContext.userIdin TypeScript (opted in withincludeRuntimeContext),safety_identifierin Python.
recordInputs: false and recordOutputs: false in the AI SDK telemetry options, or LMNR_TRACE_CONTENT=false in Python.
Related integrations
Anthropic
Trace the Anthropic SDK directly in TypeScript and Python.
Gemini
Trace Gemini calls made with the Google Gen AI SDK.
OpenRouter
Trace OpenRouter requests with model and cost attribution.
OpenAI Agents SDK
Trace agent runs, handoffs, and tool calls.
Vercel AI SDK
Trace
generateText and streamText in Next.js and Node.js.All integrations
Browse every provider, framework, coding agent, and browser integration Laminar supports.