Count fixed request overhead in context calibration so compression doesn't underestimate - #7
Merged
Conversation
…n doesn't underestimate
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.
Found this while tracing why compression sometimes kicks in later than it should.
The calibration in ContextManager works like: estimate the messages in chars, scale by (real prompt_tokens / estimated) from the last API call. Problem is, the API bills for the system prompt and tool schemas on every request, and those aren't part of
messages— so the ratio quietly absorbs that fixed overhead. That's fine while the conversation is long, but after layer 2/3 compression shrinks the message list, the fixed overhead becomes a much bigger share of the real usage. The ratio is then too big for the remaining text,measure()underestimates by 10% or more, and the 70%/90% thresholds fire late — right around where a provider's hard limit lives.Fix: the agent registers its system prompt + serialized tool schemas as explicit fixed overhead (
ContextManager.setFixedOverhead), and observe/measure count it on both sides of the ratio. The ratio stays a pure chars-per-token rate, so shrinking the conversation doesn't change what it means.Tests:
4 new tests, full suite passes.