From dfa25111993c9e26e8ec7ed40214d13eb3887c1e Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Sun, 8 Mar 2026 00:17:36 +0100 Subject: [PATCH] fix: persist token usage data for non-streaming chat responses (#22166) The non-streaming response handler was saving assistant messages without their usage/token data. While the streaming handler correctly extracted and saved usage information, the non-streaming path discarded it entirely. This caused assistant messages from non-streaming completions to have NULL usage in the chat_message table, making them invisible to the analytics token aggregation queries and contributing to the '0 tokens' display in Admin Panel Analytics. Extract and normalize the usage data from the API response and include it in the database upsert, matching the pattern already used by the streaming handler. --- backend/open_webui/utils/middleware.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index f3d5d6821..e5e75652a 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -3093,6 +3093,9 @@ async def non_streaming_chat_response_handler(response, ctx): ) # Save message in the database + raw_usage = response_data.get("usage", {}) or {} + usage = normalize_usage(raw_usage) if raw_usage else None + Chats.upsert_message_to_chat_by_id_and_message_id( metadata["chat_id"], metadata["message_id"], @@ -3100,6 +3103,7 @@ async def non_streaming_chat_response_handler(response, ctx): "role": "assistant", "content": content, "output": response_output, + **({"usage": usage} if usage else {}), }, )