Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(server): classify ContextWindowExceededError as context_too_large (#1527)

Siddhant Badolab097a227b5e2
diff --git a/server/src/__tests__/lib/exhaustion-statuses.test.ts b/server/src/__tests__/lib/exhaustion-statuses.test.ts
index 5dc45820..73238b78 100644
--- a/server/src/__tests__/lib/exhaustion-statuses.test.ts
+++ b/server/src/__tests__/lib/exhaustion-statuses.test.ts
@@ -111,6 +111,26 @@ describe('isContextTooLargeError', () => {
))).toBe('context_too_large');
});
+ it('flags Agnes AI / aggregator ContextWindowExceededError shapes (truncated OpenAIException body)', () => {
+ // Agnes AI returns "API error 400: …ContextWindowExceededError…" whose
+ // nested OpenAIException body is truncated ("The input (2250074 tokens) is
+ // longer than the mo…") before "maximum context length" ever appears, so the
+ // old marker list fell through to provider_bad_request instead of
+ // context_too_large.
+ const agnes = Object.assign(
+ new Error('Agnes AI API error 400: ***.ContextWindowExceededError: ***.BadRequestError: ContextWindowExceededError: OpenAIException - {"message":"The input (2250074 tokens) is longer than the mo..."}'),
+ { status: 400 },
+ );
+ expect(isContextTooLargeError(agnes)).toBe(true);
+ expect(classifyAttemptError(agnes)).toBe('context_too_large');
+ });
+
+ it('flags "input … longer than the max/maximum" wordings', () => {
+ expect(isContextTooLargeError(new Error('Input is longer than the maximum allowed'))).toBe(true);
+ expect(isContextTooLargeError(new Error('The input is longer than the max token limit'))).toBe(true);
+ expect(isContextTooLargeError(new Error('input (2250074 tokens) is longer than maximum'))).toBe(true);
+ });
+
it('flags literal HTTP 413s, including Groq TPM-ceiling 413s (Requested > Limit can never fit)', () => {
expect(isContextTooLargeError(Object.assign(
new Error('Groq API error 413: Request too large for model `llama-3.3-70b-versatile` in organization `org_x` on tokens per minute (TPM): Limit 30000, Requested 33476'),
diff --git a/server/src/lib/error-classify.ts b/server/src/lib/error-classify.ts
index a17a1a9a..898495a5 100644
--- a/server/src/lib/error-classify.ts
+++ b/server/src/lib/error-classify.ts
@@ -472,12 +472,22 @@ export function isContextTooLargeError(err: any): boolean {
if (typeof err?.code === 'string' && err.code.toLowerCase() === 'context_length_exceeded') return true;
const msg = (err?.message ?? '').toLowerCase();
return msg.includes('context_length_exceeded')
+ // Agnes AI and some OpenAI-compat aggregators raise a bare
+ // ContextWindowExceededError whose OpenAIException body arrives truncated
+ // mid-sentence ("The input (2250074 tokens) is longer than the mo…"), so
+ // the "maximum context length" marker below never sees its target text and
+ // the error was mis-bucketed as provider_bad_request. The exception class
+ // name is the only reliable signal left in that shape.
+ || msg.includes('contextwindowexceeded')
|| msg.includes('maximum context length')
|| msg.includes('context length exceeded')
|| /exceeds[^.]*context (length|window|size)/.test(msg)
|| msg.includes('prompt is too long')
|| msg.includes('input is too long')
|| /input token count[^.]*exceeds/.test(msg)
+ // "The input (N tokens) is longer than the max/maximum" family, worded
+ // differently by aggregators that never surface an exception class.
+ || /input[^.]*longer than (the )?(max|maximum)/.test(msg)
|| msg.includes('exceeds the maximum number of tokens')
|| msg.includes('request too large')
|| msg.includes('payload too large')