Repository navigation
fix(agent): reserve the requested max output when deciding to compact - #17
Open
christopherrobertbrooks-tech wants to merge 1 commit into
Conversation
Compaction reserved a fixed 16,000 tokens while each request asks for the model's max output (32,768 by default for custom models). A long run could therefore send input + max_tokens above the context window; strict OpenAI-compatible servers reject that with HTTP 400 instead of truncating. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
What changed?
Compaction decides when to summarize using a fixed
RESERVE_TOKENS = 16000, but each requestasks for the model's max output, which
resolveRunProfileMaxOutputTokensdefaults to 32,768 forcustom models (and can be higher for known ones). A long run can therefore send
input +
max_tokensabove the configured context window.Servers that check this strictly reject the request instead of truncating. A long research task
against an OpenAI-compatible server (131,072-token window, custom provider) ended with HTTP 400:
With a 131,072 window the compaction threshold was 115,072, so anything between
98,304 and 115,072 input tokens overflowed once 32,768 output tokens were added.
This PR adds
Agent._reserveTokens(), which returnsmax(RESERVE_TOKENS, maxOutputTokens)from the run profile, and uses it for the threshold and the existing
reserveTokenstrace/usage fields. Models with output limits at or below 16,000 behave exactly as before.
How did you test it?
tests/unit/test-compaction-reserve.mjs: fails before the change(
compaction threshold 115072 + max output 32768 exceeds window 131072), passes after.npm run build(Chrome): passes.npm run build:firefoxneeds fix(build): refresh reviewed fingerprint for Firefox page-command #15 (the stale page-command fingerprint) to build at currentmain;with fix(build): refresh reviewed fingerprint for Firefox page-command #15 applied, the Firefox build passes and the change runs in a local Firefox build.
Anything reviewers should know?
No API, saved-data, permission or dependency changes. The context-usage display now shows the
larger reserve for models with a large max output, so compaction starts earlier for them.
The threshold still relies on qz's token estimate. If the estimate runs low against a provider's
tokenizer, a small extra margin (for example a few percent of the window) would help; I left that
out to keep this change to the reserve itself.
tests/unit/test-context-compaction.mjsfails on my machine both before and after this change(summary prefix assertion; Node 26), so I couldn't use it here.
🤖 Generated with Claude Code