Integrations
OpenAI SDK (JS/TS)
Wrap the OpenAI JavaScript/TypeScript SDK to automatically trace prompts, completions, latencies, and token usage in Muster.
The Muster JS/TS integration ships a wrapper around the OpenAI SDK that adds
observability with a single function call. Wrap your OpenAI() instance
once and every call routed through it is captured automatically.
Key Features
The integration automatically tracks:
- All prompts and completions (including streaming and function calling)
- Latencies and time-to-first-token metrics
- API errors
- Token usage and costs
Setup
Installation
npm install @langfuse/openai openaiEnvironment Variables
Add credentials to your .env:
LANGFUSE_SECRET_KEY="sk-lf-..."
LANGFUSE_PUBLIC_KEY="pk-lf-..."
LANGFUSE_BASE_URL="https://app.getmuster.io" # or your self-hosted Muster URLOpenTelemetry Initialisation
import { NodeSDK } from "@opentelemetry/sdk-node";
import { LangfuseSpanProcessor } from "@langfuse/otel";
const sdk = new NodeSDK({
spanProcessors: [new LangfuseSpanProcessor()],
});
sdk.start();Basic Usage
import OpenAI from "openai";
import { observeOpenAI } from "@langfuse/openai";
const openai = observeOpenAI(new OpenAI());
const res = await openai.chat.completions.create({
messages: [{ role: "system", content: "Tell me a story about a dog." }],
model: "gpt-4o",
});Advanced Features
The langfuseConfig parameter on observeOpenAI supports:
generationName— identify specific generation typeslangfusePrompt— link to managed promptsgenerationMetadata— add custom metadatasessionId— track user sessionsuserId— identify userstags— categorise traces
const openai = observeOpenAI(new OpenAI(), {
generationName: "summary",
sessionId: "session_abc",
userId: "user_123",
tags: ["prod"],
});Important Notes
- Compatible with OpenAI SDK versions ≥4.0.0.
- Short-lived applications require manual flushing:
await langfuseSpanProcessor.forceFlush(). - The OpenAI Assistants API is not supported.
- Streamed responses require
stream_options: { include_usage: true }to capture token usage.
See also
- OpenAI SDK (Python)
- Upstream Langfuse OpenAI (JS/TS) docs for the latest cookbook examples