From e37f6e3b88bbde780bf0afafc520651104e2ac46 Mon Sep 17 00:00:00 2001 From: pashpashpash Date: Tue, 8 Apr 2025 17:55:27 -0700 Subject: [PATCH] Context in context (#2745) * context in context * keeping comments * minimal context window line * nit --- .changeset/curvy-walls-cry.md | 5 +++ src/core/context-management/ContextManager.ts | 22 ++---------- .../context-window-utils.ts | 35 +++++++++++++++++++ src/core/task/index.ts | 31 ++++++++++++++++ 4 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 .changeset/curvy-walls-cry.md create mode 100644 src/core/context-management/context-window-utils.ts diff --git a/.changeset/curvy-walls-cry.md b/.changeset/curvy-walls-cry.md new file mode 100644 index 00000000..3b733eae --- /dev/null +++ b/.changeset/curvy-walls-cry.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +Added context window usage to env variables context for messages diff --git a/src/core/context-management/ContextManager.ts b/src/core/context-management/ContextManager.ts index 09635bbd..61e2e65f 100644 --- a/src/core/context-management/ContextManager.ts +++ b/src/core/context-management/ContextManager.ts @@ -1,7 +1,7 @@ import { Anthropic } from "@anthropic-ai/sdk" import { ClineApiReqInfo, ClineMessage } from "../../shared/ExtensionMessage" import { ApiHandler } from "../../api" -import { OpenAiHandler } from "../../api/providers/openai" +import { getContextWindowInfo } from "./context-window-utils" import { formatResponse } from "../prompts/responses" import { GlobalFileNames } from "../storage/disk" import { fileExistsAtPath } from "../../utils/fs" @@ -125,25 +125,7 @@ export class ContextManager { const timestamp = previousRequest.ts const { tokensIn, tokensOut, cacheWrites, cacheReads }: ClineApiReqInfo = JSON.parse(previousRequest.text) const totalTokens = (tokensIn || 0) + (tokensOut || 0) + (cacheWrites || 0) + (cacheReads || 0) - let contextWindow = api.getModel().info.contextWindow || 128_000 - // FIXME: hack to get anyone using openai compatible with deepseek to have the proper context window instead of the default 128k. We need a way for the user to specify the context window for models they input through openai compatible - if (api instanceof OpenAiHandler && api.getModel().id.toLowerCase().includes("deepseek")) { - contextWindow = 64_000 - } - let maxAllowedSize: number - switch (contextWindow) { - case 64_000: // deepseek models - maxAllowedSize = contextWindow - 27_000 - break - case 128_000: // most models - maxAllowedSize = contextWindow - 30_000 - break - case 200_000: // claude models - maxAllowedSize = contextWindow - 40_000 - break - default: - maxAllowedSize = Math.max(contextWindow - 40_000, contextWindow * 0.8) // for deepseek, 80% of 64k meant only ~10k buffer which was too small and resulted in users getting context window errors. - } + const { maxAllowedSize } = getContextWindowInfo(api) // This is the most reliable way to know when we're close to hitting the context window. if (totalTokens >= maxAllowedSize) { diff --git a/src/core/context-management/context-window-utils.ts b/src/core/context-management/context-window-utils.ts new file mode 100644 index 00000000..c5dfa9fb --- /dev/null +++ b/src/core/context-management/context-window-utils.ts @@ -0,0 +1,35 @@ +import { ApiHandler } from "../../api" +import { OpenAiHandler } from "../../api/providers/openai" + +/** + * Gets context window information for the given API handler + * + * @param api The API handler to get context window information for + * @returns An object containing the raw context window size and the effective max allowed size + */ +export function getContextWindowInfo(api: ApiHandler) { + let contextWindow = api.getModel().info.contextWindow || 128_000 + // FIXME: hack to get anyone using openai compatible with deepseek to have the proper context window instead of the default 128k. We need a way for the user to specify the context window for models they input through openai compatible + + // Handle special cases like DeepSeek + if (api instanceof OpenAiHandler && api.getModel().id.toLowerCase().includes("deepseek")) { + contextWindow = 64_000 + } + + let maxAllowedSize: number + switch (contextWindow) { + case 64_000: // deepseek models + maxAllowedSize = contextWindow - 27_000 + break + case 128_000: // most models + maxAllowedSize = contextWindow - 30_000 + break + case 200_000: // claude models + maxAllowedSize = contextWindow - 40_000 + break + default: + maxAllowedSize = Math.max(contextWindow - 40_000, contextWindow * 0.8) // for deepseek, 80% of 64k meant only ~10k buffer which was too small and resulted in users getting context window errors. + } + + return { contextWindow, maxAllowedSize } +} diff --git a/src/core/task/index.ts b/src/core/task/index.ts index 105c5f47..df32588c 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -13,6 +13,7 @@ import { ApiHandler, buildApiHandler } from "../../api" import { AnthropicHandler } from "../../api/providers/anthropic" import { ClineHandler } from "../../api/providers/cline" import { OpenRouterHandler } from "../../api/providers/openrouter" +import { getContextWindowInfo } from "../context-management/context-window-utils" import { ApiStream } from "../../api/transform/stream" import CheckpointTracker from "../../integrations/checkpoints/CheckpointTracker" import { DIFF_VIEW_URI_SCHEME, DiffViewProvider } from "../../integrations/editor/DiffViewProvider" @@ -3682,6 +3683,36 @@ export class Task { } } + // Add context window usage information + const { contextWindow, maxAllowedSize } = getContextWindowInfo(this.api) + + // Get the token count from the most recent API request to accurately reflect context management + const getTotalTokensFromApiReqMessage = (msg: ClineMessage) => { + if (!msg.text) { + return 0 + } + try { + const { tokensIn, tokensOut, cacheWrites, cacheReads } = JSON.parse(msg.text) + return (tokensIn || 0) + (tokensOut || 0) + (cacheWrites || 0) + (cacheReads || 0) + } catch (e) { + return 0 + } + } + + const modifiedMessages = combineApiRequests(combineCommandSequences(this.clineMessages.slice(1))) + const lastApiReqMessage = findLast(modifiedMessages, (msg) => { + if (msg.say !== "api_req_started") { + return false + } + return getTotalTokensFromApiReqMessage(msg) > 0 + }) + + const lastApiReqTotalTokens = lastApiReqMessage ? getTotalTokensFromApiReqMessage(lastApiReqMessage) : 0 + const usagePercentage = Math.round((lastApiReqTotalTokens / contextWindow) * 100) + + details += "\n# Context Window Usage" + details += `\n${lastApiReqTotalTokens.toLocaleString()} / ${(contextWindow / 1000).toLocaleString()}K tokens used (${usagePercentage}%)` + details += "\n\n# Current Mode" if (this.chatSettings.mode === "plan") { details += "\nPLAN MODE\n" + formatResponse.planModeInstructions()