Skip to content

[opentelemetry-instrumentation-genai-langchain] Capture number of inference and tool calls on agent invocations - #1001

Open
rads-1996 wants to merge 9 commits into
open-telemetry:mainfrom
rads-1996:langcahin-agent-call-counts
Open

rads-1996 wants to merge 9 commits into
open-telemetry:mainfrom
rads-1996:langcahin-agent-call-counts

Conversation

@rads-1996

Copy link
Copy Markdown
Contributor

Description

Fixes #598

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. List any relevant details for your test
configuration.

  • Test A

Checklist

See CONTRIBUTING.md
for the style guide, changelog guidance, and more.

  • Followed the style guidelines of this project
  • Changelog updated if the change requires an entry
  • Unit tests added
  • Documentation updated

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on maintainers · refreshed 2026-11-09 17:41 UTC

Merge when ready.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@rads-1996
rads-1996 force-pushed the langcahin-agent-call-counts branch from 7528d97 to 5b7ad0c Compare October 28, 2026 19:10
@rads-1996
rads-1996 requested a balanced review from Copilot October 28, 2026 19:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Agent call counts can miss direct model calls or callbacks from retained managers, and can overcount calls inside tools or retrievers.

3 open issues
What changed in this PR

Adds per-agent inference and tool call metrics to LangChain instrumentation, addressing #598 through the shared GenAI invocation utilities.

Changes:

  • Initializes agent call counts and increments them from model and tool callbacks.
  • Adds metric assertions for agent classification, nested agents, tool failures, and conformance scenarios.
  • Raises the shared utility dependency floor and adds a changelog entry.

All paths below are under instrumentation/opentelemetry-instrumentation-genai-langchain/.

File Description
tests/​test_tools.py Checks tool counts on failures and nearest-agent attribution.
tests/​test_callback_handler.py Checks zero counts and unnamed-agent inference metrics.
tests/​requirements.oldest.txt Uses the local utility package pending release.
tests/​conformance/​agent.py Validates both call-count histograms and expected totals.
src/​opentelemetry/​instrumentation/​genai/​langchain/​invocation_manager.py Initializes counts and resolves the nearest parent agent.
src/​opentelemetry/​instrumentation/​genai/​langchain/​callback_handler.py Counts text-model, chat-model, and tool starts.
pyproject.toml Raises the utility dependency minimum to 1.4b0.dev.
.changelog/​1001.added Documents the new agent call-count feature.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Agent call counts can be omitted for AgentExecutor, lost under concurrency, or double-counted for delegated models, with missing failure and oldest-version test coverage.

5 open issues
3 resolved since last review

Review effort: Balanced

@rads-1996
rads-1996 force-pushed the langcahin-agent-call-counts branch from 71e3eda to 362c7bc Compare October 29, 2026 17:59
@rads-1996
rads-1996 requested a balanced review from Copilot October 29, 2026 20:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Observed model and tool calls for supported non-graph agents are discarded, so their call-count metrics are not emitted.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@rads-1996
rads-1996 force-pushed the langcahin-agent-call-counts branch from cf8a014 to 53e21f2 Compare November 5, 2026 17:25
if token is not None:
context.run(detach, token)

def _wrap_attempt_config(

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.

Sorry, my earlier retry comment sent this in the wrong direction. LangChain retries/fallbacks each produce their own chat span, so every traced inference is already one logical call from our side. Could we drop the retry dedup (_InferenceCallScope, the patch_config/BaseChatModel.invoke/middleware patches, start_inference_attempt) and just increment the agent counter in on_chat_model_start when we start an inference span? That keeps the metric consistent with the spans.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, made revisions to the code.

):
for method in (
"on_chat_model_start",
"on_llm_start",

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.

question: this looks unrelated now that on_llm_start isn't counted. Is it left over from the earlier version? If so, could we drop it along with the llm case in test_execution_context.py, and send it as a separate fix if it's still needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it was left over from the previous iteration. Removed it in the updated version.

@rads-1996
rads-1996 force-pushed the langcahin-agent-call-counts branch from 53e21f2 to e955027 Compare November 6, 2026 17:04
Comment on lines +706 to +725
@pytest.mark.asyncio
@pytest.mark.parametrize("asynchronous", [False, True], ids=["sync", "async"])
@pytest.mark.parametrize("streaming", [False, True], ids=["invoke", "stream"])
@pytest.mark.parametrize("strategy", ["retry", "fallback"])
@pytest.mark.parametrize("failure", [False, True])
@pytest.mark.parametrize("use_tool", [False, True])
@pytest.mark.parametrize(
"use_middleware",
[
False,
pytest.param(
True,
marks=pytest.mark.skipif(
not hasattr(langchain.agents, "create_agent"),
reason="Model middleware requires LangChain 1.x",
),
),
],
)
async def test_native_agent_retries_count_traced_inferences(

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.

Now that retry dedup is gone, counting is just "one per chat span", so this 64-case retry/fallback/middleware matrix (plus test_agent_inference_counts_direct_fallback_stream and test_independent_model_calls_share_one_agent_node) mostly re-tests the old design. Could we drop them and keep test_native_agent_call_counts with a single retry case? Those tests would also fit better in an agent test module than in test_tools.py.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed. Created a new test_agent suite and added the test there.

@rads-1996
rads-1996 force-pushed the langcahin-agent-call-counts branch from 13102fe to a7ea024 Compare November 9, 2026 17:32

This branch has not been deployed

No deployments
Sign up for free to 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.

[langchain] Capture number of inference and tool calls on agent invocations

4 participants