Skip to content

Commit c153077

Browse files
Handle Anthropic token input too large (RooCodeInc#2407)
* correct calculation of quarter truncation * changeset * truncate and retry if we encounter context window exceeded error * optional chaining for error message check * changeset * Handle Anthropic input breaks context window error * changeset * remove extra changeset * remove logger * use optional chaining
1 parent 1796558 commit c153077

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

.changeset/eighty-tables-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"claude-dev": patch
3+
---
4+
5+
Handle input too large Anthropic

src/core/Cline.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ import { telemetryService } from "../services/telemetry/TelemetryService"
6767
import { ConversationTelemetryService, TelemetryChatMessage } from "../services/telemetry/ConversationTelemetryService"
6868
import pTimeout from "p-timeout"
6969
import { GlobalFileNames } from "../global-constants"
70-
import { checkIsOpenRouterContextWindowError } from "./context-management/context-error-handling"
70+
import {
71+
checkIsAnthropicContextWindowError,
72+
checkIsOpenRouterContextWindowError,
73+
} from "./context-management/context-error-handling"
74+
import { AnthropicHandler } from "../api/providers/anthropic"
7175

7276
const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution
7377

@@ -1432,9 +1436,20 @@ export class Cline {
14321436
this.isWaitingForFirstChunk = false
14331437
} catch (error) {
14341438
const isOpenRouter = this.api instanceof OpenRouterHandler || this.api instanceof ClineHandler
1439+
const isAnthropic = this.api instanceof AnthropicHandler
14351440
const isOpenRouterContextWindowError = checkIsOpenRouterContextWindowError(error) && isOpenRouter
1441+
const isAnthropicContextWindowError = checkIsAnthropicContextWindowError(error) && isAnthropic
1442+
1443+
if (isAnthropic && isAnthropicContextWindowError && !this.didAutomaticallyRetryFailedApiRequest) {
1444+
this.conversationHistoryDeletedRange = this.contextManager.getNextTruncationRange(
1445+
this.apiConversationHistory,
1446+
this.conversationHistoryDeletedRange,
1447+
"quarter", // Force aggressive truncation
1448+
)
1449+
await this.saveClineMessages()
14361450

1437-
if (isOpenRouter && !this.didAutomaticallyRetryFailedApiRequest) {
1451+
this.didAutomaticallyRetryFailedApiRequest = true
1452+
} else if (isOpenRouter && !this.didAutomaticallyRetryFailedApiRequest) {
14381453
if (isOpenRouterContextWindowError) {
14391454
this.conversationHistoryDeletedRange = this.contextManager.getNextTruncationRange(
14401455
this.apiConversationHistory,
@@ -1451,7 +1466,7 @@ export class Cline {
14511466
// request failed after retrying automatically once, ask user if they want to retry again
14521467
// note that this api_req_failed ask is unique in that we only present this option if the api hasn't streamed any content yet (ie it fails on the first chunk due), as it would allow them to hit a retry button. However if the api failed mid-stream, it could be in any arbitrary state where some tools may have executed, so that error is handled differently and requires cancelling the task entirely.
14531468

1454-
if (isOpenRouterContextWindowError) {
1469+
if (isOpenRouterContextWindowError || isAnthropicContextWindowError) {
14551470
const truncatedConversationHistory = this.contextManager.getTruncatedMessages(
14561471
this.apiConversationHistory,
14571472
this.conversationHistoryDeletedRange,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
export function checkIsOpenRouterContextWindowError(error: any): boolean {
22
return error.code === 400 && error.message?.includes("context length")
33
}
4+
5+
export function checkIsAnthropicContextWindowError(response: any): boolean {
6+
return (
7+
response?.error?.error?.type === "invalid_request_error" &&
8+
response?.error?.error?.message?.includes("prompt is too long")
9+
)
10+
}

0 commit comments

Comments
 (0)