Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions docs/platforms/javascript/common/configuration/integrations/openai.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
---
title: OpenAI
description: "Adds instrumentation for OpenAI API."
supported:
- javascript.node
- javascript.aws-lambda
- javascript.azure-functions
- javascript.connect
- javascript.express
- javascript.fastify
- javascript.gcp-functions
- javascript.hapi
- javascript.hono
- javascript.koa
- javascript.nestjs
- javascript.electron
- javascript.nextjs
- javascript.nuxt
- javascript.solidstart
- javascript.sveltekit
- javascript.react-router
- javascript.remix
- javascript.astro
- javascript.bun
- javascript.tanstackstart-react
- javascript.cloudflare
---

<Alert>

This integration works in the Node.js, Cloudflare Workers, Vercel Edge Functions runtimes. It requires SDK version `10.2.0` or higher.

</Alert>

_Import name: `Sentry.openAIIntegration`_

The `openAIIntegration` adds instrumentation for the `openai` API to capture spans by automatically wrapping OpenAI client calls and recording LLM interactions with configurable input/output recording.

<PlatformSection notSupported={["javascript.cloudflare", "javascript.nextjs"]}>
It is enabled by default and will automatically capture spans for OpenAI API method calls. You can opt-in to capture inputs and outputs by setting `recordInputs` and `recordOutputs` in the integration config:

```javascript
Sentry.init({
dsn: "____PUBLIC_DSN____",
tracesSampleRate: 1.0,
integrations: [
Sentry.openAIIntegration({
recordInputs: true,
recordOutputs: true,
}),
],
});
```

</PlatformSection>

<PlatformSection supported={["javascript.cloudflare"]}>
For Cloudflare Workers, you need to manually instrument the OpenAI client using the `instrumentOpenAiClient` helper:

```javascript
import * as Sentry from "@sentry/cloudflare";
import OpenAI from "openai";

const openai = new OpenAI();
const client = Sentry.instrumentOpenAiClient(openai, {
recordInputs: true,
recordOutputs: true,
});

// Use the wrapped client instead of the original openai instance
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello!" }],
});
```

</PlatformSection>

<PlatformSection supported={['javascript.nextjs']}>

This integration is automatically instrumented in the Node.js runtime. For Next.js applications using the Edge runtime, you need to manually instrument the OpenAI client:

```javascript
import * as Sentry from "@sentry/nextjs";
import OpenAI from "openai";

const openai = new OpenAI();
const client = Sentry.instrumentOpenAiClient(openai, {
recordInputs: true,
recordOutputs: true,
});

// Use the wrapped client instead of the original openai instance
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello!" }],
});
```

</PlatformSection>

## Options

### `recordInputs`

_Type: `boolean`_

Records inputs to OpenAI API method calls (such as prompts and messages).

Defaults to `true` if `sendDefaultPii` is `true`.

```javascript
Sentry.init({
integrations: [Sentry.openAIIntegration({ recordInputs: true })],
});
```

### `recordOutputs`

_Type: `boolean`_

Records outputs from OpenAI API method calls (such as generated text and responses).

Defaults to `true` if `sendDefaultPii` is `true`.

```javascript
Sentry.init({
integrations: [Sentry.openAIIntegration({ recordOutputs: true })],
});
```

## Configuration

By default this integration adds tracing support to OpenAI API method calls including:

- `chat.completions.create()` - Chat completion requests
- `responses.create()` - Response API requests

The integration will automatically detect streaming vs non-streaming requests and handle them appropriately.

## Supported Versions

- `openai`: `>=4.0.0 <6`
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ Depending on whether an integration enhances the functionality of a particular r
| [`localVariablesIntegration`](./localvariables) | | ✓ | | |
| [`nodeProfilingIntegration`](./nodeprofiling) | | | ✓ | |
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
| [`onUnhandledRejectionIntegration`](./unhandledrejection) | ✓ | ✓ | | |
| [`childProcessIntegration`](./childProcess) | ✓ | | | ✓ |
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`amqplibIntegration`](./amqplib) | | | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
| [`tediousIntegration`](./tedious) | ✓ | | ✓ | |
| [`prismaIntegration`](./prisma) | ✓ | | ✓ | |
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
| [`dataloaderIntegration`](./dataloader) | | | ✓ | |
| [`extraErrorDataIntegration`](./extraerrordata) | | | | ✓ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
| [`childProcessIntegration`](./childProcess) | ✓ | | | ✓ |
| [`rewriteFramesIntegration`](./rewriteframes) | ✓ | ✓ | | |
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
| [`eventLoopBlockIntegration`](./event-loop-block) | | ✓ | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
| [`childProcessIntegration`](./childProcess) | ✓ | | | ✓ |
| [`prismaIntegration`](./prisma) | ✓ | | ✓ | |
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
| [`eventLoopBlockIntegration`](./event-loop-block) | | ✓ | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
| [`onUnhandledRejectionIntegration`](./unhandledrejection) | ✓ | ✓ | | |
| [`childProcessIntegration`](./childProcess) | ✓ | | | ✓ |
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`amqplibIntegration`](./amqplib) | | | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
| [`childProcessIntegration`](./childProcess) | ✓ | | | ✓ |
| [`prismaIntegration`](./prisma) | ✓ | | ✓ | |
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
| [`eventLoopBlockIntegration`](./event-loop-block) | | ✓ | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@
| [`supabaseIntegration`](./supabase) | | ✓ | ✓ | |
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`unleashIntegration`](./unleash) | | | | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Depending on whether an integration enhances the functionality of a particular r
| [`nodeProfilingIntegration`](./nodeprofiling) | | | ✓ | |
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |

### Edge Integrations

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@
| [`supabaseIntegration`](./supabase) | | ✓ | ✓ | |
| [`tediousIntegration`](./tedious) | ✓ | | ✓ | |
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ Depending on whether an integration enhances the functionality of a particular r
| [`localVariablesIntegration`](./localvariables) | | ✓ | | |
| [`nodeProfilingIntegration`](./nodeprofiling) | | | ✓ | |
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ Depending on whether an integration enhances the functionality of a particular r
| [`localVariablesIntegration`](./localvariables) | | ✓ | | |
| [`nodeProfilingIntegration`](./nodeprofiling) | | | ✓ | |
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ Depending on whether an integration enhances the functionality of a particular r
| [`localVariablesIntegration`](./localvariables) | | ✓ | | |
| [`nodeProfilingIntegration`](./nodeprofiling) | | | ✓ | |
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ Depending on whether an integration enhances the functionality of a particular r
| [`nodeProfilingIntegration`](./nodeprofiling) | | | ✓ | |
| [`supabaseIntegration`](./supabase) | | ✓ | ✓ | |
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |