Fix incorrect reserved tokens calculation on OpenRouter (#3626)

fix: improve token reservation logic in calculateTokenDistribution
This commit is contained in:
Daniel 2025-05-15 06:22:07 -05:00 • committed by GitHub
parent b69825b95d
commit 8447670499
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -100,7 +100,8 @@ export const calculateTokenDistribution = (
// Get the actual max tokens value from the model
// If maxTokens is valid, use it, otherwise reserve 20% of the context window as a default
const reservedForOutput = maxTokens && maxTokens > 0 ? maxTokens : Math.ceil(safeContextWindow * 0.2)
const reservedForOutput =
maxTokens && maxTokens > 0 && maxTokens !== safeContextWindow ? maxTokens : Math.ceil(safeContextWindow * 0.2)
// Calculate sizes directly without buffer display
const availableSize = Math.max(0, safeContextWindow - safeContextTokens - reservedForOutput)