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 {}), }, )