fix: tool calls make chats compact too early and inflate prompt token counts (#33218)

Since a recent refactor on dev, the prompt and completion token counts of a reply that used tools added up every model call in the loop, even though each later call already re-sends the whole conversation. A reply whose context was 50k tokens showed 90k, the context gauge read over 100% and the next message triggered compaction long before the chat reached the threshold. Prompt and completion tokens show the latest call again, while total tokens and cost still add up across all calls.
This commit is contained in:
Classic298
2026-10-26 19:17:34 +09:00
committed by GitHub
parent 208dd4f43f
commit 302b8cfd50
+3 -3
View File
@@ -138,10 +138,10 @@ def merge_usage(current: dict | None, incoming: dict | None) -> dict:
incoming_usage.get(key) if isinstance(incoming_usage.get(key), dict) else {},
)
# Aliases mirror the cumulative canonical values, including a measured zero.
# Aliases keep the latest call's size, which context compaction reads.
for alias, canonical in (('prompt_tokens', 'input_tokens'), ('completion_tokens', 'output_tokens')):
if canonical in result:
result[alias] = result[canonical]
if canonical in incoming_usage:
result[alias] = incoming_usage[canonical]
return result