Skip to content

fix(agent): reserve the requested max output when deciding to compact - #17

Open
christopherrobertbrooks-tech wants to merge 1 commit into
alibaba:mainfrom
christopherrobertbrooks-tech:fix/compaction-reserve-max-output
Open

christopherrobertbrooks-tech wants to merge 1 commit into
alibaba:mainfrom
christopherrobertbrooks-tech:fix/compaction-reserve-max-output

Conversation

@christopherrobertbrooks-tech

Copy link
Copy Markdown

What changed?

Compaction decides when to summarize using a fixed RESERVE_TOKENS = 16000, but each request
asks for the model's max output, which resolveRunProfileMaxOutputTokens defaults to 32,768 for
custom models (and can be higher for known ones). A long run can therefore send
input + max_tokens above 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:

prompt (102589 tokens) + max tokens (32768) exceeds the context (131072)

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 returns max(RESERVE_TOKENS, maxOutputTokens)
from the run profile, and uses it for the threshold and the existing reserveTokens
trace/usage fields. Models with output limits at or below 16,000 behave exactly as before.

How did you test it?

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.mjs fails 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

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant