Repository navigation
compaction: count opaque state by the provider's own measure - #1694
Merged
Merged
Conversation
Compaction state was always estimated at its bytes / 16. The real ratio varies widely: a 1.4 MB blob counted ~70k tokens, a 9.8 MB one ~80k. The second read as ~612k, and since the context meter is floored at the local estimate, a history of one blob and a short reply sat over the compaction line and compacted again every turn, while the provider reported ~82k. After a full (unchained) Responses request that carries compaction state, the state's share of the reported input (the input less the estimate for everything else) is kept by size and replaces /16 for that state. Chained requests send only a delta and are skipped. Before the first count, /16 still applies. Fixes #1686
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.
Fixes #1686 (the accounting half; #1687 handled repeated compaction).
Root cause
context_tokens.estimatedTokensFromLencounted compaction state at bytes / 16. The real ratio varies widely: a 1.4 MB blob counted ~70k tokens, a 9.8 MB one ~80k. The second read as ~612k. The context meter is floored at the local estimate (agent_context.replaceContextTokens), so a history of one blob and a short reply sat over the compaction line and compacted every turn, while the provider reported ~82k input tokens.Fix
opaque_state_usage.learn, run before the meter reads the estimate. After a full (unchained) Responses request that carries compaction state, it computes the state's share of the reported input: the input less the estimate for everything else.context_tokens.calibrateState) and replaces /16 for that state.Tests