|
| 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) and works with Anthropic versions `0.16.0` and above. |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +Install `sentry-sdk` from PyPI with the `anthropic` extra: |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install --upgrade 'sentry-sdk[anthropic]' |
| 14 | +``` |
| 15 | + |
| 16 | +## Configure |
| 17 | + |
| 18 | +Add `AnthropicIntegration()` to your `integrations` in the `sentry_sdk.init` call: |
| 19 | + |
| 20 | +```python |
| 21 | +from anthropic import Anthropic |
| 22 | + |
| 23 | +import sentry_sdk |
| 24 | + |
| 25 | +sentry_sdk.init( |
| 26 | + dsn="___PUBLIC_DSN___", |
| 27 | + enable_tracing=True, |
| 28 | + traces_sample_rate=1.0, |
| 29 | + integrations=[AnthropicIntegration()], |
| 30 | +) |
| 31 | + |
| 32 | +client = Anthropic() |
| 33 | +``` |
| 34 | + |
| 35 | +## Verify |
| 36 | + |
| 37 | +Verify that the integration works by adding in a bug (such as a bad API key) to your code. This will cause a new error event to be sent to Sentry: |
| 38 | + |
| 39 | +```python |
| 40 | +from anthropic import Anthropic |
| 41 | +import sentry_sdk |
| 42 | + |
| 43 | +sentry_sdk.init(...) # same as above |
| 44 | + |
| 45 | +client = Anthropic(api_key="invalid-key") |
| 46 | +with sentry_sdk.start_transaction(op="ai-inference", name="AI inference"): |
| 47 | + response = ( |
| 48 | + client.messages.create( |
| 49 | + max_tokens=42, |
| 50 | + model="some-model", |
| 51 | + messages=[{"role": "system", "content": "Hello, Anthropic!"}] |
| 52 | + ) |
| 53 | + ) |
| 54 | + print(response) |
| 55 | +``` |
| 56 | + |
| 57 | +Executing this script initiates a transaction, which will show up in the Performance section of [sentry.io](https://sentry.io). Furthermore, it also sends Sentry an error event (about the bad API key), which will be linked to the transaction. |
| 58 | + |
| 59 | +It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io). |
| 60 | + |
| 61 | +## Behavior |
| 62 | + |
| 63 | +- The supported modules are currently `chat.messages.create` with `stream=True` and `stream=False`. |
| 64 | + |
| 65 | +- All exceptions leading to an `AnthropicError` are reported. |
| 66 | + |
| 67 | +- Sentry is configured not to consider LLM and tokenizer inputs/outputs as PII. If you want to include them, set `send_default_pii=True` in the `sentry_sdk.init()` call. To explicitly exclude prompts despite `send_default_pii=True`, configure the integration with `include_prompts=False`, as shown in [Options](#options). |
| 68 | + |
| 69 | +## Options |
| 70 | + |
| 71 | +The `AnthropicIntegration` takes an optional `include_prompts` parameter. If set to `False`, prompts are excluded from being sent to Sentry, despite `send_default_pii=True`. |
| 72 | + |
| 73 | +```python |
| 74 | +import sentry_sdk |
| 75 | +from sentry_sdk.integrations.anthropic import AnthropicIntegration |
| 76 | + |
| 77 | +sentry_sdk.init( |
| 78 | + dsn="___PUBLIC_DSN___", |
| 79 | + enable_tracing=True, |
| 80 | + send_default_pii=True, |
| 81 | + traces_sample_rate=1.0, |
| 82 | + integrations=[ |
| 83 | + AnthropicIntegration( |
| 84 | + include_prompts=False, # Exclude prompts from being sent to Sentry, despite send_default_pii=True |
| 85 | + ), |
| 86 | + ], |
| 87 | +) |
| 88 | +``` |
| 89 | + |
| 90 | +## Supported Versions |
| 91 | + |
| 92 | +- Anthropic: 0.16.0+ |
| 93 | +- Python: 3.7+ |
0 commit comments