From 302b8cfd50adfa0abe8a00adb3c1ee73dcf66d77 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:17:34 +0100 Subject: [PATCH] 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. --- backend/open_webui/utils/response.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/open_webui/utils/response.py b/backend/open_webui/utils/response.py index 621d642c73..3eda90f686 100644 --- a/backend/open_webui/utils/response.py +++ b/backend/open_webui/utils/response.py @@ -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