fix(ai): honor an explicitly configured maxTokens - #839
Open
BILLKISHORE wants to merge 5 commits into
Open
Conversation
BILLKISHORE
force-pushed
the
fix/755-max-tokens-clamp
branch
2 times, most recently
from
August 11, 2026 14:31
deb6ac4 to
0615aa7
Compare
BILLKISHORE
force-pushed
the
fix/755-max-tokens-clamp
branch
from
August 11, 2026 21:32
0615aa7 to
c7387b2
Compare
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.
Closes #755.
Problem
buildBaseOptionsclamped every request to 32000:model.maxTokensis also where user configuration lands, so amodels.jsonentry asking for"maxTokens": 131072went on the wire asmax_tokens: 32000, with no warning and no documentedoverride. This hits self-hosted and long-output setups hardest, which is where the reporter found it.
Why not just remove the clamp
The clamp is not a rare guard against bad catalog metadata. 901 of the 1162 models in
models.generated.tsthat declare amaxTokensare above 32000 (128000 on 281 models, 131072 on113), so removing it outright would change the requested output size for roughly 78% of the catalog
and the cost and latency that come with it. The default is doing real work and is worth keeping.
The bug is narrower than the clamp: an explicitly configured value should not be silently overridden
by a default.
Change
Modelgains an optionalmaxTokensExplicit. When set,buildBaseOptionsusesmodel.maxTokensunchanged; otherwise the existing 32000 default applies, now named
DEFAULT_MAX_OUTPUT_TOKENS.The registry sets the flag at the three places where a
maxTokenscan only have come fromconfiguration:
applyModelOverridewhenoverride.maxTokensis presentparseModelswhen amodels.jsonmodel definition supplies one, since the field is optional thereand otherwise defaults to 16384
applyProviderConfig, wheremaxTokensis a required field on the programmatic provider inputCatalog models are untouched and keep the default ceiling. A per-request
options.maxTokensstilltakes precedence over both, as before.
Tests
packages/ai/test/max-tokens.test.tscovers the ceiling, explicit passthrough, an explicit valuebelow the ceiling,
maxTokens: 0, and per-request precedence.packages/coding-agent/test/suite/regressions/755-configured-max-tokens.test.tscovers the threeregistry paths plus an untouched built-in.
Both fail without the corresponding change: the first asserts 131072 and gets 32000, the second
asserts the flag and gets
undefined.Verified with
npm run checkand the affected suites on Node 22.22.3.Note
Fix
maxTokensclamping to honor explicitly configured values in model registrymaxTokensvalues were clamped to a 32000 default ceiling, ignoring explicitly configured values frommodels.jsonor overrides.maxTokensExplicitflag to theModelinterface and aresolveMaxTokenshelper insimple-options.tsthat bypasses the clamp when the flag is set.model-registry.tsnow setsmaxTokensExplicit: truewhen loading models with a configuredmaxTokens, both frommodels.jsondefinitions and fromapplyModelOverride.Macroscope summarized c7387b2.