diff --git a/docs/platforms/javascript/common/configuration/integrations/openai.mdx b/docs/platforms/javascript/common/configuration/integrations/openai.mdx index 082871cee80f4..6e03e97b73b4a 100644 --- a/docs/platforms/javascript/common/configuration/integrations/openai.mdx +++ b/docs/platforms/javascript/common/configuration/integrations/openai.mdx @@ -1,6 +1,6 @@ --- title: OpenAI -description: "Adds instrumentation for OpenAI API." +description: "Adds instrumentation for the OpenAI SDK." supported: - javascript.node - javascript.aws-lambda @@ -25,66 +25,66 @@ supported: - javascript.tanstackstart-react - javascript.cloudflare - javascript + - javascript.react + - javascript.angular + - javascript.vue + - javascript.svelte + - javascript.solid + - javascript.ember + - javascript.gatsby --- + + +## Server-Side Usage + +_Import name: `Sentry.openAIIntegration`_ + +The `openAIIntegration` adds instrumentation for the [`openai`](https://www.npmjs.com/package/openai) SDK to capture spans by wrapping OpenAI client calls and recording LLM interactions. + + + -This integration works in the Node.js, Cloudflare Workers, Vercel Edge Functions, and browser runtimes. It requires SDK version `10.2.0` or higher. +Enabled by default and automatically captures spans for OpenAI SDK calls. Requires SDK version `10.28.0` or higher. -_Import name: `Sentry.openAIIntegration`_ + -The `openAIIntegration` adds instrumentation for the `openai` API to capture spans by wrapping OpenAI client calls and recording LLM interactions with configurable input/output recording. + - -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, - }), - ], -}); -``` +Enabled by default and automatically captures spans for OpenAI SDK calls. Requires SDK version `10.28.0` or higher. + +For other runtimes, like the Browser, the instrumentation needs to be manually enabled. See the [setup instructions below](#browser-side-usage). + + - -For Cloudflare Workers, you need to manually instrument the OpenAI client using the `instrumentOpenAiClient` helper: +To customize what data is captured (such as inputs and outputs), see the [Options](#options) in the Configuration section. -```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!" }], -}); -``` +## Browser-Side Usage + +_Import name: `Sentry.instrumentOpenAiClient`_ - - -For Next.js applications, you need to manually instrument the OpenAI client using the `instrumentOpenAiClient` helper: +The `instrumentOpenAiClient` helper adds instrumentation for the `openai` SDK to capture spans by wrapping OpenAI client calls and recording LLM interactions with configurable input/output recording. You need to manually wrap your OpenAI client instance with this helper: ```javascript -import * as Sentry from "@sentry/nextjs"; import OpenAI from "openai"; -const openai = new OpenAI(); +const openai = new OpenAI({ + // Warning: API key will be exposed in browser! + apiKey: 'your-api-key', +}); + const client = Sentry.instrumentOpenAiClient(openai, { recordInputs: true, recordOutputs: true, @@ -97,68 +97,73 @@ const response = await client.chat.completions.create({ }); ``` +To customize what data is captured (such as inputs and outputs), see the [Options](#options) in the Configuration section. + - -For browser applications, you need to manually instrument the OpenAI client using the `instrumentOpenAiClient` helper: +## Configuration -```javascript -import * as Sentry from "@sentry/browser"; -import OpenAI from "openai"; +### Options -const openai = new OpenAI(); -const client = Sentry.instrumentOpenAiClient(openai, { - recordInputs: true, - recordOutputs: true, -}); +The following options control what data is captured from OpenAI SDK calls: -// 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!" }], -}); -``` +#### `recordInputs` - +_Type: `boolean` (optional)_ -## Options +Records inputs to OpenAI SDK calls (such as prompts and messages). -### `recordInputs` +Defaults to `true` if `sendDefaultPii` is `true`. -_Type: `boolean`_ +#### `recordOutputs` -Records inputs to OpenAI API method calls (such as prompts and messages). +_Type: `boolean` (optional)_ + +Records outputs from OpenAI SDK calls (such as generated text and responses). Defaults to `true` if `sendDefaultPii` is `true`. +**Usage** + + + +Using the `openAIIntegration` integration: + ```javascript Sentry.init({ - integrations: [Sentry.openAIIntegration({ recordInputs: true })], + dsn: "____PUBLIC_DSN____", + // Tracing must be enabled for agent monitoring to work + tracesSampleRate: 1.0, + integrations: [ + Sentry.openAIIntegration({ + // your options here + }), + ], }); ``` -### `recordOutputs` - -_Type: `boolean`_ + -Records outputs from OpenAI API method calls (such as generated text and responses). + -Defaults to `true` if `sendDefaultPii` is `true`. +Using the `instrumentOpenAiClient` helper: ```javascript -Sentry.init({ - integrations: [Sentry.openAIIntegration({ recordOutputs: true })], +const client = Sentry.instrumentOpenAiClient(openai, { + // your options here }); ``` -## Configuration + + +## Supported Operations -By default this integration adds tracing support to OpenAI API method calls including: +By default, tracing support is added to the following OpenAI SDK calls: - `chat.completions.create()` - Chat completion requests -- `responses.create()` - Response API requests +- `responses.create()` - Response SDK requests -The integration will automatically detect streaming vs non-streaming requests and handle them appropriately. +Streaming and non-streaming requests are automatically detected and handled appropriately. ## Supported Versions