feat(compaction): reconcile token estimator with provider-reported usage#3449
Merged
Conversation
Assistant messages now weigh their exact provider-reported token count instead of the chars-per-token heuristic, and a new compaction.Estimator calibrates heuristic estimates against prompt-token deltas observed between consecutive usage-bearing turns. The base heuristic moves to 3.5 chars/token and binary attachments get a flat conservative charge, keeping the estimator biased toward compacting slightly early rather than overflowing. Closes #3437
dgageot
approved these changes
Jul 3, 2026
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
Closes #3437.
compaction.EstimateMessageTokenswas a fixedlen/4 + 5heuristic. It now reconciles with provider-reported usage: exact counts for messages that carry usage, plus a session-calibrated correction for unreported content, with a conservative bias so compaction fires slightly early rather than overflowing.Issue expectations vs implementation
chat.Message.Usageweigh their provider-counted size (OutputTokens, minus reasoning tokens that are never resent as input)MessageUsagemsg.Usage) is read directly; a newcompaction.Estimatoradditionally calibrates on prompt-token deltas between consecutive usage-bearing assistant turnspkg/compaction,pkg/runtime/compactorSplitIndexForKeep/FirstIndexInBudgetself-calibrate on the slice they receive (signatures unchanged, the compactor benefits transitively);compactIfNeededcalibrates the fresh tool-result estimate against session historyEstimateMessageTokenscases, 8NewEstimatorsub-tests, calibration test forSplitIndexForKeep, runtime test proving calibration flips thecompactIfNeededdecisionCalibration mechanism
Guard rails:
Estimator accuracy fixes
len/4guesslen/4(undercounts 15-25%)len/3.5, then usage-calibratedInteraction with #3448 (configurable threshold)
Built on top of the merged threshold work:
ShouldCompact(..., threshold),sessionCompactionEnabled(a)anda.CompactionThreshold()are untouched; this PR only changes howaddedTokensand split boundaries are estimated.Validation
go build,go vet,golangci-lintpkg/compaction,pkg/runtime(+ compactor, toolexec, wire),pkg/agent,pkg/session,pkg/tuigo test ./...pkg/config,pkg/httpclient,pkg/teamloader), reproduced identically on a cleanmaincheckoutResidual design note
The 0.75 calibration floor limits downward correction: if provider reports prove the heuristic overestimates by more than 25%, compaction still fires early. That is the conservative trade-off the issue asks for; the floor can be lowered if accuracy is preferred over the early-compaction bias.