From c773f36277c986bc45c47dd9022c8718ca37a3f8 Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 13:34:36 -0700 Subject: [PATCH] A 400 that only mentions context is not an overflow: a rate-limit retry ended a 2.5k-token run as too large --- src/engine.ts | 11 ++++++++++- test/turn-loss.test.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/engine.ts b/src/engine.ts index 97b497e..af55c8d 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -339,7 +339,15 @@ export type Overflow = { }; export function contextOverflow(body: string): Overflow | null { - if (!/context|n_ctx|too many tokens|maximum context length/i.test(body)) return null; + // Overflow wording, not the bare word "context": a pinned OpenRouter provider + // answered a rate-limit retry with a 400 that merely mentioned context, and a + // 2,476-token request was shed to nothing and the run ended (2026-10-07). + if ( + !/n_ctx|too many tokens|maximum context length|context (length|window|size)|exceeds? (the )?(available |maximum )?context|context.{0,40}(exceed|too (long|large))|prompt is too long|request too large/i.test( + body, + ) + ) + return null; // Every field the common servers use, most specific first. const win = /"n_ctx"\s*:\s*(\d+)/.exec(body) ?? @@ -6250,6 +6258,7 @@ export class Engine { if (target > 0) this.cfg.autoShedAtTokens = target; log?.append("note", { + body: body.slice(0, 600), text: `context window ${over.window || "unknown"}; server counted ${over.sent} where ` + `maat estimated ${bom.requestTotalEst} (x${this.tokenScale.toFixed(2)}) — ` + diff --git a/test/turn-loss.test.ts b/test/turn-loss.test.ts index fa8e7f0..e1cc1ff 100644 --- a/test/turn-loss.test.ts +++ b/test/turn-loss.test.ts @@ -832,6 +832,17 @@ describe("an endpoint too small for the conversation", () => { // would destroy a working conversation to fix something else. assert.equal(contextOverflow(`{"error":"invalid api key"}`), null); assert.equal(contextOverflow(`{"error":"unsupported tool_choice value"}`), null); + // Mentioning context is not refusing for size: a pinned provider's 400 after a + // rate-limit retry ended a 2,476-token run as "too large" (2026-10-07). + assert.equal( + contextOverflow(`{"error":{"message":"No endpoints available for the requested provider and context","code":400}}`), + null, + ); + assert.equal(contextOverflow(`{"error":"invalid message: context field missing"}`), null); + // Real overflow wordings still count. + assert.notEqual(contextOverflow(`{"error":"Prompt exceeds the maximum context of 131072 tokens"}`), null); + assert.notEqual(contextOverflow(`{"error":{"message":"prompt is too long: 210000 tokens > 200000 maximum"}}`), null); + assert.notEqual(contextOverflow(`{"error":"context_length_exceeded"}`), null); }); it("measures how wrong its own token estimate is", () => {