# @arizeai/openinference-instrumentation-openai

**JavaScript · Instrumentation**

OpenInference instrumentation for OpenAI

- **Version**: 4.5.1
- **Instruments**: `openai ^7.0.0 || ^6.0.0 || ^5.0.0`

[npm](https://www.npmjs.com/package/@arizeai/openinference-instrumentation-openai) · [README](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-openai/README.md) · [Source](https://github.com/Arize-ai/openinference/tree/main/js/packages/openinference-instrumentation-openai)

## Install

```sh
npm install @arizeai/openinference-instrumentation-openai
```

## Usage

Register the instrumentation once, at startup, before the library is imported. The [`OpenAIInstrumentation`](https://openinf.dev/js/openinference-instrumentation-openai.html.md#OpenAIInstrumentation) reference below lists its options.

```ts
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()`](https://openinf.dev/js/openinference-instrumentation-openai.html.md#OpenAIInstrumentation.manuallyInstrument).

## `@arizeai/openinference-instrumentation-openai`

### `HOST_SUFFIX_TO_PROVIDER`

_constant_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-openai/src/instrumentation.ts#L63)

```typescript
const HOST_SUFFIX_TO_PROVIDER: Record<string, LLMProvider>
```

Maps hostname suffixes to their corresponding LLM provider value.

### `getProviderFromHost`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-openai/src/instrumentation.ts#L98)

```typescript
function getProviderFromHost(host: string): LLMProvider | undefined
```

Return the LLM provider name for the given API hostname.

### `isPatched`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-openai/src/instrumentation.ts#L132)

```typescript
function isPatched(): boolean
```

function to check if instrumentation is enabled / disabled

### `OpenAIInstrumentation`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-openai/src/instrumentation.ts#L246)

```typescript
class OpenAIInstrumentation extends InstrumentationBase<typeof openai>
```

An auto instrumentation class for OpenAI that creates [OpenInference](https://github.com/Arize-ai/openinference/blob/main/spec/attributes/semantic-conventions.md) Compliant spans for the OpenAI API

> **See also**
>
> - `InstrumentationConfig`
> - [`TraceConfigOptions`](https://openinf.dev/js/openinference-core.html.md#TraceConfigOptions)

**Properties**

| Name | Type |
| - | - |
| [`tracer`](https://openinf.dev/js/openinference-instrumentation-openai.html.md#OpenAIInstrumentation.tracer) | `Tracer` |

#### `new OpenAIInstrumentation`

_constructor_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-openai/src/instrumentation.ts#L254)

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

**Parameters**

| Name | Type | Description |
| - | - | - |
| `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`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-openai/src/instrumentation.ts#L301)

```typescript
manuallyInstrument(module: typeof OpenAI): void
```

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

#### `disable`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-openai/src/instrumentation.ts#L306)

```typescript
disable(): void
```

#### `setTracerProvider`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-instrumentation-openai/src/instrumentation.ts#L320)

```typescript
setTracerProvider(tracerProvider: TracerProvider): void
```

Sets TraceProvider to this plugin
