Anthropic for Python
Learn about using Sentry for Anthropic.
This integration connects Sentry with the Anthropic Python SDK.
Once you've installed this SDK, you can use the Sentry Agents Tracing, a Sentry dashboard that helps you understand what's going on with your AI requests.
Sentry AI Observability will automatically collect information about prompts, tools, tokens, and models. Learn more about the Agents Dashboard.
If you're using stream mode, this page's references to "transaction" should be applied to service spans instead. See Streamed Spans for more information.
Install sentry-sdk from PyPI:
pip install sentry-sdk
pip install sentry-sdk
uv add sentry-sdk
If you have the anthropic package in your dependencies, the Anthropic integration will be enabled automatically when you initialize the Sentry SDK.
import sentry_sdk
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"user_info": True,
},
# ___PRODUCT_OPTION_START___ performance
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# ___PRODUCT_OPTION_END___ performance
# ___PRODUCT_OPTION_START___ 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",
# ___PRODUCT_OPTION_END___ profiling
)
import sentry_sdk
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"user_info": True,
},
# ___PRODUCT_OPTION_START___ performance
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# ___PRODUCT_OPTION_END___ performance
# ___PRODUCT_OPTION_START___ 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",
# ___PRODUCT_OPTION_END___ profiling
)
Verify that the integration works by making a chat request to Anthropic.
import sentry_sdk
from anthropic import Anthropic
sentry_sdk.init(...) # same as above
client = Anthropic(api_key="(your Anthropic key)")
def my_llm_stuff():
# or sentry_sdk.traces.start_span(name="The result of the AI inference", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="The result of the AI inference"):
print(
client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1024,
messages=[{"role": "user", "content": "say hello"}]
)
.content[0]
.text
)
import sentry_sdk
from anthropic import Anthropic
sentry_sdk.init(...) # same as above
client = Anthropic(api_key="(your Anthropic key)")
def my_llm_stuff():
# or sentry_sdk.traces.start_span(name="The result of the AI inference", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="The result of the AI inference"):
print(
client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1024,
messages=[{"role": "user", "content": "say hello"}]
)
.content[0]
.text
)
After running this script, the resulting data should show up in the AI Spans tab on the Explore > Traces > Trace 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 Agents Dashboard.
It may take a couple of moments for the data to appear in sentry.io.
The Anthropic integration will connect Sentry with the supported Anthropic methods automatically.
The supported function is currently
messages.create(both sync and async).Sentry considers LLM inputs/outputs as PII (Personally identifiable information) and doesn't include prompts, messages, or responses by default. Tool definitions are still attached by default; setting
inputstoFalsein thegen_aikey described below removes them. To include prompts, messages, and responses, use thegen_aikey of thedata_collectionoption, which lets you control inputs and outputs separately. When you pass adata_collectiondictionary, both are collected by default andinclude_promptsis ignored:Copiedsentry_sdk.init( # ... data_collection={ # Record LLM inputs, but not outputs "gen_ai": {"inputs": True, "outputs": False}, }, )sentry_sdk.init( # ... data_collection={ # Record LLM inputs, but not outputs "gen_ai": {"inputs": True, "outputs": False}, }, )Alternatively, set
send_default_pii=Truein thesentry_sdk.init()call. To explicitly exclude prompts and outputs despitesend_default_pii=True, configure the integration withinclude_prompts=Falseas shown in the Options section below.send_default_piiwill be removed in version 3.0 of the SDK.
By adding AnthropicIntegration to your sentry_sdk.init() call explicitly, you can set options for AnthropicIntegration to change its behavior:
import sentry_sdk
from sentry_sdk.integrations.anthropic import AnthropicIntegration
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=[
AnthropicIntegration(
include_prompts=False, # LLM inputs/outputs will be not sent to Sentry, despite send_default_pii=True
),
],
)
import sentry_sdk
from sentry_sdk.integrations.anthropic import AnthropicIntegration
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=[
AnthropicIntegration(
include_prompts=False, # LLM inputs/outputs will be not sent to Sentry, despite send_default_pii=True
),
],
)
You can pass the following keyword arguments to AnthropicIntegration():
include_prompts:Whether LLM inputs and outputs should be sent to Sentry. If you use the
data_collectionoption, this option is ignored. Use thegen_aikey instead.Sentry considers this data personal identifiable data (PII) by default. If you want to include the data, set
send_default_pii=Truein thesentry_sdk.init()call. To explicitly exclude prompts and outputs despitesend_default_pii=True, configure the integration withinclude_prompts=False.send_default_piiwill be removed in version 3.0 of the SDK.The default is
True.
- Anthropic: 0.16.0+
- Python: 3.8+
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").