LangChain for Next.js
Adds instrumentation for LangChain. (default)
Requires SDK version 11.0.0 or higher. On earlier versions, see the Agent Tracing 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.
The langChainIntegration adds instrumentation for langchain to capture agent tracing gen_ai spans, recording model, token usage, latency, and (when enabled) inputs and outputs.
Sentry.init({
dsn: "___PUBLIC_DSN___",
// Tracing must be enabled for agent tracing to work
tracesSampleRate: 1.0,
integrations: [Sentry.langChainIntegration()],
});
Sentry.init({
dsn: "___PUBLIC_DSN___",
// Tracing must be enabled for agent tracing to work
tracesSampleRate: 1.0,
integrations: [Sentry.langChainIntegration()],
});
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
TypeSafeClassifierruns
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
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
Available since: v11.7.0
@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 node, or through the modelRouterMiddleware and autoModeMiddleware of @langchain/typesafe/middleware inside a createAgent run. No additional integration or setup is needed.
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);
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.decide span named decide <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 and 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_decide() instead, see the TypeSafe or Vercel AI integration.
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.
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.
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.
Sentry.init({
dsn: "___PUBLIC_DSN___",
tracesSampleRate: 1.0,
integrations: [
Sentry.langChainIntegration({
recordInputs: false,
recordOutputs: false,
}),
],
});
Sentry.init({
dsn: "___PUBLIC_DSN___",
tracesSampleRate: 1.0,
integrations: [
Sentry.langChainIntegration({
recordInputs: false,
recordOutputs: false,
}),
],
});
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").