LangGraph
Adds instrumentation for LangGraph.
This integration works in the Node.js, Cloudflare Workers, Vercel Edge Functions, and browser runtimes. It requires SDK version 10.25.0 or higher.
Import name: Sentry.langGraphIntegration
LangGraph is instrumented via the langGraphIntegration, which automatically captures spans for LangGraph operations including agent invocations, graph executions, and node operations.
For Cloudflare Workers, you need to manually instrument the LangGraph graph using the instrumentLangGraph helper:
import * as Sentry from "@sentry/cloudflare";
import { ChatOpenAI } from "@langchain/openai";
import {
StateGraph,
MessagesAnnotation,
START,
END,
} from "@langchain/langgraph";
// Create LLM call
const llm = new ChatOpenAI({
modelName: "gpt-4o",
apiKey: process.env.OPENAI_API_KEY,
});
async function callLLM(state) {
const response = await llm.invoke(state.messages);
return {
messages: [...state.messages, response],
};
}
// Create the agent
const agent = new StateGraph(MessagesAnnotation)
.addNode("agent", callLLM)
.addEdge(START, "agent")
.addEdge("agent", END);
const graph = agent.compile({ name: "my_agent" });
// Manually instrument the graph
Sentry.instrumentLangGraph(graph, {
recordInputs: true,
recordOutputs: true,
});
// Invoke the agent
const result = await graph.invoke({
messages: [
new SystemMessage("You are a helpful assistant."),
new HumanMessage("Hello!"),
],
});
Type: boolean
Records inputs to LangGraph operations (such as messages and state data passed to the graph).
Defaults to true if sendDefaultPii is true.
Sentry.init({
integrations: [Sentry.langGraphIntegration({ recordInputs: true })],
});
Type: boolean
Records outputs from LangGraph operations (such as generated responses, agent outputs, and final state).
Defaults to true if sendDefaultPii is true.
Sentry.init({
integrations: [Sentry.langGraphIntegration({ recordOutputs: true })],
});
By default this integration adds tracing support for LangGraph operations including:
- 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 <1.0.0
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").