fix: count actual tool calls instead of tool-related messages#564
Merged
teknium1 merged 1 commit intoNousResearch:mainfrom Mar 10, 2026
Merged
Conversation
tool_call_count was inaccurate in two ways: 1. Under-counting: an assistant message with N parallel tool calls (e.g. "kill the light and shut off the fan" = 2 ha_call_service) only incremented tool_call_count by 1 instead of N. 2. Over-counting: tool response messages (role=tool) also incremented tool_call_count, double-counting every tool interaction. Combined: 2 parallel tool calls produced tool_call_count=3 (1 from assistant + 2 from tool responses) instead of the correct value of 2. Fix: only count from assistant messages with tool_calls, incrementing by len(tool_calls) to handle parallel calls correctly. Tool response messages no longer affect tool_call_count. This impacts /insights and /usage accuracy for sessions with tool use.
Contributor
|
Merged to main — thanks @0xbyt4! Good catch on the double-counting from tool responses and under-counting of parallel calls. |
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.
Summary
tool_call_countin the session database was inaccurate due to two bugs inappend_message():Bug 1 — Under-counting parallel tool calls: An assistant message with N parallel tool calls (e.g. "kill the light and shut off the fan" = 2
ha_call_servicecalls) only incrementedtool_call_countby 1 instead of N.Bug 2 — Over-counting tool responses: Tool response messages (
role=tool) also incrementedtool_call_count, double-counting every tool interaction.Combined effect: 2 parallel tool calls produced
tool_call_count=3(1 from assistant + 2 from tool responses) instead of the correct value of 2. This makes/insightsand/usagereport inflated tool call numbers.Fix: Only count from assistant messages that carry
tool_calls, incrementing bylen(tool_calls). Tool response messages no longer affect the counter.Test plan
test_tool_call_count_matches_actual_calls— 2 parallel tool calls + 2 tool responses = count should be 2 (fails on old code with 3)test_tool_response_does_not_increment_tool_count— tool response alone should not increment counttest_assistant_tool_calls_increment_by_count— single tool call increments by 1