---
title: "OpenAI"
description: "Learn about using Sentry for OpenAI."
url: https://docs.sentry.io/platforms/python/agent-tracing/openai/
---

# OpenAI for Python

This integration connects Sentry with the [OpenAI Python SDK](https://github.com/openai/openai-python).

Once you've installed this SDK, you can use Sentry Agents Tracing, a Sentry dashboard that helps you understand what's going on with your AI requests.

Sentry AI Observability will automatically collect information about prompts, tools, tokens, and models. Learn more about the [Agents Dashboard](https://docs.sentry.io/product/agents/dashboards.md).

##### Latest Features

AI frameworks are moving fast, and so are our integrations. To get the most out of our Agents Tracing dashboard, use Python SDK version 2.41.0 or later.

In SDK `3.x`, and in stream mode in SDK `2.x`, this page's references to "transaction" apply to service spans instead. See [Streamed Spans](https://docs.sentry.io/platforms/python/tracing/streamed-spans.md) for more information.

## [Install](https://docs.sentry.io/platforms/python/agent-tracing/openai.md#install)

Install `sentry-sdk` from PyPI:

```bash
pip install sentry-sdk
```

*Other available variations of the above snippet: uv*

## [Configure](https://docs.sentry.io/platforms/python/agent-tracing/openai.md#configure)

If you have the `openai` package in your dependencies, the OpenAI integration will be enabled automatically when you initialize the Sentry SDK.

An additional dependency, `tiktoken`, is required if you want to calculate token usage for streaming chat responses.

Error Monitoring\[ ]Tracing\[ ]Profiling

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    # Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
    data_collection={
        "user_info": True,
    },
    # ___PRODUCT_OPTION_START___ performance
    # Set traces_sample_rate to 1.0 to capture 100%
    # of service spans for tracing.
    traces_sample_rate=1.0,
    # ___PRODUCT_OPTION_END___ performance
    # ___PRODUCT_OPTION_START___ profiling
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be automatically collected while
    # there is an active span.
    profile_lifecycle="trace",
    # ___PRODUCT_OPTION_END___ profiling
)
```

## [Verify](https://docs.sentry.io/platforms/python/agent-tracing/openai.md#verify)

Verify that the integration works by making a chat request to OpenAI.

```python
import sentry_sdk
from openai import OpenAI

sentry_sdk.init(...)  # same as above

client = OpenAI(api_key="(your OpenAI key)")

def my_llm_stuff():
    # In SDK 2.x, use sentry_sdk.start_transaction(name=..., op="ai-inference") instead
    with sentry_sdk.start_span(
        name="The result of the AI inference",
        attributes={"sentry.op": "ai-inference"},
    ):
      print(
          client.chat.completions.create(
              model="gpt-3.5", messages=[{"role": "system", "content": "say hello"}]
          )
          .choices[0]
          .message.content
      )
```

After running this script, the resulting data should show up in the `"AI Spans"` tab on the `"Explore" > "Traces"` page on Sentry.io.

If you manually created an [Invoke Agent Span](https://docs.sentry.io/platforms/python/agent-tracing/manual-instrumentation.md#invoke-agent-span) (not done in the example above) the data will also show up in the [Agents Dashboard](https://docs.sentry.io/product/agents/dashboards.md).

It may take a couple of moments for the data to appear in [sentry.io](https://sentry.io).

## [Behavior](https://docs.sentry.io/platforms/python/agent-tracing/openai.md#behavior)

* The OpenAI integration will connect Sentry with all supported OpenAI methods automatically.

* All exceptions leading to an `OpenAIException` are reported.

* The supported modules are currently `responses.create`, `chat.completions.create`, and `embeddings.create`.

* By default, the SDK records LLM and tokenizer inputs and outputs, such as prompts, messages, responses, and tool definitions. To control this, use the `gen_ai` key of the [`data_collection` option](https://docs.sentry.io/platforms/python/configuration/options.md#data_collection), which lets you control inputs and outputs separately:

  ```python
  sentry_sdk.init(
      # ...
      data_collection={
          # Record LLM inputs, but not outputs
          "gen_ai": {"inputs": True, "outputs": False},
      },
  )
  ```

  In SDK versions prior to `3.0`, Sentry considers LLM and tokenizer inputs and outputs as PII and doesn't include prompts, messages, or responses unless you pass a `data_collection` dictionary or set `send_default_pii=True`. With `send_default_pii=True`, you can exclude them with the integration's `include_prompts=False` option, as described in the [Options section](https://docs.sentry.io/platforms/python/agent-tracing/openai.md#options) below.

## [Options](https://docs.sentry.io/platforms/python/agent-tracing/openai.md#options)

By adding `OpenAIIntegration` to your `sentry_sdk.init()` call explicitly, you can set options for `OpenAIIntegration` to change its behavior:

```python
import sentry_sdk
from sentry_sdk.integrations.openai import OpenAIIntegration

sentry_sdk.init(
    # ...
    integrations=[
        OpenAIIntegration(
            tiktoken_encoding_name="cl100k_base",
        ),
    ],
)
```

You can pass the following keyword arguments to `OpenAIIntegration()`:

* `include_prompts`:

  Only available in SDK versions prior to `3.0`. In SDK `3.x`, use the `gen_ai` key of the `data_collection` option instead.

  Whether LLM and tokenizer inputs and outputs should be sent to Sentry. If you use the [`data_collection` option](https://docs.sentry.io/platforms/python/configuration/options.md#data_collection), this option is ignored. Use the `gen_ai` key instead.

  Sentry considers this data personal identifiable data (PII) by default. If you want to include the data, set `send_default_pii=True` in the `sentry_sdk.init()` call. To explicitly exclude prompts and outputs despite `send_default_pii=True`, configure the integration with `include_prompts=False`.

  The default is `True`.

* `tiktoken_encoding_name`:

  If you want to calculate token usage for streaming chat responses you need to have an additional dependency, [tiktoken](https://pypi.org/project/tiktoken/) installed and specify the `tiktoken_encoding_name` that you use for tokenization. See the [OpenAI Cookbook](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for possible values.

  The default is `None`.

## [Supported Versions](https://docs.sentry.io/platforms/python/agent-tracing/openai.md#supported-versions)

* OpenAI: 1.0+
* tiktoken: 0.3.0+
* Python: 3.9+
* Sentry Python SDK 2.41.0+
