|
| 1 | +--- |
| 2 | +title: OpenAI Agents |
| 3 | +description: "Learn about using Sentry for OpenAI Agents SDK." |
| 4 | +--- |
| 5 | + |
| 6 | +<Alert title="Beta"> |
| 7 | + |
| 8 | +The support for **OpenAI Agents SDK** is in its beta phase. Please test locally before using in production. |
| 9 | + |
| 10 | +</Alert> |
| 11 | + |
| 12 | +This integration connects Sentry with the [OpenAI Python SDK](https://openai.github.io/openai-agents-python/). |
| 13 | +The integration has been confirmed to work with OpenAI Agents version 0.0.19. |
| 14 | + |
| 15 | +Once you've installed this SDK, you can use [Sentry AI Agents Insights](https://sentry.io/orgredirect/organizations/:orgslug/insights/agents/), a Sentry dashboard that helps you understand what's going on with your AI agents. |
| 16 | + |
| 17 | +Sentry AI Agents monitoring will automatically collect information about agents, tools, prompts, tokens, and models. |
| 18 | + |
| 19 | +## Install |
| 20 | + |
| 21 | +Install `sentry-sdk` from PyPI: |
| 22 | + |
| 23 | +```bash {tabTitle:pip} |
| 24 | +pip install "sentry-sdk" |
| 25 | +``` |
| 26 | + |
| 27 | +```bash {tabTitle:uv} |
| 28 | +uv add "sentry-sdk" |
| 29 | +``` |
| 30 | + |
| 31 | +## Configure |
| 32 | + |
| 33 | +Add `OpenAIAgentsIntegration()` to your `integrations` list: |
| 34 | + |
| 35 | +```python |
| 36 | +import sentry_sdk |
| 37 | +from sentry_sdk.integrations.openai_agents import OpenAIAgentsIntegration |
| 38 | + |
| 39 | +sentry_sdk.init( |
| 40 | + dsn="___PUBLIC_DSN___", |
| 41 | + # Add data like inputs and responses to/from LLMs and tools; |
| 42 | + # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info |
| 43 | + send_default_pii=True, |
| 44 | + integrations=[ |
| 45 | + OpenAIAgentsIntegration(), |
| 46 | + ], |
| 47 | +) |
| 48 | +``` |
| 49 | + |
| 50 | +## Verify |
| 51 | + |
| 52 | +Verify that the integration works by running an AI agent. The resulting data should show up in your AI Agents Insights dashboard. |
| 53 | + |
| 54 | +```python |
| 55 | +import asyncio |
| 56 | +import random |
| 57 | + |
| 58 | +import sentry_sdk |
| 59 | +from sentry_sdk.integrations.openai_agents import OpenAIAgentsIntegration |
| 60 | + |
| 61 | +import agents |
| 62 | +from pydantic import BaseModel # installed by openai-agents |
| 63 | + |
| 64 | +@agents.function_tool |
| 65 | +def random_number(max: int) -> int: |
| 66 | + return random.randint(0, max) |
| 67 | + |
| 68 | +class FinalResult(BaseModel): |
| 69 | + number: int |
| 70 | + |
| 71 | +random_number_agent = agents.Agent( |
| 72 | + name="Random Number Agent", |
| 73 | + instructions="Generate a random number.", |
| 74 | + tools=[random_number, ], |
| 75 | + output_type=FinalResult, |
| 76 | + model="gpt-4o-mini", |
| 77 | +) |
| 78 | + |
| 79 | +async def main() -> None: |
| 80 | + sentry_sdk.init( |
| 81 | + dsn="...", # same as above |
| 82 | + # Add data like LLM and tool inputs/outputs; |
| 83 | + # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info |
| 84 | + send_default_pii=True, |
| 85 | + integrations=[ |
| 86 | + OpenAIAgentsIntegration(), |
| 87 | + ], |
| 88 | + ) |
| 89 | + |
| 90 | + await agents.Runner.run( |
| 91 | + random_number_agent, |
| 92 | + input=f"Generate a random number between 0 and {10}.", |
| 93 | + ) |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + asyncio.run(main()) |
| 97 | +``` |
| 98 | + |
| 99 | +It may take a couple of moments for the data to appear in [sentry.io](https://sentry.io). |
| 100 | + |
| 101 | +## Behavior |
| 102 | + |
| 103 | +Data on the following will be collected: |
| 104 | + |
| 105 | +- AI agents invocations |
| 106 | +- execution of tools |
| 107 | +- number of input and output tokens used |
| 108 | +- LLM models usage |
| 109 | + |
| 110 | +Sentry considers LLM and tool inputs/outputs as PII and doesn't include PII data by default. If you want to include the data, set `send_default_pii=True` in the `sentry_sdk.init()` call. |
| 111 | + |
| 112 | +## Supported Versions |
| 113 | + |
| 114 | +- OpenAI Agents SDK: 0.0.19+ |
| 115 | +- Python: 3.9+ |
0 commit comments