Skip to content

fix(telemetry): use per-invocation usage in agent span attributes#2017

Open
en-yao wants to merge 2 commits intostrands-agents:mainfrom
en-yao:fix/otel-per-invocation-usage
Open

fix(telemetry): use per-invocation usage in agent span attributes#2017
en-yao wants to merge 2 commits intostrands-agents:mainfrom
en-yao:fix/otel-per-invocation-usage

Conversation

@en-yao
Copy link
Copy Markdown

@en-yao en-yao commented Mar 31, 2026

Description

In multi-turn sessions, end_agent_span reports accumulated_usage (session-lifetime cumulative tokens) instead of per-invocation usage, causing inflated token metrics in OTEL spans. The per-invocation usage is already tracked at response.metrics.latest_agent_invocation.usage — this PR points end_agent_span to the correct field.

Related Issues

Resolves #2010

Documentation PR

N/A

Type of Change

Bug fix

Testing

Updated existing agent span tests to distinguish between accumulated and per-invocation usage. Added dedicated tests verifying per-invocation values are reported and fallback behavior when no invocations exist.

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] OTEL span reports accumulated_usage instead of per-invocation usage, causing inflated token metrics in Langfuse

1 participant