Skip to content

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

Merged
mkmeral merged 5 commits intostrands-agents:mainfrom
en-yao:fix/otel-per-invocation-usage
Apr 9, 2026
Merged

fix(telemetry): use per-invocation usage in agent span attributes#2017
mkmeral merged 5 commits intostrands-agents:mainfrom
en-yao:fix/otel-per-invocation-usage

Conversation

@en-yao
Copy link
Copy Markdown
Contributor

@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

@github-actions github-actions bot added size/s and removed size/s labels Apr 7, 2026
@en-yao en-yao requested a deployment to manual-approval April 7, 2026 17:07 — with GitHub Actions Waiting
@en-yao en-yao requested a deployment to manual-approval April 7, 2026 17:07 — with GitHub Actions Waiting
@mkmeral
Copy link
Copy Markdown
Contributor

mkmeral commented Apr 9, 2026

/strands review

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 2026

Assessment: Comment

The fix correctly identifies and addresses the root cause from #2010end_agent_span was reporting session-lifetime accumulated_usage instead of per-invocation usage. The implementation is clean, the fallback behavior with a warning when latest_agent_invocation is None is good (per poshinchen's earlier feedback), and the tests cover the key paths.

Review Themes
  • Opt-in vs Default: The main design question is whether this bug fix should be opt-in (as implemented) or the default behavior. The issue reporter describes the current behavior as a bug causing inflated metrics for all multi-turn users. Gating the fix behind an env var means affected users must discover and set gen_ai_use_latest_invocation_tokens to get correct metrics.
  • Env Var Namespace: gen_ai_use_latest_invocation_tokens is an SDK-specific value being placed in the OTEL standard OTEL_SEMCONV_STABILITY_OPT_IN env var alongside actual OTEL semconv values. This could benefit from clearer scoping or documentation.
  • Test Coverage: The new tests cover the happy path and the None-fallback path well. Cache token fields could use additional coverage in the per-invocation path.

@mkmeral
Copy link
Copy Markdown
Contributor

mkmeral commented Apr 9, 2026

@en-yao CI is failing, can you run hatch fmt --formatter && hatch fmt --linter? https://github.com/strands-agents/sdk-python/actions/runs/24200518889/job/70644356740?pr=2017

@en-yao
Copy link
Copy Markdown
Contributor Author

en-yao commented Apr 9, 2026

Ran hatch fmt --formatter && hatch fmt --linter locally, both pass. The lint failure is a mypy error on line 856 that's already on main — not from this PR.

@mkmeral
Copy link
Copy Markdown
Contributor

mkmeral commented Apr 9, 2026

I see, @en-yao can you merge from main then? This is a required workflow, so we need it to be passing before merging

@mkmeral mkmeral enabled auto-merge (squash) April 9, 2026 18:41
@github-actions github-actions bot added size/s and removed size/s labels Apr 9, 2026
@mkmeral
Copy link
Copy Markdown
Contributor

mkmeral commented Apr 9, 2026

actually no problem, i merged from main. once the CI passes, it will merge automatically. thank you for the contribution :)

@mkmeral mkmeral merged commit d27b8ff into strands-agents:main Apr 9, 2026
37 of 46 checks passed
@en-yao en-yao deleted the fix/otel-per-invocation-usage branch April 10, 2026 01:28
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

3 participants