Skip to content

Commit 607fa80

Browse files
fix(gettingStartedDocs): Update LangGraph example to use StateGraph API
Replace createReactAgent with StateGraph and MessagesAnnotation in the agent monitoring getting started guide. instrumentLangGraph works by wrapping the .compile() method on a StateGraph to intercept the compilation step and inject tracing. This means it must be called before .compile() is invoked. createReactAgent calls .compile() internally and returns the already-compiled graph — there's no hook point between graph construction and compilation. By the time you get the return value, the compile step has already happened, making it too late for instrumentLangGraph to do its job. In short: createReactAgent hides the compile step, but instrumentLangGraph needs to wrap that exact step. They are fundamentally incompatible with the current SDK API. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 05988cd commit 607fa80

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

static/app/gettingStartedDocs/javascript/agentMonitoring.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Sentry.init({
116116
language: 'javascript',
117117
code: `${sentryImport}
118118
import { ChatOpenAI } from "@langchain/openai";
119-
import { createReactAgent } from "@langchain/langgraph/prebuilt";
119+
import { StateGraph, MessagesAnnotation } from "@langchain/langgraph";
120120
import { HumanMessage, SystemMessage } from "@langchain/core/messages";
121121
122122
const llm = new ChatOpenAI({
@@ -125,13 +125,20 @@ const llm = new ChatOpenAI({
125125
apiKey: "OPENAI_API_KEY",
126126
});
127127
128-
const agent = createReactAgent({ llm, tools: [] });
128+
const graph = new StateGraph(MessagesAnnotation)
129+
.addNode("agent", async (state) => {
130+
const response = await llm.invoke(state.messages);
131+
return { messages: [response] };
132+
})
133+
.addEdge("__start__", "agent");
129134
130-
Sentry.instrumentLangGraph(agent, {
135+
Sentry.instrumentLangGraph(graph, {
131136
recordInputs: true,
132137
recordOutputs: true,
133138
});
134139
140+
const agent = graph.compile();
141+
135142
const result = await agent.invoke({
136143
messages: [
137144
new SystemMessage("You are a helpful assistant."),

0 commit comments

Comments
 (0)