fix(tokens): image blocks as flat ~1500 tokens; compute real contextUsagePct#54
Closed
KillerQueen-Z wants to merge 1 commit into
Closed
fix(tokens): image blocks as flat ~1500 tokens; compute real contextUsagePct#54KillerQueen-Z wants to merge 1 commit into
KillerQueen-Z wants to merge 1 commit into
Conversation
…sagePct Three related issues that combined to make /context and the renderer's context-window ring widely inaccurate on image-bearing sessions: 1. estimateContentPartTokens: when tool_result.content was an array containing an image block, the whole array was JSON.stringify-ed and tokenized as text. That counted the base64 `data` field literally, so a single ~100KB normalized image was estimated at ~70k tokens (Anthropic actually bills (w*h)/750 ≈ 1100-1500). One image read pushed a fresh session's /context to 35%+ and could trigger spurious /compact loops. Fix: walk the content array block-by-block. Text blocks count as text; image blocks count as a flat 1500 tokens (close enough to Anthropic's real billing for normalized images — Read tool caps long edge so most images land near 1024x768 ≈ 1050 tokens). Unknown block types still stringify, but with `source.data` redacted to '<bytes>' to prevent the same blow-up. 2. getAnchoredTokenCount: returned `contextUsagePct: 0` hardcoded on both return paths. The agent loop emits this verbatim via `kind: 'usage'` events, so the desktop/extension renderer's context ring sat at 0% regardless of how full the model's context actually was. `/context` was unaffected because the CLI command re-derives pct from `estimated` itself. Fix: compute (estimated / contextWindow) * 100, using the current model's window from getContextWindow(_currentModel). 3. loop.ts emitted contextPct rounded to an integer. A 200-message session at 0.4% rounded to 0 and froze the renderer's ring. Match /context's `.toFixed(1)` fidelity by keeping one decimal place. Verified locally: a 4-message session with one ~100KB image read now shows ~1.9k / 200k (1.0%) on /context, matching Anthropic's true input token count within ~5%. Pre-fix the same session showed ~75k / 200k (37.8%).
2 tasks
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.
Summary
Three related issues that combined to make
/contextand the renderer's context-window ring widely inaccurate on image-bearing sessions.Reproduced empirically: same 4-message session with one ~100KB image,
/contextshows ~75k / 200k (37.8%)/contextshows ~1.9k / 200k (1.0%) ← matches Anthropic's true count40x discrepancy.
Root causes
1. `estimateContentPartTokens` JSON.stringify-ed image blocks
When `tool_result.content` was a `[{text}, {image}]` array, the whole array was `JSON.stringify`-ed and the base64 `data` field counted as text. A single normalized image (~140KB base64) became ~70k phantom tokens. Anthropic actually bills `(w*h)/750` ≈ 1100-1500 per image.
Fix: walk the content array block-by-block. Text blocks count as text; image blocks count as a flat 1500 tokens (close to Anthropic's real billing — `Read` tool caps long edge so normalized images land near 1024×768 ≈ 1050 tokens). Unknown block types still stringify, but with `source.data` redacted to `` so future block kinds don't regress.
2. `getAnchoredTokenCount` returned `contextUsagePct: 0` always
Both return paths hardcoded the field. Agent loop emits this verbatim via `kind: 'usage'` events, so the desktop/extension renderer's context ring sat at 0% regardless of real fullness. `/context` was unaffected because the CLI command re-derives `pct` from `estimated` itself.
Fix: compute `(estimated / contextWindow) * 100` using the current model's window from `getContextWindow(_currentModel)`.
3. `loop.ts` rounded `contextPct` to integer
A 200-message session at 0.4% rounded to 0 and froze the renderer's ring. Match `/context`'s `.toFixed(1)` fidelity by keeping one decimal.
Test plan
Impact