Skip to content
Open
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
11 changes: 10 additions & 1 deletion src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ??
Expand Down Expand Up @@ -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)}) — ` +
Expand Down
11 changes: 11 additions & 0 deletions test/turn-loss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading