Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/claude/outbound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* - errors: {type:"error", error:{type,message}}; may arrive mid-stream after HTTP 200.
*/
import { createHash } from "node:crypto";
import { httpStatusFromTerminalError } from "../lib/errors";
import { httpStatusFromTerminalError, isDefiniteContextOverflowMessage } from "../lib/errors";
import { isTransientUpstreamStatus } from "../lib/upstream-retry";
import {
isTranslatorBudgetExceededError,
Expand Down Expand Up @@ -70,8 +70,22 @@ export function anthropicErrorType(status: number): string {
}
}

/**
* Claude Code recognizes an overflowing prompt only by Anthropic's wording ("prompt is too long"),
* and only then runs its reactive compaction and the head-truncating retry inside /compact. A routed
* provider's own overflow text ("Your input exceeds the context window of this model") reads to it as
* a generic 400, so every later turn and every /compact resends the same oversized history and the
* session never recovers. Prefixed, never replaced: the provider text stays the evidence.
*/
function claudeCodeOverflowMessage(status: number, message: string, code?: string): string {
if (status === 429 || /prompt is too long/i.test(message)) return message;
return code === "context_length_exceeded" || isDefiniteContextOverflowMessage(message)
? `prompt is too long: ${message}`
: message;
}

export function anthropicErrorBody(status: number, message: string, type?: string, code?: string): Rec {
return { type: "error", error: { type: type ?? anthropicErrorType(status), message, ...(code ? { code } : {}) } };
return { type: "error", error: { type: type ?? anthropicErrorType(status), message: claudeCodeOverflowMessage(status, message, code), ...(code ? { code } : {}) } };
}

export function anthropicErrorResponse(status: number, message: string, type?: string, code?: string): Response {
Expand Down
23 changes: 1 addition & 22 deletions src/combos/failover.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseResetCooldownMs } from "../codex/routing";
import { classifyError, isCyberPolicyCode } from "../lib/errors";
import { classifyError, isCyberPolicyCode, isDefiniteContextOverflowMessage } from "../lib/errors";
import { isNonReplayableUpstreamCode } from "../lib/upstream-retry";
import type { OcxComboTarget } from "../types";
import { targetKey } from "./types";
Expand Down Expand Up @@ -616,30 +616,9 @@ function isProviderTargetContextOverflow(
/** A status can carry a verdict about the REQUEST; 401/403/429 speak about the credential. */
const CONTEXT_VERDICT_STATUSES: ReadonlySet<number> = new Set([400, 413, 422]);

/**
* Phrases a provider emits when the INPUT does not fit this model's context window. Matched
* against the innermost provider message only, so an unrelated refusal that merely quotes one
* of these tokens in a code field cannot authorize a replay.
*/
const DEFINITE_CONTEXT_OVERFLOW_PHRASES = [
"exceeds the context window",
"exceed the context window",
"context window exceeded",
"context length exceeded",
"maximum context length",
"maximum context window",
"too many tokens",
];

/** Wrapper envelopes unwrapped before the leaf message is read. */
const MAX_CONTEXT_OVERFLOW_ENVELOPES = 4;

function isDefiniteContextOverflowMessage(text: string): boolean {
const normalized = text.toLowerCase();
return normalized === "context_length_exceeded"
|| DEFINITE_CONTEXT_OVERFLOW_PHRASES.some(phrase => normalized.includes(phrase));
}

/**
* Confirm a context overflow from the provider MESSAGE rather than from a code token that
* merely appears somewhere in the envelope. An upstream controls both fields and can emit a
Expand Down
21 changes: 21 additions & 0 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,24 @@ export function httpStatusFromTerminalError(error: {
}
return 502;
}

/**
* Phrases a provider emits when the INPUT does not fit this model's context window. Matched
* against the innermost provider message only, so an unrelated refusal that merely quotes one
* of these tokens in a code field cannot authorize a replay.
*/
const DEFINITE_CONTEXT_OVERFLOW_PHRASES = [
"exceeds the context window",
"exceed the context window",
"context window exceeded",
"context length exceeded",
"maximum context length",
"maximum context window",
"too many tokens",
];

export function isDefiniteContextOverflowMessage(text: string): boolean {
const normalized = text.toLowerCase();
return normalized === "context_length_exceeded"
|| DEFINITE_CONTEXT_OVERFLOW_PHRASES.some(phrase => normalized.includes(phrase));
}
36 changes: 35 additions & 1 deletion tests/claude-integration/claude-529-mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,42 @@ test("non-transient upstream 400 stays 400 invalid_request_error; no retry", asy
});
expect(calls).toBe(1);
expect(response.status).toBe(400);
const json = await response.json() as { error?: { type?: string } };
const json = await response.json() as { error?: { type?: string; message?: string } };
expect(json.error?.type).toBe("invalid_request_error");
expect(json.error?.message).toBe("bad request shape");
} finally {
await server.stop(true);
await upstream.stop(true);
}
});

// Claude Code runs its reactive compaction and the head-truncating /compact retry only for Anthropic's
// "prompt is too long" wording. OpenAI's overflow text alone left a hosted session refusing every turn
// and every /compact (COMPACTION-400-REPRO, 2026-11-07).
test("upstream context-window 400 reaches Claude Code as prompt is too long, provider text kept", async () => {
const upstream = Bun.serve({
port: 0,
fetch() {
return Response.json({ error: {
message: "Your input exceeds the context window of this model. Please adjust your input and try again.",
type: "invalid_request_error", code: "context_length_exceeded",
} }, { status: 400 });
},
});
saveConfig(nativeConfig(`${upstream.url.toString().replace(/\/$/, "")}/v1`));
const server = startServer(0);
try {
const response = await fetch(new URL("/v1/messages", server.url), {
method: "POST",
headers: { "content-type": "application/json" },
body: messagesBody(),
});
expect(response.status).toBe(400);
const json = await response.json() as { error?: { type?: string; message?: string } };
expect(json.error?.type).toBe("invalid_request_error");
expect(json.error?.message).toBe(
"prompt is too long: Your input exceeds the context window of this model. Please adjust your input and try again.",
);
} finally {
await server.stop(true);
await upstream.stop(true);
Expand Down
Loading