open-webui/backend/open_webui
Classic298 9cda762af8 fix: context compaction not triggering with llama.cpp prompt caching
Context compaction failed to trigger for llama.cpp backends with prompt
caching enabled, letting chats grow until the server rejected the request
with a context overflow error. Two issues combined to cause this:

1. load_messages_from_db() dropped the usage field when reconstructing
   messages from the DB, so _exceeds_token_threshold() never saw any
   reported token counts and always fell back to rough char-based
   estimation.

2. The threshold check summed only input_tokens + output_tokens. llama.cpp
   reports cached prefix tokens separately (timings.cache_n, merged into
   the stored usage) and excludes them from prompt_n, so a mostly cached
   context of ~30k tokens was counted as only a few thousand.

Keep usage on DB-loaded messages so the threshold check can use it, count
cache_n towards the total, and strip usage in strip_compaction_fields()
after compaction runs so the non-standard field never reaches the LLM
payload.

Reported and verified in
https://github.com/open-webui/open-webui/discussions/24410
2026-07-03 22:03:52 +02:00
..
data refac: mv backend files to /open_webui dir 2024-09-04 16:54:48 +02:00
internal refac 2026-06-29 12:40:20 -05:00
migrations refac 2026-07-01 03:37:35 -05:00
models refac 2026-07-01 03:01:46 -05:00
retrieval refac 2026-07-01 02:20:16 -05:00
routers refac 2026-07-01 03:37:35 -05:00
socket refac 2026-06-29 03:58:00 -05:00
static chore: format 2026-06-01 14:10:40 -07:00
storage refac: modernize type annotations (PEP 604 / PEP 585) 2026-05-12 17:10:15 +09:00
tools refac 2026-07-01 03:35:46 -05:00
utils fix: context compaction not triggering with llama.cpp prompt caching 2026-07-03 22:03:52 +02:00
__init__.py refac 2026-06-17 03:01:11 +02:00
alembic.ini fix: Alembic CLI commands from failing 2025-08-15 04:17:47 -04:00
config.py refac 2026-07-01 02:26:47 -05:00
constants.py refac 2026-06-29 05:46:51 -05:00
env.py refac 2026-07-01 02:59:14 -05:00
events.py refac 2026-07-01 03:22:13 -05:00
functions.py fix: emit [DONE] for AsyncGenerator pipe returns (#24763) 2026-05-19 22:07:56 +04:00
main.py refac 2026-07-01 03:37:35 -05:00
tasks.py refac: modernize type annotations (PEP 604 / PEP 585) 2026-05-12 17:10:15 +09:00