|
| 1 | +--- |
| 2 | +title: Anthropic |
| 3 | +description: "Learn about using Sentry for Anthropic." |
| 4 | +--- |
| 5 | + |
| 6 | +This integration connects Sentry with the [Anthropic Python SDK](https://github.com/anthropics/anthropic-sdk-python). |
| 7 | + |
| 8 | +Once you've installed this SDK, you can use the Sentry AI Agents Monitoring, a Sentry dashboard that helps you understand what's going on with your AI requests. |
| 9 | + |
| 10 | +Sentry AI Monitoring will automatically collect information about prompts, tools, tokens, and models. Learn more about the [AI Agents Dashboard](/product/insights/ai/agents). |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +Install `sentry-sdk` from PyPI with the `anthropic` extra: |
| 15 | + |
| 16 | +```bash {tabTitle:pip} |
| 17 | +pip install "sentry-sdk[anthropic]" |
| 18 | +``` |
| 19 | + |
| 20 | +```bash {tabTitle:uv} |
| 21 | +uv add "sentry-sdk[anthropic]" |
| 22 | +``` |
| 23 | + |
| 24 | +## Configure |
| 25 | + |
| 26 | +If you have the `anthropic` package in your dependencies, the Anthropic integration will be enabled automatically when you initialize the Sentry SDK. |
| 27 | + |
| 28 | +<PlatformContent includePath="getting-started-config" /> |
| 29 | + |
| 30 | +## Verify |
| 31 | + |
| 32 | +Verify that the integration works by making a chat request to Anthropic. |
| 33 | + |
| 34 | +```python |
| 35 | +import sentry_sdk |
| 36 | +from anthropic import Anthropic |
| 37 | + |
| 38 | +sentry_sdk.init(...) # same as above |
| 39 | + |
| 40 | +client = Anthropic(api_key="(your Anthropic key)") |
| 41 | + |
| 42 | +def my_llm_stuff(): |
| 43 | + with sentry_sdk.start_transaction(name="The result of the AI inference"): |
| 44 | + print( |
| 45 | + client.messages.create( |
| 46 | + model="claude-3-5-sonnet-20240620", |
| 47 | + max_tokens=1024, |
| 48 | + messages=[{"role": "user", "content": "say hello"}] |
| 49 | + ) |
| 50 | + .content[0] |
| 51 | + .text |
| 52 | + ) |
| 53 | +``` |
| 54 | + |
| 55 | +After running this script, the resulting data should show up in the `AI Spans` tab on the `Explore > "races > Trace` page on Sentry.io. |
| 56 | + |
| 57 | +If you manually created an <PlatformLink to="/tracing/instrumentation/custom-instrumentation/ai-agents-module/#invoke-agent-span">Invoke Agent Span</PlatformLink> (not done in the example above) the data will also show up in the [AI Agents Dashboard](/product/insights/ai/agents). |
| 58 | + |
| 59 | +It may take a couple of moments for the data to appear in [sentry.io](https://sentry.io). |
| 60 | + |
| 61 | +## Behavior |
| 62 | + |
| 63 | +- The Anthropic integration will connect Sentry with the supported Anthropic methods automatically. |
| 64 | + |
| 65 | +- The supported function is currently `messages.create` (both sync and async). |
| 66 | + |
| 67 | +- Sentry considers LLM 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=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` as shown in the [Options section](#options) below. |
| 68 | + |
| 69 | +## Options |
| 70 | + |
| 71 | +By adding `AnthropicIntegration` to your `sentry_sdk.init()` call explicitly, you can set options for `AnthropicIntegration` to change its behavior: |
| 72 | + |
| 73 | +```python |
| 74 | +import sentry_sdk |
| 75 | +from sentry_sdk.integrations.anthropic import AnthropicIntegration |
| 76 | + |
| 77 | +sentry_sdk.init( |
| 78 | + # ... |
| 79 | + # Add data like inputs and responses; |
| 80 | + # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info |
| 81 | + send_default_pii=True, |
| 82 | + integrations=[ |
| 83 | + AnthropicIntegration( |
| 84 | + include_prompts=False, # LLM inputs/outputs will be not sent to Sentry, despite send_default_pii=True |
| 85 | + ), |
| 86 | + ], |
| 87 | +) |
| 88 | +``` |
| 89 | + |
| 90 | +You can pass the following keyword arguments to `AnthropicIntegration()`: |
| 91 | + |
| 92 | +- `include_prompts`: |
| 93 | + |
| 94 | + Whether LLM 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=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`. |
| 95 | + |
| 96 | + The default is `True`. |
| 97 | + |
| 98 | +## Supported Versions |
| 99 | + |
| 100 | +- Anthropic: 0.16.0+ |
| 101 | +- Python: 3.8+ |
0 commit comments