Skip to content

Commit 8334f08

Browse files
authored
Update the max_tokens fallback logic in the sliding window (#5993)
1 parent 0500894 commit 8334f08

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/core/sliding-window/__tests__/sliding-window.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,9 +1103,9 @@ describe("Sliding Window", () => {
11031103
expect(result2.prevContextTokens).toBe(50001)
11041104
})
11051105

1106-
it("should use 20% of context window as buffer when maxTokens is undefined", async () => {
1106+
it("should use ANTHROPIC_DEFAULT_MAX_TOKENS as buffer when maxTokens is undefined", async () => {
11071107
const modelInfo = createModelInfo(100000, undefined)
1108-
// Max tokens = 100000 - (100000 * 0.2) = 80000
1108+
// Max tokens = 100000 - ANTHROPIC_DEFAULT_MAX_TOKENS = 100000 - 8192 = 91808
11091109

11101110
// Create messages with very small content in the last one to avoid token overflow
11111111
const messagesWithSmallContent = [
@@ -1117,7 +1117,7 @@ describe("Sliding Window", () => {
11171117
// Below max tokens and buffer - no truncation
11181118
const result1 = await truncateConversationIfNeeded({
11191119
messages: messagesWithSmallContent,
1120-
totalTokens: 69999, // Well below threshold + dynamic buffer
1120+
totalTokens: 81807, // Well below threshold + dynamic buffer (91808 - 10000 = 81808)
11211121
contextWindow: modelInfo.contextWindow,
11221122
maxTokens: modelInfo.maxTokens,
11231123
apiHandler: mockApiHandler,
@@ -1132,13 +1132,13 @@ describe("Sliding Window", () => {
11321132
messages: messagesWithSmallContent,
11331133
summary: "",
11341134
cost: 0,
1135-
prevContextTokens: 69999,
1135+
prevContextTokens: 81807,
11361136
})
11371137

11381138
// Above max tokens - truncate
11391139
const result2 = await truncateConversationIfNeeded({
11401140
messages: messagesWithSmallContent,
1141-
totalTokens: 80001, // Above threshold
1141+
totalTokens: 81809, // Above threshold (81808)
11421142
contextWindow: modelInfo.contextWindow,
11431143
maxTokens: modelInfo.maxTokens,
11441144
apiHandler: mockApiHandler,
@@ -1153,7 +1153,7 @@ describe("Sliding Window", () => {
11531153
expect(result2.messages.length).toBe(3) // Truncated with 0.5 fraction
11541154
expect(result2.summary).toBe("")
11551155
expect(result2.cost).toBe(0)
1156-
expect(result2.prevContextTokens).toBe(80001)
1156+
expect(result2.prevContextTokens).toBe(81809)
11571157
})
11581158

11591159
it("should handle small context windows appropriately", async () => {

src/core/sliding-window/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { TelemetryService } from "@roo-code/telemetry"
55
import { ApiHandler } from "../../api"
66
import { MAX_CONDENSE_THRESHOLD, MIN_CONDENSE_THRESHOLD, summarizeConversation, SummarizeResponse } from "../condense"
77
import { ApiMessage } from "../task-persistence/apiMessages"
8+
import { ANTHROPIC_DEFAULT_MAX_TOKENS } from "@roo-code/types"
89

910
/**
1011
* Default percentage of the context window to use as a buffer when deciding when to truncate
@@ -105,7 +106,7 @@ export async function truncateConversationIfNeeded({
105106
let error: string | undefined
106107
let cost = 0
107108
// Calculate the maximum tokens reserved for response
108-
const reservedTokens = maxTokens || contextWindow * 0.2
109+
const reservedTokens = maxTokens || ANTHROPIC_DEFAULT_MAX_TOKENS
109110

110111
// Estimate tokens for the last message (which is always a user message)
111112
const lastMessage = messages[messages.length - 1]

0 commit comments

Comments
 (0)