---
title: "LangChain"
description: "Adds instrumentation for LangChain. (default)"
url: https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain/
---

# LangChain for Mastra

Requires SDK version `11.0.0` or higher. On earlier versions, see the [Agent Tracing](https://docs.sentry.io/platforms/javascript/agent-tracing.md) setup for your SDK.

*Import name: `Sentry.langChainIntegration`*

This integration is enabled by default when tracing is enabled. If you'd like to modify your default integrations, read [this](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations.md#modifying-default-integrations).

The `langChainIntegration` adds instrumentation for [`langchain`](https://www.npmjs.com/package/langchain) to capture [agent tracing](https://docs.sentry.io/platforms/javascript/guides/mastra/agent-tracing.md) `gen_ai` spans, recording model, token usage, latency, and (when enabled) inputs and outputs.

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  // Tracing must be enabled for agent tracing to work
  tracesSampleRate: 1.0,
  integrations: [Sentry.langChainIntegration()],
});
```

## [Supported Operations](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain.md#supported-operations)

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

* **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
* **Classifier evaluations** - Captures spans for `TypeSafeClassifier` runs

The integration automatically instruments the `invoke()`, `stream()`, and `batch()` runnable methods, across the following LangChain provider packages:

* `@langchain/anthropic`
* `@langchain/openai`
* `@langchain/google-genai`
* `@langchain/mistralai`
* `@langchain/google-vertexai`
* `@langchain/groq`

### [Embeddings](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain.md#embeddings)

The integration captures `gen_ai.embeddings` spans for `embedQuery()` and `embedDocuments()` calls from these provider packages:

* `@langchain/openai`
* `@langchain/google-genai`
* `@langchain/mistralai`
* `@langchain/google-vertexai`

## [Trace Classifier Evaluations](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain.md#trace-classifier-evaluations)

Available since: `v11.6.0`

[`@langchain/typesafe`](https://www.npmjs.com/package/@langchain/typesafe)'s `TypeSafeClassifier` asks TypeSafe's Jev model typed questions about your input. The `langChainIntegration` traces these calls for you, whether you call the classifier yourself, from a [LangGraph](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langgraph.md#trace-classifier-evaluations) node, or through the `modelRouterMiddleware` and `autoModeMiddleware` of `@langchain/typesafe/middleware` inside a `createAgent` run. No additional integration or setup is needed.

```javascript
import { TypeSafeClassifier } from "@langchain/typesafe";

const classifier = new TypeSafeClassifier({
  questions: {
    department: {
      type: "choice",
      instructions: "Which team should handle this?",
      criteria: {
        billing: "Charges and refunds",
        technical: "Bugs and outages",
      },
    },
  },
});

const result = await classifier.invoke(
  "I was charged twice. Please refund the extra charge.",
);

console.log(result.choices.department.choice);
```

Each `invoke()` call creates a `gen_ai.evaluate` span named `evaluate <model>`, with the model, provider, latency, and token usage. Inside an agent, the span also records the agent name. If the evaluation fails, the span is marked as failed. When recording is enabled, Sentry captures the `state` and `questions` sent to Jev as input messages and `answers` as output messages, including probabilities returned by the model. LangChain messages in the input are recorded as transcript lines, the same way the classifier sends them.

Evaluation inputs and outputs follow the same [`recordInputs`](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain.md#recordinputs) and [`recordOutputs`](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain.md#recordoutputs) options as other LangChain calls. Disable these options if evaluation state or answers contain sensitive data.

If you call Jev through `TypeSafeClient.systemOne()` or Vercel AI SDK's `experimental_evaluate()` instead, see the [TypeSafe](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/typesafe.md) or [Vercel AI](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/vercelai.md#trace-jev-evaluations) integration.

## [Supported Versions](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain.md#supported-versions)

* `langchain`: `>=0.1.0 <2.0.0`

Automatic embeddings instrumentation supports these package versions:

| Package                    | Supported Versions |
| -------------------------- | ------------------ |
| `@langchain/openai`        | `>=0.1.0 <2.0.0`   |
| `@langchain/google-genai`  | `>=0.1.0 <3.0.0`   |
| `@langchain/mistralai`     | `>=0.1.0 <2.0.0`   |
| `@langchain/google-common` | `>=0.1.0 <3.0.0`   |

For `@langchain/google-vertexai`, the version requirement applies to its `@langchain/google-common` dependency, which implements the embedding methods.

Classifier evaluation tracing supports `@langchain/typesafe` `>=0.0.1 <1.0.0`.

## [Options](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain.md#options)

### [`recordInputs`](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain.md#recordinputs)

*Type: `boolean` (optional)*

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

Defaults to `true` if `dataCollection.genAI.inputs` is `true`, which is the default.

### [`recordOutputs`](https://docs.sentry.io/platforms/javascript/guides/mastra/configuration/integrations/langchain.md#recordoutputs)

*Type: `boolean` (optional)*

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

Defaults to `true` if `dataCollection.genAI.outputs` is `true`, which is the default.

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  tracesSampleRate: 1.0,
  integrations: [
    Sentry.langChainIntegration({
      recordInputs: false,
      recordOutputs: false,
    }),
  ],
});
```
