LangGraph

Adds instrumentation for the LangGraph SDK.

Import name: Sentry.instrumentLangGraph

The instrumentLangGraph helper adds instrumentation for @langchain/langgraph to capture spans by wrapping a StateGraph before compilation and recording AI agent interactions with configurable input/output recording. You need to call this helper on the graph before calling .compile(). See example below:

Copied
import { ChatOpenAI } from "@langchain/openai";
import {
  StateGraph,
  MessagesAnnotation,
  START,
  END,
} from "@langchain/langgraph";
import { SystemMessage, HumanMessage } from "@langchain/core/messages";

// Create LLM call
const 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 agent
const agent = new StateGraph(MessagesAnnotation)
  .addNode("agent", callLLM)
  .addEdge(START, "agent")
  .addEdge("agent", END);

// Instrument the graph before compiling
Sentry.instrumentLangGraph(agent, {
  recordInputs: true,
  recordOutputs: true,
});

const graph = agent.compile({ name: "my_agent" });

// Invoke the agent
const 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 instrumentLangGraph helper:

Copied
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 via invoke()

  • @langchain/langgraph: >=0.2.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").