# OpenInference JS

This is the JavaScript version of OpenInference, a framework for collecting traces from LLM applications.

## Installation

OpenInference uses OpenTelemetry Protocol (OTLP) to send traces to a compatible backend (e.x. [Arize Phoenix](https://github.com/Arize-ai/phoenix) or [Arize AX](https://arize.com/docs/ax)). To use OpenInference, you will need to install the OpenTelemetry SDK and the OpenInference instrumentation for the LLM framework you are using.

Install the OpenTelemetry SDK:

```shell
npm install --save @opentelemetry/exporter-trace-otlp-http @opentelemetry/exporter-trace-otlp-proto @opentelemetry/resources @opentelemetry/sdk-trace-node @opentelemetry/instrumentation
```

Install the OpenInference instrumentation you would like to use:

```shell
npm install --save @arizeai/openinference-instrumentation-openai
```

If you plan on manually instrumenting your application, you will also need to install the OpenInference Semantic Conventions:

```shell
npm install --save @arizeai/openinference-semantic-conventions
```

> [!NOTE]
> This example instruments OpenAI but you can replace `@arizeai/openinference-instrumentation-openai` with the instrumentation(s) of your choosing.

## Usage

To load the OpenAI instrumentation, specify it in the registerInstrumentations call along with any additional instrumentation you wish to enable.

```typescript
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { OpenAIInstrumentation } = require("@arizeai/openinference-instrumentation-openai");
const { registerInstrumentations } = require("@opentelemetry/instrumentation");

const provider = new NodeTracerProvider();
provider.register();

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

For more information on OpenTelemetry Node.js SDK, see the [OpenTelemetry Node.js SDK documentation](https://opentelemetry.io/docs/instrumentation/js/getting-started/nodejs/).

> [!WARNING]
> Note the above instrumentation must run before any other code in your application. This is because the instrumentation will only capture spans for the code that runs after the instrumentation is loaded. Typically this is done by requiring the instrumentation when running your application.
> `node -r ./path/to/instrumentation.js ./path/to/your/app.js`

## Middleware & Adapter Integrations

Some frameworks expose first-class middleware or telemetry hooks instead of being patched at the module level. OpenInference ships dedicated packages for those:

- [`@arizeai/openinference-tanstack-ai`](./packages/openinference-tanstack-ai) — middleware for [TanStack AI](https://tanstack.com/ai/latest/docs/getting-started/overview) that emits OpenInference-shaped spans for `chat()` runs, including LLM turns and tool calls.
- [`@arizeai/openinference-vercel`](./packages/openinference-vercel) — utilities to ingest [Vercel AI SDK](https://github.com/vercel/ai) telemetry and reshape it to the OpenInference spec.

## Examples

For more examples on how to use OpenInference, see the [examples](./examples) directory.

## Contributing

See [contributing guide](../CONTRIBUTING) for information on how to contribute to this project and the [JS Development Guide](./DEVELOPMENT.md) for setting up a development environment.
