Anthropic
Beta
The support for Anthropic is in its beta phase.
We are working on supporting different AI libraries (see GitHub discussion).
If you want to try the beta features and are willing to give feedback, please let us know on Discord.
This integration connects Sentry with the Anthropic Python SDK and works with Anthropic versions 0.16.0
and above.
Install sentry-sdk
from PyPI with the anthropic
extra:
pip install --upgrade 'sentry-sdk[anthropic]'
Add AnthropicIntegration()
to your integrations
in the sentry_sdk.init
call:
from anthropic import Anthropic
import sentry_sdk
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
enable_tracing=True,
traces_sample_rate=1.0,
integrations=[AnthropicIntegration()],
)
client = Anthropic()
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:
from anthropic import Anthropic
import sentry_sdk
sentry_sdk.init(...) # same as above
client = Anthropic(api_key="invalid-key")
with sentry_sdk.start_transaction(op="ai-inference", name="AI inference"):
response = (
client.messages.create(
max_tokens=42,
model="some-model",
messages=[{"role": "system", "content": "Hello, Anthropic!"}]
)
)
print(response)
Executing this script initiates a transaction, which will show up in the Performance section of sentry.io. Furthermore, it also sends Sentry an error event (about the bad API key), which will be linked to the transaction.
It takes a couple of moments for the data to appear in sentry.io.
The supported modules are currently
chat.messages.create
withstream=True
andstream=False
.All exceptions leading to an
AnthropicError
are reported.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 thesentry_sdk.init()
call. To explicitly exclude prompts despitesend_default_pii=True
, configure the integration withinclude_prompts=False
, as shown in Options.
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
.
import sentry_sdk
from sentry_sdk.integrations.anthropic import AnthropicIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
enable_tracing=True,
send_default_pii=True,
traces_sample_rate=1.0,
integrations=[
AnthropicIntegration(
include_prompts=False, # Exclude prompts from being sent to Sentry, despite send_default_pii=True
),
],
)
- Anthropic: 0.16.0+
- Python: 3.7+
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").
- Package:
- pypi:sentry-sdk
- Version:
- 2.1.0
- Repository:
- https://github.com/getsentry/sentry-python
- API Documentation:
- https://getsentry.github.io/sentry-python/