Skip to content

Commit 7459552

Browse files
fix(langgraph): Call instrumentLangGraph before .compile() (#16611)
Fix the `instrumentLangGraph` example in the Browser-Side Usage section to call the helper **before** `.compile()`, not after. `instrumentLangGraph` works by wrapping the `.compile()` method on a `StateGraph` to intercept the compilation step and inject tracing. It must be called before `.compile()` is invoked. The previous example passed the already-compiled graph, meaning the compile step had already happened and there was no hook point for the SDK to instrument. Also updates the surrounding description from "wrapping a compiled LangGraph graph" to "wrapping a StateGraph before compilation" to match the correct usage.
1 parent 0a998c1 commit 7459552

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

  • docs/platforms/javascript/common/configuration/integrations

docs/platforms/javascript/common/configuration/integrations/langgraph.mdx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ supported:
4242

4343
<Alert>
4444

45-
For meta-framework applications using all runtimes, you need to manually wrap your compiled graph with `instrumentLangGraph`. See instructions in the [Browser-Side Usage](#browser-side-usage) section.
45+
For meta-framework applications using all runtimes, you need to manually wrap your graph before compiling with `instrumentLangGraph`. See instructions in the [Browser-Side Usage](#browser-side-usage) section.
4646

4747
</Alert>
4848

@@ -52,7 +52,7 @@ For meta-framework applications using all runtimes, you need to manually wrap yo
5252

5353
_Import name: `Sentry.langGraphIntegration`_
5454

55-
The `langGraphIntegration` adds instrumentation for [`@langchain/langgraph`](https://www.npmjs.com/package/@langchain/langgraph) to capture spans by automatically wrapping LangGraph operations and recording AI agent interactions including agent invocations, graph executions, and node operations.
55+
The `langGraphIntegration` adds instrumentation for [`@langchain/langgraph`](https://www.npmjs.com/package/@langchain/langgraph) to capture spans by automatically wrapping LangGraph operations and recording AI agent interactions including agent invocations, graph executions, and node operations.
5656

5757
<PlatformSection notSupported={["javascript.bun", "javascript.cloudflare"]}>
5858

@@ -98,11 +98,12 @@ For Cloudflare Workers, manual instrumentation is required using `instrumentLang
9898

9999
</PlatformSection>
100100

101-
The `instrumentLangGraph` helper adds instrumentation for [`@langchain/langgraph`](https://www.npmjs.com/package/@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:
101+
The `instrumentLangGraph` helper adds instrumentation for [`@langchain/langgraph`](https://www.npmjs.com/package/@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:
102102

103103
```javascript
104104
import { ChatOpenAI } from "@langchain/openai";
105105
import { StateGraph, MessagesAnnotation, START, END } from '@langchain/langgraph';
106+
import { SystemMessage, HumanMessage } from '@langchain/core/messages';
106107

107108
// Create LLM call
108109
const llm = new ChatOpenAI({
@@ -124,14 +125,14 @@ const agent = new StateGraph(MessagesAnnotation)
124125
.addEdge(START, 'agent')
125126
.addEdge('agent', END);
126127

127-
const graph = agent.compile({ name: 'my_agent' });
128-
129-
// Manually instrument the graph
130-
Sentry.instrumentLangGraph(graph, {
128+
// Instrument the graph before compiling
129+
Sentry.instrumentLangGraph(agent, {
131130
recordInputs: true,
132131
recordOutputs: true,
133132
});
134133

134+
const graph = agent.compile({ name: 'my_agent' });
135+
135136
// Invoke the agent
136137
const result = await graph.invoke({
137138
messages: [

0 commit comments

Comments
 (0)