Support a per-model input context window override for custom gateways
Custom model gateways can advertise their actual context window, but packages/core/src/core/tokenLimits.ts currently gives every unknown model DEFAULT_TOKEN_LIMIT (1,048,576). The function accepts only the model name. The documented modelConfigs settings allow generation settings such as maxOutputTokens, without an input context-window override.
We encountered this while fixing https://github.com/link-assistant/formal-ai/issues/1582. Our symbolic server derives its context capacity from available resources and publishes context_window_tokens through its model metadata. Assuming the fixed Gemini window can trigger premature compaction when the server supports more, or allow a larger context than a smaller server supports.
Reproduction against the current source (not an installed CLI end-to-end test):
- Configure a custom gateway/model named
formal-aiwhose advertised input window is 1,234,567 tokens. - Evaluate
tokenLimit('formal-ai')frompackages/core/src/core/tokenLimits.ts. - It returns 1,048,576 regardless of the gateway's advertised value. The
defaultbranch cannot receive an override.
Expected: a validated per-model input context-window setting, optionally populated from gateway model discovery. Existing Gemini model defaults should remain the fallback when no override is supplied.
Suggested change: add an optional positive integer contextWindowTokens to model configuration, resolve it alongside the existing model-specific generation configuration, and pass it to the token-budget/compaction code before falling back to tokenLimit(model). Reject invalid/non-finite values and test both values below and above 1,048,576. Do not infer an unlimited context from a model name.
Current workaround: use a client exposing an actual context-window override, or keep the gateway within the fixed window while accepting the reduced capacity. maxOutputTokens alone cannot fix the input budget. We have not patched installed Gemini packages or claimed this workaround satisfies our all-clients acceptance criterion.
There is a problem with the Gemini CLI issue triaging. Please check the action logs for details.
I’d like to contribute this if the team considers it suitable for community work. I checked the current token-limit helper and model configuration types: unknown model IDs receive 1,048,576 tokens, and the model configuration has no input-window field. A bounded first implementation could add a validated, explicitly configured per-model input window and use the resolved value consistently in context budgeting, while retaining the existing defaults when no override is configured. Automatic gateway discovery would stay separate.
Would that scope fit, and which existing configuration layer would you prefer to own the override? If suitable, could this receive the help-wanted label so I can take it through implementation and regression coverage?