LangChain

Adds instrumentation for LangChain.

Import name: Sentry.createLangChainCallbackHandler

The createLangChainCallbackHandler helper adds instrumentation for langchain to capture spans by creating a callback handler that wraps LangChain operations and records AI agent interactions with configurable input/output recording. You need to manually create and pass this callback handler to your LangChain operations. See example below:

Copied
import { ChatAnthropic } from "@langchain/anthropic";

// Create a LangChain callback handler
const callbackHandler = Sentry.createLangChainCallbackHandler({
  recordInputs: true,
  recordOutputs: true,
});

// Use with chat models
const model = new ChatAnthropic({
  model: "claude-3-5-sonnet-20241022",
  apiKey: "your-api-key", // Warning: API key will be exposed in browser!
});

await model.invoke("Tell me a joke", {
  callbacks: [callbackHandler],
});

To customize what data is captured (such as inputs and outputs), see the Options in the Configuration section.

The following options control what data is captured from LangChain operations:

Type: boolean (optional)

Records inputs to LangChain operations (such as prompts and messages).

Defaults to true if sendDefaultPii is true.

Type: boolean (optional)

Records outputs from LangChain operations (such as generated text and responses).

Defaults to true if sendDefaultPii is true.

Usage

Using the createLangChainCallbackHandler helper:

Copied
const callbackHandler = Sentry.createLangChainCallbackHandler({
  // your options here
});

By default, tracing support is added to the following LangChain SDK calls:

  • Chat model invocations - Captures spans for chat model calls
  • LLM invocations - Captures spans for LLM pipeline executions
  • Chain executions - Captures spans for chain invocations
  • Tool executions - Captures spans for tool calls

The integration automatically instruments the following LangChain runnable methods:

  • invoke() - Single execution
  • stream() - Streaming execution
  • batch() - Batch execution

The automatic instrumentation supports the following LangChain provider packages:

  • @langchain/anthropic
  • @langchain/openai
  • @langchain/google-genai
  • @langchain/mistralai
  • @langchain/google-vertexai
  • @langchain/groq

  • langchain: >=0.1.0 <2.0.0
Was this helpful?
Help improve this content
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").