Fix/underestimates token count#16117
Closed
SimbaKingjoe wants to merge 2 commits into
Closed
Conversation
added 2 commits
April 26, 2026 23:43
… tail protection and ineffective context compression
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
_find_tail_cut_by_tokens calls
len(content)to estimate message tokens, butwhen
contentis a multimodal list (e.g.[{"type": "text", "text": "..."}, {"type": "image_url", ...}]),len()returns the number of blocks (~2) instead of total character count.Every multimodal message is therefore estimated as ~10 tokens regardless of
actual size, causing tail protection to expand far beyond the token budget.
The fix sums text lengths across content blocks when
contentis a list —mirroring the pattern already used correctly in the sibling method
_prune_old_tool_results (line 488).
Related Issue
Fixes #16087
Related: #14694 (anti-thrashing cascade), supersedes #14395 (closed without merge)
Type of Change
Changes Made
len(content)withsum(len(p.get("text", "")) for p in content if isinstance(p, dict)) if isinstance(content, list) else len(content)TestTokenBudgetTailProtection::test_multimodal_list_content_counted_correctly
How to Test