Context usage indicator counts base64 images as text and shows several times too many tokens
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 70/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
Direzione di ricerca
Start in src/utils/analyzeContext.ts: the estimate-only branch of countTokensWithFallback stringifies the whole conversation, and processAssistantMessage and processUserMessage stringify each block. Compare with roughTokenCountEstimationForMessages and roughTokenCountEstimationForBlock in src/services/tokenEstimation.ts, which already count images as a flat 2000 tokens. Done when a message with a large base64 image estimates to about 2000 tokens in a new src/utils/tests/analyzeContext.test.ts, and that test fails before the change.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
What happens
The context usage indicator in the desktop app shows far more tokens than a session really uses when the conversation holds screenshots.
In one session (a model with a 1M-token window) the indicator showed 933,868 / 1,000,000, with 903,784 under "Messages". The provider's dashboard recorded about 140,000 input tokens for the latest request. Auto-compaction did not run. That is correct for the real usage, but it leaves the indicator looking wrong, and a nearly full bar tells the user to compact a session that is nowhere near its limit.
To reproduce
- Start a session in which the agent takes many browser screenshots, for example with the built-in browser tools (in the session above the agent had taken about 40).
- Open the context usage indicator below the input box.
- Compare "Messages" with the input tokens your provider reports for the latest request.
Cause
The desktop app reads the indicator through the sidecar, which asks the CLI for get_context_usage with estimateOnly: true (sessions.ts#L554, #L567).
In that mode countTokensWithFallback returns roughTokenCountEstimation(jsonStringify({ messages, tools })) (analyzeContext.ts#L85). It serializes the whole conversation, so the base64 data of every image is counted as text, at about one token per four characters. A screenshot of a few hundred KB of base64 becomes tens of thousands of tokens, where a provider charges about 1 to 2 thousand.
An image-aware estimator already exists. roughTokenCountEstimationForBlock counts an image or document as a flat 2000 tokens, and its comment explains that base64 must not go through JSON serialization (tokenEstimation.ts#L400). roughTokenCountEstimationForMessages uses it (tokenEstimation.ts#L327). The estimate-only path does not.
The per-category breakdown has the same problem: processAssistantMessage and processUserMessage stringify each block and estimate the string (analyzeContext.ts#L813, #L845), so "tool results" is inflated too.
A transcript from such a session holds 41 image blocks with about 5.9M base64 characters, which is about 1.48M tokens at one per four characters. That is enough to explain the inflated number. The CLI drops older tool results before estimating, so the indicator did not show the full 1.48M.
Why auto-compaction is not affected
shouldAutoCompact uses tokenCountWithEstimation (autoCompact.ts#L210, tokens.ts#L244). It starts from the real usage of the last API response and estimates only the messages added after it, so it saw about 140k and correctly did not compact.
Suggested fix
- In the estimate-only branch, count the messages with
roughTokenCountEstimationForMessages(messages)and the tools with a rough estimate of their JSON, instead of stringifying everything. - In
processAssistantMessageandprocessUserMessage, estimate each block with the same image-aware per-block estimator (export it) instead of stringifying the block. - Add a test: a message holding a large base64 image estimates to about 2000 tokens, not hundreds of thousands. There is no test file for
analyzeContextortokenEstimationyet, so a newsrc/utils/__tests__/analyzeContext.test.tsis fine. Check that the test fails without the change.
The result stays an estimate and will not match the provider exactly. Making the indicator and auto-compaction agree by construction would mean anchoring the indicator on the last API usage as well. That is a bigger change and not needed to fix this.
- Lingua principale
- TypeScript
- Stelle
- 490
- Fork
- 47
- Merge medio
- 8h 51m
- PR unite (30g)
- 91
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di GoDiao/dreamcoder
-
[good first issue] UX polish — friendlier copy for error toasts and empty statesForse già presa @iamcipherdev l’ha presa 22 giorni fa. Apertaarea:i18n area:ux difficulty:1 good first issue mentor-available
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
GoDiao/dreamcoder#9 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Add more provider presets (Groq / OpenRouter / SiliconFlow / etc.)Forse di nuovo libera @01iamysf l’ha presa 29 giorni fa e non c’è nessuna pull request aperta. Apertaarea:desktop area:provider difficulty:1 good first issue mentor-available
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
GoDiao/dreamcoder#6 · 6 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement rfc
Difficoltà 5/5 Più di una settimana Idoneità per principianti 28/100
GoDiao/dreamcoder#241 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Composer shows a disabled send button instead of Stop while the CLI is startingForse già presa @22chiragmodh l’ha presa 1 giorno fa. Apertabug good first issue
GoDiao/dreamcoder#240 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
area:desktop enhancement rfc
Difficoltà 5/5 Più di una settimana Idoneità per principianti 8/100
GoDiao/dreamcoder#234 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di GoDiao/dreamcoder
Issue simili
-
content triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
tensorflow/tfjs#8754 ·
-
tool-submission
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 83/100
BraveOPotato/NoSignups#2348 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
code-yeongyu/oh-my-openagent#12784 ·
I maintainer di solito rispondono entro 1 giorno
-
external-issue to-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 71/100
I maintainer di solito rispondono entro 1 giorno