LangGraph | Sentry for Next.js
Source URL: https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/integrations/langgraph
LangGraph | Sentry for Next.js
Section titled “LangGraph | Sentry for Next.js”For meta-framework applications using all runtimes, you need to manually wrap your compiled graph with instrumentLangGraph. See instructions in the Browser-Side Usage section.
Import name: Sentry.instrumentLangGraph
The instrumentLangGraph helper adds instrumentation for @langchain/langgraph to capture spans by wrapping a compiled LangGraph graph and recording AI agent interactions with configurable input/output recording. You need to manually wrap your compiled graph with this helper. See example below:
import { ChatOpenAI } from "@langchain/openai";import { StateGraph, MessagesAnnotation, START, END,} from "@langchain/langgraph";
// Create LLM callconst llm = new ChatOpenAI({ modelName: "gpt-4o", apiKey: "your-api-key", // Warning: API key will be exposed in browser!});
async function callLLM(state) { const response = await llm.invoke(state.messages);
return { messages: [...state.messages, response], };}
// Create the agentconst agent = new StateGraph(MessagesAnnotation) .addNode("agent", callLLM) .addEdge(START, "agent") .addEdge("agent", END);
const graph = agent.compile({ name: "my_agent" });
// Manually instrument the graphSentry.instrumentLangGraph(graph, { recordInputs: true, recordOutputs: true,});
// Invoke the agentconst result = await graph.invoke({ messages: [ new SystemMessage("You are a helpful assistant."), new HumanMessage("Hello!"), ],});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 LangGraph operations:
Type: boolean (optional)
Records inputs to LangGraph operations (such as messages and state data passed to the graph).
Defaults to true if sendDefaultPii is true.
Type: boolean (optional)
Records outputs from LangGraph operations (such as generated responses, agent outputs, and final state).
Defaults to true if sendDefaultPii is true.
Usage
Using the langGraphIntegration integration:
Sentry.init({ dsn: "____PUBLIC_DSN____", // Tracing must be enabled for agent monitoring to work tracesSampleRate: 1.0, integrations: [ Sentry.langGraphIntegration({ // your options here }), ],});Using the instrumentLangGraph helper:
Sentry.instrumentLangGraph(graph, { // your options here});By default, tracing support is added to the following LangGraph SDK calls:
- Agent Creation (
gen_ai.create_agent) - Captures spans when compiling a StateGraph into an executable agent - Agent Invocation (
gen_ai.invoke_agent) - Captures spans for agent execution viainvoke()
@langchain/langgraph:>=0.2.0 <2.0.0