Skip to content

fix: track all tool invocations in ToolMetrics instead of overwriting#1892

Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/tool-metrics-all-invocations
Open

fix: track all tool invocations in ToolMetrics instead of overwriting#1892
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/tool-metrics-all-invocations

Conversation

@giulio-leone
Copy link
Contributor

Problem

ToolMetrics.add_call() overwrites self.tool with the latest invocation on every call (line 142: self.tool = tool). When users inspect AgentResult.metrics.tool_metrics, they can only see the last call's toolUseId and input — all previous invocations are lost.

This means to_dict() / get_summary() only expose the final tool call's data, making it impossible to audit which inputs were passed across multiple invocations of the same tool.

Closes #301

Solution

  • Added invocations: list[ToolUse] field to ToolMetrics that accumulates every call
  • Updated add_call() to append each tool invocation to the list
  • Updated to_dict() to expose an invocations array with all tool_use_ids and input_params
  • Backwards compatible: self.tool is still updated to the latest invocation — existing code reading metrics.tool continues to work

Changes

  • src/strands/telemetry/metrics.py: Added invocations field, updated add_call() and to_dict()
  • tests/strands/telemetry/test_metrics.py: Updated existing tests + 2 new tests for invocation tracking

Testing

  • All 1888 tests pass (1866 core + 22 telemetry)
  • New tests verify:
    • Multiple invocations are tracked with correct inputs
    • get_summary() includes full invocations array
    • Backwards compatibility (.tool still points to latest)

Previously, ToolMetrics.add_call() overwrote self.tool with the latest
invocation, losing all previous tool inputs and IDs. Users inspecting
AgentResult.metrics.tool_metrics could only see the last call's data.

Changes:
- Add 'invocations' list field to ToolMetrics that accumulates every call
- Update add_call() to append each tool invocation to the list
- Update to_dict() / get_summary() to expose invocations array
- Keep self.tool update for backwards compatibility

Closes strands-agents#301
@giulio-leone giulio-leone force-pushed the fix/tool-metrics-all-invocations branch from 62abba9 to 29496bc Compare March 15, 2026 16:12
@github-actions github-actions bot added size/s and removed size/s labels Mar 15, 2026
@giulio-leone
Copy link
Contributor Author

Friendly ping — fixes ToolMetrics to track all tool invocations instead of overwriting previous entries, giving accurate metrics for multi-tool calls.

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] AgentResult.metrics.tool_metrics skips previous tool inputs for repeatedly invoked tools

1 participant