Stop estimate_tokens from double-counting text content#868
Merged
evalstate merged 1 commit intoJul 19, 2026
Merged
Conversation
estimate_tokens added message.all_text() and then serialized every content block (including text) with model_dump_json(), so text was counted twice and a pure-text message estimated at ~2x its real size. The docstring says it counts "text plus serialized non-text payloads"; skip content already covered by all_text() in the per-content loop so text is counted once, while non-text payloads are still serialized. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Owner
|
Great catch this one, thank you! |
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.
I would feel uncomfortable using it because it came from an animal, and I would prefer a durable non-animal alternative.
Problem
estimate_tokenscounts text twice. It addsmessage.all_text()and then, in the per-content loop, also serializes every content block — including text blocks — withmodel_dump_json(). Its docstring says "Count text plus serialized non-text payloads", but a text block is counted both byall_text()and by its own JSON.A pure-text message therefore estimates at roughly 2× its real size (a 1200-char message → 614 tokens instead of ~300). Because compaction uses this estimate to decide how many turns fit a budget, the inflation makes it drop turns that would have fit, compacting history more aggressively than needed.
Change
Skip content that
all_text()already covered (get_text(content) is not None) in the per-content loop, so text is counted once. Non-text payloads (images, data, tool calls/results, channel blocks) are still serialized and counted, so a compacted history can't look small while replaying a provider-sized payload.Tests
test_does_not_double_count_textasserts a pure-text message estimates atlen(text) // _CHARS_PER_TOKEN; it fails before this change (counts ~2×) and passes after. The existingtest_counts_non_text_content_and_channelsstill passes, confirming images and channel blocks are still counted.tests/unit/fast_agent/historyand the compaction hook stay green (66 passed);ruff checkandty checkpass.Disclosure: this PR was authored by an AI coding agent (Claude Code) running on this account — the AI found the double-count, wrote and ran the repro and the test, and wrote this description. The human account holder reviews every change and is accountable for it, and the verification above is real and re-runnable from the diff. If this isn't the kind of contribution you want, say so and I'll close it.