From 46fcd370c5edceae0434b2bc4a9884351c0c55ca Mon Sep 17 00:00:00 2001 From: Canyon Robins Date: Mon, 19 May 2025 18:28:30 -0700 Subject: [PATCH] account for system prompt when estimating new context size --- src/core/condense/index.ts | 4 ++++ src/core/sliding-window/index.ts | 5 ++++- src/core/task/Task.ts | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/core/condense/index.ts b/src/core/condense/index.ts index c02f7c7c47..367fb99208 100644 --- a/src/core/condense/index.ts +++ b/src/core/condense/index.ts @@ -62,6 +62,7 @@ export type SummarizeResponse = { export async function summarizeConversation( messages: ApiMessage[], apiHandler: ApiHandler, + systemPrompt?: string, ): Promise { const response: SummarizeResponse = { messages, cost: 0, summary: "" } const messagesToSummarize = getMessagesSinceLastSummary(messages.slice(0, -N_MESSAGES_TO_KEEP)) @@ -111,6 +112,9 @@ export async function summarizeConversation( // Count the tokens in the context for the next API request // We only estimate the tokens in summaryMesage if outputTokens is 0, otherwise we use outputTokens const contextMessages = outputTokens ? [...keepMessages] : [summaryMessage, ...keepMessages] + if (systemPrompt) { + contextMessages.unshift({ role: "user", content: systemPrompt }) + } const contextBlocks = contextMessages.flatMap((message) => typeof message.content === "string" ? [{ text: message.content, type: "text" as const }] : message.content, ) diff --git a/src/core/sliding-window/index.ts b/src/core/sliding-window/index.ts index 1938d8db9a..598cc473b2 100644 --- a/src/core/sliding-window/index.ts +++ b/src/core/sliding-window/index.ts @@ -53,6 +53,7 @@ export function truncateConversation(messages: ApiMessage[], fracToRemove: numbe * @param {number} maxTokens - The maximum number of tokens allowed. * @param {ApiHandler} apiHandler - The API handler to use for token counting. * @param {boolean} autoCondenseContext - Whether to use LLM summarization or sliding window implementation + * @param {string} systemPrompt - The system prompt, used for estimating the new context size after summarizing. * @returns {ApiMessage[]} The original or truncated conversation messages. */ @@ -63,6 +64,7 @@ type TruncateOptions = { maxTokens?: number | null apiHandler: ApiHandler autoCondenseContext?: boolean + systemPrompt?: string } type TruncateResponse = SummarizeResponse & { prevContextTokens: number } @@ -81,6 +83,7 @@ export async function truncateConversationIfNeeded({ maxTokens, apiHandler, autoCondenseContext, + systemPrompt, }: TruncateOptions): Promise { // Calculate the maximum tokens reserved for response const reservedTokens = maxTokens || contextWindow * 0.2 @@ -103,7 +106,7 @@ export async function truncateConversationIfNeeded({ if (effectiveTokens <= allowedTokens) { return { messages, summary: "", cost: 0, prevContextTokens: effectiveTokens } } else if (autoCondenseContext) { - const result = await summarizeConversation(messages, apiHandler) + const result = await summarizeConversation(messages, apiHandler, systemPrompt) if (messages !== result.messages) { return { ...result, prevContextTokens: effectiveTokens } } diff --git a/src/core/task/Task.ts b/src/core/task/Task.ts index d68391862c..b301904f7a 100644 --- a/src/core/task/Task.ts +++ b/src/core/task/Task.ts @@ -1479,6 +1479,7 @@ export class Task extends EventEmitter { contextWindow, apiHandler: this.api, autoCondenseContext, + systemPrompt, }) if (truncateResult.messages !== this.apiConversationHistory) { await this.overwriteApiConversationHistory(truncateResult.messages)