# @arizeai/openinference-instrumentation-langchain

**JavaScript · Instrumentation**

OpenInference Instrumentation for LangChain.js

- **Version**: 4.2.2
- **Instruments**: `@langchain/core ^1.0.0 || ^0.3.0 || ^0.2.0`

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

## Install

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

## Usage

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

```ts
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { LangChainInstrumentation } from "@arizeai/openinference-instrumentation-langchain";

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

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

If the library is imported first (as ES modules often are), patch it directly with [`manuallyInstrument()`](https://openinf.dev/js/openinference-instrumentation-langchain.html.md#LangChainInstrumentation.manuallyInstrument).

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

### `isPatched`

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

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

function to check if instrumentation is enabled / disabled

### `LangChainInstrumentation`

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

```typescript
class LangChainInstrumentation extends InstrumentationBase<CallbackManagerModule>
```

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

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

**Properties**

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

#### `new LangChainInstrumentation`

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

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

**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-langchain/src/instrumentation.ts#L81)

```typescript
manuallyInstrument(module: __module): void
```

#### `setTracerProvider`

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

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

Sets TraceProvider to this plugin
