JavaScript APIopenai

Core

Instrumentation

Integrations

JavaScript · Instrumentation

@arizeai/openinference-instrumentation-openai

OpenInference instrumentation for OpenAI

Version
4.5.1
Instruments
openai ^7.0.0 || ^6.0.0 || ^5.0.0

Install

npm install @arizeai/openinference-instrumentation-openai

Usage

Register the instrumentation once, at startup, before the library is imported. The OpenAIInstrumentation reference below lists its options.

import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { OpenAIInstrumentation } from "@arizeai/openinference-instrumentation-openai";

const provider = new NodeTracerProvider({
  // ...a span processor that exports to Phoenix or any OTLP backend.
});
provider.register();

registerInstrumentations({
  instrumentations: [new OpenAIInstrumentation()],
});

If the library is imported first (as ES modules often are), patch it directly with manuallyInstrument().

@arizeai/openinference-instrumentation-openai

HOST_SUFFIX_TO_PROVIDER

constantSource ↗
const HOST_SUFFIX_TO_PROVIDER: Record<string, LLMProvider>

Maps hostname suffixes to their corresponding LLM provider value.

getProviderFromHost

functionSource ↗
function getProviderFromHost(host: string): LLMProvider | undefined

Return the LLM provider name for the given API hostname.

isPatched

functionSource ↗
function isPatched(): boolean

function to check if instrumentation is enabled / disabled

OpenAIInstrumentation

classSource ↗
class OpenAIInstrumentation extends InstrumentationBase<typeof openai>

An auto instrumentation class for OpenAI that creates OpenInference Compliant spans for the OpenAI API

Properties

NameType
tracerTracer

new OpenAIInstrumentation

constructorSource ↗
new OpenAIInstrumentation(
  options?: {
    instrumentationConfig?: InstrumentationConfig;
    traceConfig?: TraceConfigOptions;
    tracerProvider?: TracerProvider;
  },
): OpenAIInstrumentation

Parameters

NameTypeDescription
instrumentationConfig?InstrumentationConfig

The config for the instrumentation

traceConfig?TraceConfigOptions

The OpenInference trace configuration. Can be used to mask or redact sensitive information on spans.

tracerProvider?TracerProvider

An optional custom trace provider to be used for tracing. If not provided, a tracer will be created using the global tracer provider. This is useful if you want to use a non-global tracer provider.

manuallyInstrument

methodSource ↗
manuallyInstrument(module: typeof OpenAI): void

Manually instruments the OpenAI module. This is needed when the module is not loaded via require (commonjs)

disable

methodSource ↗
disable(): void

setTracerProvider

methodSource ↗
setTracerProvider(tracerProvider: TracerProvider): void

Sets TraceProvider to this plugin