LangGraph

Adds instrumentation for LangGraph.

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.

It is enabled by default and will automatically capture spans for LangGraph operations. You can opt-in to capture inputs and outputs by setting recordInputs and recordOutputs in the integration config:

Copied
Sentry.init({
  dsn: "____PUBLIC_DSN____",
  tracesSampleRate: 1.0,
  integrations: [
    Sentry.langGraphIntegration({
      recordInputs: true,
      recordOutputs: true,
    }),
  ],
});

Copied
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 compiledAgent = agent.compile({ name: "my_agent" });

// Invoke the agent - automatically instrumented
const result = await compiledAgent.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.

Copied
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.

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

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