Skip to content

fix: don't double-count cached tokens when deciding to compact context - #28593

Open
Classic298 wants to merge 1 commit into
open-webui:devfrom
Classic298:fix-compaction-cache-double-count
Open

fix: don't double-count cached tokens when deciding to compact context#28593
Classic298 wants to merge 1 commit into
open-webui:devfrom
Classic298:fix-compaction-cache-double-count

Conversation

@Classic298

Copy link
Copy Markdown
Member

Context compaction fired at roughly half the configured threshold on backends that report prompt caching. With a 70k threshold, a chat showing 39k tokens in the UI was already being compacted, and the more the cache helped, the earlier it happened.

llama.cpp's timings are merged into usage, so the same dict carries both prompt_tokens and cache_n, where prompt_tokens already includes cache_n (38153 cached + 43 new = 38196 prompt). The threshold check added cache_n on top of that, counting every cached token twice.

Dropping the extra cache_n term makes the backend agree with the number the UI shows, which is what the threshold is configured against.

Fixes #28590

Contributor License Agreement

Note

Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.

Context compaction fired at roughly half the configured threshold on backends that report prompt caching. With a 70k threshold, a chat showing 39k tokens in the UI was already being compacted, and the more the cache helped, the earlier it happened.

llama.cpp's `timings` are merged into `usage`, so the same dict carries both `prompt_tokens` and `cache_n`, where `prompt_tokens` already includes `cache_n` (38153 cached + 43 new = 38196 prompt). The threshold check added `cache_n` on top of that, counting every cached token twice.

Dropping the extra `cache_n` term makes the backend agree with the number the UI shows, which is what the threshold is configured against.

Fixes open-webui#28590
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant