Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(context_compaction): charge inline base64 tool-result images at flat image cost - #29818

Closed
Timmy-South wants to merge 1 commit into
open-webui:devfrom
Timmy-South:fix/context-compaction-image-tokens
Closed

fix(context_compaction): charge inline base64 tool-result images at flat image cost#29818
Timmy-South wants to merge 1 commit into
open-webui:devfrom
Timmy-South:fix/context-compaction-image-tokens

Conversation

@Timmy-South

Copy link
Copy Markdown

Summary

_estimate_messages_tokens charged a message's files list through _estimate_tokens, which JSON-serializes the value and counts len // 4. Tool-result images are persisted inline as base64 data URIs, so this counted the entire base64 payload as text tokens.

For a single ~3 MB image (≈ 4,197,680 base64 chars) this produced a ~1,049,420-token estimate for one message — larger than the backend's whole KV pool — and fired spurious auto-compaction even though engine-side prompt tokens stayed normal.

This PR:

  • charges files entries that carry a data: image URI with the flat image cost already used for content images (image / image_url → 1000 tokens), and
  • strips any remaining inline base64 data URIs from other file fields so their payload is not counted as text.

Why

Reported in #29761. #29765 fixes the model-facing leak (middleware.py extract_base64_images) but leaves this estimator charging the same base64 via files, so the spurious compaction and inflated context meter persisted.

How I verified

Standalone reproduction using the reported scenario (one files entry with a ~4,197,680-char base64 data URI):

  • before: 1,049,438 tokens
  • after: 1,000 tokens (flat image cost); non-image data URIs are stripped before counting.

Notes

@owui-terminator

Copy link
Copy Markdown

Notice regarding contribution eligibility

@Timmy-South

This pull request is missing the required checked confirmation:

  • A maintainer explicitly asked me to open this PR, or this PR only updates i18n/localization.

This pull request has been closed. If this statement applies to your contribution, check the box (- [x]) in the pull request description and reopen it.

@owui-terminator

Copy link
Copy Markdown

Notice regarding Contributor License Agreement

@Timmy-South

The description of this pull request does not contain the necessary Contributor License Agreement (CLA) text.

The inclusion of the exact CLA confirmation text is a prerequisite for the review and acceptance of any contribution. Without this text, the contribution cannot be evaluated.

This pull request has been closed. To proceed, please open a new pull request utilizing the provided PR template, which includes the requisite legal text.

@owui-terminator owui-terminator Bot closed this Sep 9, 2026
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