From 88e42178788bb0becf10e9ff1fc9e7a2f71db733 Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:37:58 -0400 Subject: [PATCH] Set better max token size --- src/ClaudeDev.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ClaudeDev.ts b/src/ClaudeDev.ts index 5fe2637..c1ea9be 100644 --- a/src/ClaudeDev.ts +++ b/src/ClaudeDev.ts @@ -1305,7 +1305,7 @@ ${this.customInstructions.trim()} ` } - // Check last API request metrics to see if we need to truncate + // If the last API request's total token usage is close to the context window, truncate the conversation history to free up space for the new request const lastApiReqFinished = findLast(this.claudeMessages, (m) => m.say === "api_req_finished") if (lastApiReqFinished && lastApiReqFinished.text) { const { @@ -1317,8 +1317,9 @@ ${this.customInstructions.trim()} lastApiReqFinished.text ) const totalTokens = (tokensIn || 0) + (tokensOut || 0) + (cacheWrites || 0) + (cacheReads || 0) - const isCloseToContextWindowLimit = totalTokens >= this.api.getModel().info.contextWindow * 0.8 - if (isCloseToContextWindowLimit) { + const contextWindow = this.api.getModel().info.contextWindow + const maxAllowedSize = Math.max(contextWindow - 20_000, contextWindow * 0.8) + if (totalTokens >= maxAllowedSize) { const truncatedMessages = truncateHalfConversation(this.apiConversationHistory) await this.overwriteApiConversationHistory(truncatedMessages) }