open-webui/backend
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
open_webui fix: context compaction not triggering with llama.cpp prompt caching 2026-07-03 22:03:52 +02:00
.dockerignore fix: litellm config issue 2024-02-24 22:35:11 -08:00
.gitignore refac 2024-09-06 04:59:20 +02:00
dev.sh refac 2026-03-24 19:43:30 -05:00
requirements-min.txt chore: bump Python backend dependencies, drop unused peewee (#25786) 2026-06-29 02:02:18 -05:00
requirements.txt chore: bump Python backend dependencies, drop unused peewee (#25786) 2026-06-29 02:02:18 -05:00
start.sh refac 2026-06-17 03:01:11 +02:00
start_windows.bat refac 2026-06-17 03:01:11 +02:00