Skip to content

Commit 1d67f88

Browse files
committed
Sliding window fix
1 parent 4c5045d commit 1d67f88

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/core/sliding-window/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ export function truncateConversationIfNeeded(
5555
/**
5656
* Calculates the maximum allowed tokens for models that support prompt caching.
5757
*
58-
* The maximum is computed as the greater of (contextWindow - 40000) and 80% of the contextWindow.
58+
* The maximum is computed as the greater of (contextWindow - buffer) and 80% of the contextWindow.
5959
*
6060
* @param {ModelInfo} modelInfo - The model information containing the context window size.
6161
* @returns {number} The maximum number of tokens allowed for prompt caching models.
6262
*/
6363
function getMaxTokensForPromptCachingModels(modelInfo: ModelInfo): number {
64-
return Math.max(modelInfo.contextWindow - 40_000, modelInfo.contextWindow * 0.8)
64+
// The buffer needs to be at least as large as `modelInfo.maxTokens`.
65+
const buffer = modelInfo.maxTokens ? Math.max(40_000, modelInfo.maxTokens) : 40_000
66+
return Math.max(modelInfo.contextWindow - buffer, modelInfo.contextWindow * 0.8)
6567
}
6668

6769
/**

0 commit comments

Comments
 (0)