Skip to content

feat(core): Instrument langgraph createReactAgent - #20344

Merged
andreiborza merged 8 commits into
developfrom
ab/langgraph-agents
Apr 29, 2026
Merged

feat(core): Instrument langgraph createReactAgent#20344
andreiborza merged 8 commits into
developfrom
ab/langgraph-agents

Conversation

@andreiborza

@andreiborzaandreiborza commented Apr 16, 2026

Copy link
Copy Markdown
Member

This PR adds instrumentation for LangGraph's createReactAgent API.

createReactAgent wrapping

  • Extracts agent name, LLM model, and tools from params
  • Wraps compiled graph's invoke() with invoke_agent span
  • Wraps tool invoke() with execute_tool spans (name, type, description, arguments, result)
  • Injects LangChain callback handler + lc_agent_name + __sentry_langgraph__ metadata at invoke level for chat span creation and agent name propagation to all child spans
  • Suppresses StateGraph.compile instrumentation inside createReactAgent to avoid duplicate spans

LangChain callback handler improvements

  • Reads gen_ai.agent.name from metadata.lc_agent_name (convention from newer LangGraph createAgent, adopted for our supported versions)
  • Suppresses chain and tool callback spans inside agent context (based on metadata.__sentry_langgraph__ presence) to avoid duplicates with our direct instrumentation
  • Extracts tool definitions from extraParams in handleChatModelStart and sets gen_ai.request.available_tools on chat spans
  • Uses runName for tool name in handleToolStart (set by LangChain's StructuredTool.call()) — fixes unknown_tool issue
  • Adds gen_ai.operation.name to tool spans
  • Extracts .content from ToolMessage objects in handleToolEnd instead of serializing the full wrapper
  • addToolCallsAttributes now prefers message.tool_calls (LangChain's normalized format) over scanning message.content for Anthropic-style tool_use items, fixing duplicate tool calls on Anthropic chat spans. Falls back to message.content scanning for older LangChain versions.

OTel module patching

  • Patches @langchain/langgraph/prebuilt for createReactAgent (ESM + CJS file patches for dist/prebuilt/index.cjs)

Exports

  • instrumentCreateReactAgent from core, browser, cloudflare

Closes: #19372

@github-actions

github-actionsBot commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

PathSize% ChangeChange
@sentry/browser26.16 kB--
@sentry/browser - with treeshaking flags24.63 kB--
@sentry/browser (incl. Tracing)44.11 kB--
@sentry/browser (incl. Tracing + Span Streaming)46.16 kB--
@sentry/browser (incl. Tracing, Profiling)49.06 kB--
@sentry/browser (incl. Tracing, Replay)83.46 kB--
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags72.94 kB--
@sentry/browser (incl. Tracing, Replay with Canvas)88.14 kB--
@sentry/browser (incl. Tracing, Replay, Feedback)100.79 kB--
@sentry/browser (incl. Feedback)43.4 kB--
@sentry/browser (incl. sendFeedback)30.96 kB--
@sentry/browser (incl. FeedbackAsync)36.14 kB--
@sentry/browser (incl. Metrics)27.44 kB--
@sentry/browser (incl. Logs)27.59 kB--
@sentry/browser (incl. Metrics & Logs)28.28 kB--
@sentry/react27.9 kB--
@sentry/react (incl. Tracing)46.35 kB--
@sentry/vue31.03 kB--
@sentry/vue (incl. Tracing)45.95 kB--
@sentry/svelte26.18 kB--
CDN Bundle28.84 kB--
CDN Bundle (incl. Tracing)46.71 kB--
CDN Bundle (incl. Logs, Metrics)30.25 kB--
CDN Bundle (incl. Tracing, Logs, Metrics)47.8 kB--
CDN Bundle (incl. Replay, Logs, Metrics)69.34 kB--
CDN Bundle (incl. Tracing, Replay)83.88 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)84.94 kB--
CDN Bundle (incl. Tracing, Replay, Feedback)89.69 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)90.78 kB--
CDN Bundle - uncompressed84.55 kB--
CDN Bundle (incl. Tracing) - uncompressed139.68 kB--
CDN Bundle (incl. Logs, Metrics) - uncompressed88.75 kB--
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed143.14 kB--
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed212.71 kB--
CDN Bundle (incl. Tracing, Replay) - uncompressed257.49 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed260.93 kB--
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed271.18 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed274.62 kB--
@sentry/nextjs (client)48.84 kB--
@sentry/sveltekit (client)44.56 kB--
@sentry/node-core59 kB+0.02%+11 B 🔺
@sentry/node171.08 kB+0.58%+979 B 🔺
@sentry/node - without tracing97.81 kB+0.02%+12 B 🔺
@sentry/aws-serverless114.53 kB+0.05%+49 B 🔺
@sentry/cloudflare (withSentry) - minified164.13 kB--
@sentry/cloudflare (withSentry)415.2 kB--

