diff --git a/backend/open_webui/utils/context_compaction.py b/backend/open_webui/utils/context_compaction.py index e20f6b9cdc..54e0c3a26f 100644 --- a/backend/open_webui/utils/context_compaction.py +++ b/backend/open_webui/utils/context_compaction.py @@ -231,8 +231,13 @@ def _exceeds_token_threshold(messages: list[dict], system_prompt: str, summary: for idx in range(len(messages) - 1, -1, -1): usage = messages[idx].get('usage') or (messages[idx].get('info') or {}).get('usage') - if isinstance(usage, dict) and usage.get('input_tokens'): - total = int(usage.get('input_tokens') or 0) + int(usage.get('output_tokens') or 0) + if isinstance(usage, dict) and (usage.get('input_tokens') or usage.get('cache_n')): + # llama.cpp reports cached prefix tokens separately (timings.cache_n), excluded from prompt_n + total = ( + int(usage.get('input_tokens') or 0) + + int(usage.get('output_tokens') or 0) + + int(usage.get('cache_n') or 0) + ) return total + _estimate_messages_tokens(messages[idx + 1 :]) > threshold estimated = _estimate_tokens(system_prompt) + _estimate_tokens(summary or '') + _estimate_messages_tokens(messages) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index b507aa66c5..5c99d07e5a 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1975,7 +1975,7 @@ async def load_messages_from_db(chat_id: str, message_id: str) -> Optional[list[ return None return [ - {k: v for k, v in msg.items() if k in ('role', 'content', 'output', 'files', 'contextSummary')} + {k: v for k, v in msg.items() if k in ('role', 'content', 'output', 'files', 'contextSummary', 'usage')} for msg in db_messages ] @@ -2034,6 +2034,7 @@ def strip_compaction_fields(messages: list[dict]) -> list[dict]: clean = dict(message) clean.pop('contextSummary', None) clean.pop('context_summary', None) + clean.pop('usage', None) stripped.append(clean) return stripped @@ -2222,7 +2223,7 @@ async def process_chat_payload(request, form_data, user, metadata, model): { k: v for k, v in assistant_message.items() - if k in ('role', 'content', 'output', 'files', 'contextSummary') + if k in ('role', 'content', 'output', 'files', 'contextSummary', 'usage') } )