fix(config): clamp maxTokens to contextWindow to prevent invalid configurations

Closes #5308

When users configure maxTokens larger than contextWindow (e.g., maxTokens: 40960
with contextWindow: 32768), the model may fail silently. This fix clamps
maxTokens to be at most contextWindow, preventing such invalid configurations.
This commit is contained in:
damaozi
2026-01-31 23:46:38 +08:00
parent 83e64c1ac9
commit 5918affe49

View File

@@ -215,7 +215,9 @@ export function applyModelDefaults(cfg: OpenClawConfig): OpenClawConfig {
}
const defaultMaxTokens = Math.min(DEFAULT_MODEL_MAX_TOKENS, contextWindow);
const maxTokens = isPositiveNumber(raw.maxTokens) ? raw.maxTokens : defaultMaxTokens;
// Clamp maxTokens to contextWindow to prevent invalid configurations
const rawMaxTokens = isPositiveNumber(raw.maxTokens) ? raw.maxTokens : defaultMaxTokens;
const maxTokens = Math.min(rawMaxTokens, contextWindow);
if (raw.maxTokens !== maxTokens) {
modelMutated = true;
}