View base workflow run

@andreiborza
andreiborza marked this pull request as ready for review April 20, 2026 04:09
Comment threadpackages/core/src/tracing/langgraph/index.ts Outdated
Comment threadpackages/core/src/tracing/langchain/index.ts Outdated
Comment threadpackages/core/src/tracing/langgraph/index.ts
Comment threadpackages/core/src/tracing/langchain/utils.ts
@andreiborza
andreiborzaforce-pushed the ab/langgraph-agents branch 3 times, most recently from 2dc7fd2 to fcaa34bCompareApril 20, 2026 06:09
Comment threadpackages/core/src/tracing/langgraph/index.ts
Comment threadpackages/core/src/tracing/langgraph/utils.ts
Comment threadpackages/core/src/tracing/langgraph/index.ts
Comment threaddev-packages/node-integration-tests/suites/tracing/langgraph/test.ts Outdated
Comment threadpackages/core/src/tracing/langchain/index.ts
Comment threadpackages/core/src/tracing/langchain/index.ts
Comment threadpackages/core/src/tracing/langchain/utils.ts Outdated
Comment threadpackages/core/src/tracing/langchain/index.ts
Comment threadpackages/core/src/tracing/langgraph/index.ts
Comment threadpackages/core/src/tracing/langgraph/index.ts Outdated
Comment threadpackages/core/src/tracing/langgraph/utils.ts
Comment threadpackages/core/src/tracing/langchain/utils.ts
Comment threadpackages/core/src/tracing/langgraph/utils.ts
andreiborzaand others added 7 commits April 28, 2026 19:15
Add instrumentation for LangGraph's `createReactAgent` API with
full span hierarchy: invoke_agent, gen_ai.chat, and execute_tool.
createReactAgent wrapping:
- Extract agent name, LLM model, and tools from params
- Wrap compiled graph's invoke() with invoke_agent span
- Wrap tool invoke() with execute_tool spans (name, type,
description, arguments, result)
- Inject LangChain callback handler + lc_agent_name metadata
at invoke level for chat span creation and agent name
propagation to all child spans
- Suppress StateGraph.compile instrumentation inside
createReactAgent to avoid duplicate spans
LangChain callback handler improvements:
- Read gen_ai.agent.name from metadata.lc_agent_name
- Suppress chain and tool callback spans inside agent context
to avoid duplicates with our direct instrumentation
- Extract tool definitions from extraParams in handleChatModelStart
- Use runName for tool name (set by LangChain's StructuredTool)
- Add gen_ai.operation.name to tool spans
- Extract ToolMessage .content in handleToolEnd
BREAKING: addToolCallsAttributes now reads from message.tool_calls
(LangChain's normalized format) instead of scanning message.content
for Anthropic-style tool_use items. This fixes duplicate tool calls
on Anthropic chat spans but changes the tool call format in
gen_ai.response.tool_calls from Anthropic-native to LangChain-
normalized (args instead of input, type: tool_call instead of
tool_use).
OTel module patching:
- Patch @langchain/langgraph/prebuilt for createReactAgent
(ESM + CJS file patches for dist/prebuilt/index.cjs)
Exports:
- instrumentCreateReactAgent from core, browser, cloudflare
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment threadpackages/core/src/tracing/langchain/utils.ts Outdated
export { LANGCHAIN_INTEGRATION_NAME } from './tracing/langchain/constants';
export type { LangChainOptions, LangChainIntegration } from './tracing/langchain/types';
export { instrumentStateGraphCompile, instrumentLangGraph } from './tracing/langgraph';
export { instrumentStateGraphCompile, instrumentCreateReactAgent, instrumentLangGraph } from './tracing/langgraph';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h: So if I understand correctly right now users need instrumentCreateReactAgent for the agent and instrumentLangGraph for the StateGraph, right? I think now that we have the separate API instrumentLangGraph for the StateGraph might actually be a bit confusing, wdyt? I guess ideally what we would probably do is to merge this into one, detect what we are given and then handle the patching internally. Can we do this with the current approach?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is certainly valid and a bit confusing. I do think it's better to have two functions here, these are for manual instrumenting, so it's clearer what they both do.

That being said, let's rename instrumentLangGraph to instrumentStateGraph as you suggested in #20584.

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 565b49f. Configure here.

Comment threadpackages/core/src/tracing/langgraph/utils.ts

@nicohrubecnicohrubec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! thanks for the updates, lgtm

@andreiborza
andreiborza merged commit a84b2f1 into developApr 29, 2026
499 of 501 checks passed
@andreiborza
andreiborza deleted the ab/langgraph-agents branch April 29, 2026 08:40
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(core): Instrument langgraph createReactAgent

2 participants

@andreiborza@nicohrubec