Fix token count mismatch in Agents dashboard for nested LangGraph setups#195
Open
hectorhdzg wants to merge 1 commit into
Open
Fix token count mismatch in Agents dashboard for nested LangGraph setups#195hectorhdzg wants to merge 1 commit into
hectorhdzg wants to merge 1 commit into
Conversation
Performance comparisonThreshold: regressions >15.0% on gating scenarios fail the build. Higher ops/s is better; positive Δ means the PR is slower.
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes incorrect token aggregation in the Azure Monitor Agents dashboard for multi-agent / nested LangGraph setups by ensuring agent detection treats any agent-like chain under an existing agent (even when separated by intermediate non-agent chain nodes) as internal rather than a new agent.
Changes:
- Update
_is_agent_run()to detect nested agents by walking the full ancestor chain via_find_agent_ancestor()instead of only checking the direct parent. - Add a regression unit test covering a deeply nested “agent-like” sub-graph separated from the top-level agent by an intermediate non-agent chain.
- Minor test formatting cleanup in an existing message/tool-call test case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/microsoft/opentelemetry/_genai/_langchain/_tracer.py |
Prevents incorrectly classifying nested agent-like sub-graphs as separate agents by checking all ancestors for an existing agent. |
tests/langchain/test_tracer.py |
Adds a regression test ensuring deeply nested agent-like chains under an existing agent are not treated as new agents. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
JacksonWeber
approved these changes
Jun 8, 2026
rads-1996
approved these changes
Jun 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In multi-agent LangGraph setups (e.g., graphs with sub-graphs), the Agents dashboard in Azure Monitor shows significantly lower token counts than what appears in the trace spans. For example, a trace showing 6,816 total tokens would only display ~2.2K in the Token Consumption chart.
Root Cause
_is_agent_run() only checked the direct parent when preventing nested agent detection:
In LangGraph, agent-like sub-graphs are often separated from the top-level agent by intermediate chain nodes:
Since SubGraph's direct parent is node_step (not in _agent_run_ids), it was incorrectly treated as a separate agent. LLM tokens under it aggregated to SubGraph's bucket instead of the top-level agent. When SubGraph ended, its tokens were discarded -- agent runs don't roll up to parent agents.
Fix
Changed the direct-parent check to walk the full ancestor chain using the existing _find_agent_ancestor() method:
This ensures any agent-like chain nested anywhere under an existing agent is treated as internal, and all LLM token counts aggregate to the single top-level invoke_agent wrapper span.