({
modelId,
model,
settings,
format,
}: {
modelId: string
model: ModelInfo
settings?: ProviderSettings
format?: "anthropic" | "openai" | "gemini" | "openrouter"
})
| 103 | // Max Tokens |
| 104 | |
| 105 | export const getModelMaxOutputTokens = ({ |
| 106 | modelId, |
| 107 | model, |
| 108 | settings, |
| 109 | format, |
| 110 | }: { |
| 111 | modelId: string |
| 112 | model: ModelInfo |
| 113 | settings?: ProviderSettings |
| 114 | format?: "anthropic" | "openai" | "gemini" | "openrouter" |
| 115 | }): number | undefined => { |
| 116 | if (shouldUseReasoningBudget({ model, settings })) { |
| 117 | return settings?.modelMaxTokens || DEFAULT_HYBRID_REASONING_MODEL_MAX_TOKENS |
| 118 | } |
| 119 | |
| 120 | const isAnthropicContext = |
| 121 | modelId.includes("claude") || |
| 122 | format === "anthropic" || |
| 123 | (format === "openrouter" && modelId.startsWith("anthropic/")) |
| 124 | |
| 125 | // For "Hybrid" reasoning models, discard the model's actual maxTokens for Anthropic contexts |
| 126 | if (model.supportsReasoningBudget && isAnthropicContext) { |
| 127 | return ANTHROPIC_DEFAULT_MAX_TOKENS |
| 128 | } |
| 129 | |
| 130 | // For Anthropic contexts, always ensure a maxTokens value is set |
| 131 | if (isAnthropicContext && (!model.maxTokens || model.maxTokens === 0)) { |
| 132 | return ANTHROPIC_DEFAULT_MAX_TOKENS |
| 133 | } |
| 134 | |
| 135 | // Models that expose a configurable max-output slider (e.g. Z.ai GLM) honor the user's |
| 136 | // explicit override instead of the default 20% context-window clamp, capped at the model's |
| 137 | // own ceiling. This keeps the runtime budget consistent with the value sent to the provider. |
| 138 | if (model.supportsMaxTokens && settings?.modelMaxTokens != null && settings.modelMaxTokens > 0) { |
| 139 | return model.maxTokens ? Math.min(settings.modelMaxTokens, model.maxTokens) : settings.modelMaxTokens |
| 140 | } |
| 141 | |
| 142 | // If model has explicit maxTokens, clamp it to 20% of the context window |
| 143 | // Exception: GPT-5 models should use their exact configured max output tokens |
| 144 | if (model.maxTokens) { |
| 145 | // Check if this is a GPT-5 model (case-insensitive) |
| 146 | const isGpt5Model = modelId.toLowerCase().includes("gpt-5") |
| 147 | |
| 148 | // GPT-5 models bypass the 20% cap and use their full configured max tokens |
| 149 | if (isGpt5Model) { |
| 150 | return model.maxTokens |
| 151 | } |
| 152 | |
| 153 | // All other models are clamped to 20% of context window |
| 154 | return Math.min(model.maxTokens, Math.ceil(model.contextWindow * 0.2)) |
| 155 | } |
| 156 | |
| 157 | // For non-Anthropic formats without explicit maxTokens, return undefined |
| 158 | if (format) { |
| 159 | return undefined |
| 160 | } |
| 161 | |
| 162 | // Default fallback |
no test coverage detected