OpenAI
Learn about using Sentry for OpenAI.
This integration connects Sentry with the OpenAI Python SDK.
Once you've installed this SDK, you can use Sentry AI Agents Monitoring, a Sentry dashboard that helps you understand what's going on with your AI requests.
Sentry AI Monitoring will automatically collect information about prompts, tools, tokens, and models. Learn more about the AI Agents Dashboard.
Install sentry-sdk from PyPI with the openai extra:
pip install "sentry-sdk[openai]"
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.
import sentry_sdk
sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0example-org / example-project",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    #  performance
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    #  performance
    #  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",
    #  profiling
    #  logs
    # Enable logs to be sent to Sentry
    enable_logs=True,
    #  logs
)
Verify that the integration works by making a chat request to OpenAI.
import sentry_sdk
from openai import OpenAI
sentry_sdk.init(...)  # same as above
client = OpenAI(api_key="(your OpenAI key)")
def my_llm_stuff():
    with sentry_sdk.start_transaction(
        name="The result of the AI inference",
        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 (not done in the example above) the data will also show up in the AI Agents Dashboard.
It may take a couple of moments for the data to appear in sentry.io.
- The OpenAI integration will connect Sentry with all supported OpenAI methods automatically. 
- All exceptions leading to an - OpenAIExceptionare reported.
- The supported modules are currently - responses.create,- chat.completions.create, and- embeddings.create.
- Sentry considers LLM and tokenizer inputs/outputs as PII (Personally identifiable information) and doesn't include PII data by default. If you want to include the data, set - send_default_pii=Truein the- sentry_sdk.init()call. To explicitly exclude prompts and outputs despite- send_default_pii=True, configure the integration with- include_prompts=Falseas shown in the Options section below.
By adding OpenAIIntegration to your sentry_sdk.init() call explicitly, you can set options for OpenAIIntegration to change its behavior:
import sentry_sdk
from sentry_sdk.integrations.openai import OpenAIIntegration
sentry_sdk.init(
    # ...
    # Add data like inputs and responses;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    integrations=[
        OpenAIIntegration(
            include_prompts=False,  # LLM/tokenizer inputs/outputs will be not sent to Sentry, despite send_default_pii=True
            tiktoken_encoding_name="cl100k_base",
        ),
    ],
)
You can pass the following keyword arguments to OpenAIIntegration():
- include_prompts:- Whether LLM and tokenizer inputs and outputs should be sent to Sentry. Sentry considers this data personal identifiable data (PII) by default. If you want to include the data, set - send_default_pii=Truein 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 installed and specify the - tiktoken_encoding_namethat you use for tokenization. See the OpenAI Cookbook for possible values.- The default is - None.
- OpenAI: 1.0+
- tiktoken: 0.6.0+
- Python: 3.9+
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").