LangChain
Learn about using Sentry for LangChain.
This integration connects Sentry with LangChain in Python.
Once you've installed this SDK, you can use Sentry AI Agents Monitoring, a Sentry dashboard that helps you understand what's going on with your AI requests. Sentry AI Monitoring will automatically collect information about prompts, tools, tokens, and models. Learn more about the AI Agents Dashboard.
Install sentry-sdk
from PyPI with the langchain
extra:
pip install "sentry-sdk[langchain]"
If you have the langchain
package in your dependencies, the LangChain integration will be enabled automatically when you initialize the Sentry SDK. For correct token accounting, you need to disable the integration for the model provider you are using (e.g. OpenAI or Anthropic).
import sentry_sdk
from sentry_sdk.integrations.langchain import LangchainIntegration
from sentry_sdk.integrations.openai import OpenAIIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
environment="local",
traces_sample_rate=1.0,
send_default_pii=True,
debug=True,
integrations=[
LangchainIntegration(),
],
disabled_integrations=[OpenAIIntegration()],
)
Verify that the integration works by initializing a transaction and invoking an agent. In these examples, we're providing a function tool to roll a die.
import random
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain.chat_models import init_chat_model
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.tools import tool
@tool
def roll_die(sides: int = 6) -> str:
"""Roll a die with a given number of sides"""
return f"Rolled a {random.randint(1, sides)} on a {sides}-sided die."
with sentry_sdk.start_transaction(name="langchain-openai"):
model = init_chat_model(
"gpt-4o-mini",
model_provider="openai",
model_kwargs={"stream_options": {"include_usage": True}},
)
tools = [roll_die]
prompt = ChatPromptTemplate.from_messages(
[
SystemMessage(
content="Greet the user and use the die roll tool. Do not terminate before using the tool."
),
HumanMessage(content="{input}"),
MessagesPlaceholder("agent_scratchpad"),
]
)
agent = create_openai_functions_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = agent_executor.invoke(
{
"input": "Hello, my name is Alice! Please roll a six-sided die.",
"chat_history": [],
}
)
print(result)
After running this script, the resulting data should show up in the "AI Spans"
tab on the "Explore" > "Traces"
page on Sentry.io, and in the AI Agents Dashboard.
It may take a couple of moments for the data to appear in sentry.io.
The LangChain integration will connect Sentry with all supported LangChain methods automatically.
All exceptions are reported.
Sentry considers LLM and tokenizer 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 thesentry_sdk.init()
call. To explicitly exclude prompts and outputs despitesend_default_pii=True
, configure the integration withinclude_prompts=False
as shown in the Options section below.
By adding LangchainIntegration
to your sentry_sdk.init()
call explicitly, you can set options for LangchainIntegration
to change its behavior:
import sentry_sdk
from sentry_sdk.integrations.langchain import LangchainIntegration
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=[
LangchainIntegration(
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 LangchainIntegration()
:
include_prompts
Whether LLM and tokenizer 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 thesentry_sdk.init()
call. To explicitly exclude prompts and outputs despitesend_default_pii=True
, configure the integration withinclude_prompts=False
.The default is
True
.
- OpenAI: 1.0+
- Python: 3.9+
- langchain: 0.1.11+
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").