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(routing): learn output caps from every provider's rejection phrasing (#1367)

tashdroidf78d1380a38b
diff --git a/server/src/__tests__/lib/output-cap.test.ts b/server/src/__tests__/lib/output-cap.test.ts
index b2cc5901..e2688237 100644
--- a/server/src/__tests__/lib/output-cap.test.ts
+++ b/server/src/__tests__/lib/output-cap.test.ts
@@ -20,11 +20,28 @@ describe('parseMaxTokensCeiling', () => {
expect(parseMaxTokensCeiling('max_completion_tokens must be at most 32768')).toBe(32768);
});
+ it('reads the ceiling from the phrasings the first cut missed', () => {
+ // Providers whose wording the original CEILING alternation did not carry:
+ // each one still benched the model instead of clamping and retrying.
+ expect(parseMaxTokensCeiling('OpenRouter error 400: max_tokens exceeds the maximum allowed value of 4096')).toBe(4096);
+ expect(parseMaxTokensCeiling('Together API error 400: max_tokens (128000) exceeds the model maximum output tokens of 65536')).toBe(65536);
+ expect(parseMaxTokensCeiling('Fireworks 400: max_tokens: 128000 is greater than the maximum 16384')).toBe(16384);
+ expect(parseMaxTokensCeiling('DeepInfra 400: max_tokens must be less than 8192')).toBe(8192);
+ expect(parseMaxTokensCeiling('custom endpoint 422: max_output_tokens: 100000 is at most 16384')).toBe(16384);
+ });
+
it('ignores context-window and unrelated errors', () => {
expect(parseMaxTokensCeiling('Cloudflare API error 413: AiError: Ai: The estimated number of input and maximum output tokens (24092) exceeded this model context window limit (24000).')).toBeNull();
expect(parseMaxTokensCeiling('Groq API error 413: Request too large for model `x` on tokens per minute (TPM): Limit 8000, Requested 17679')).toBeNull();
expect(parseMaxTokensCeiling('max_tokens must be a positive integer')).toBeNull();
});
+
+ it('never reads a number that belongs to some other limit', () => {
+ expect(parseMaxTokensCeiling('max_tokens: requested 16000, the number of input tokens exceeds 200 tokens')).toBeNull();
+ expect(parseMaxTokensCeiling('Request exceeds 30000 TPM limit; max_tokens=4096')).toBeNull();
+ expect(parseMaxTokensCeiling('quota exceeds 500 credits; set max_tokens lower')).toBeNull();
+ expect(parseMaxTokensCeiling('max_completion_tokens: messages must have less than 1000 items')).toBeNull();
+ });
});
describe('learned output caps', () => {
@@ -41,6 +58,12 @@ describe('learned output caps', () => {
expect(learnedOutputCap('ollama', 'nemotron-3-ultra')).toBeUndefined();
});
+ it('ignores a reading below the floor', () => {
+ expect(learnOutputCapFromError(route, Object.assign(new Error('max_tokens must be at most 512'), { status: 400 }))).toBeNull();
+ expect(learnedOutputCap('ollama', 'nemotron-3-ultra')).toBeUndefined();
+ expect(learnOutputCapFromError(route, Object.assign(new Error('max_tokens must be at most 1024'), { status: 400 }))).toBe(1024);
+ });
+
it('lowers the route output budget to the learned cap', () => {
expect(routeOutputBudget(route, 20000)).toBe(242144);
learnOutputCapFromError(route, Object.assign(new Error(OLLAMA), { status: 400 }));
diff --git a/server/src/lib/output-cap.ts b/server/src/lib/output-cap.ts
index 1358b9f9..18b13295 100644
--- a/server/src/lib/output-cap.ts
+++ b/server/src/lib/output-cap.ts
@@ -17,8 +17,20 @@ const learned = new Map<string, number>();
const MAX_TOKENS_PARAM = /\b(max_tokens|max_completion_tokens|max_output_tokens|maxoutputtokens|max_new_tokens)\b/;
// The number that follows a ceiling phrase: "less than or equal to `65536`",
-// "maximum output tokens (65536)", "at most 32768", "no more than 8192".
-const CEILING = /(?:less than or equal to|at most|maximum output tokens|no (?:more|greater) than|cannot exceed|must not exceed)\W{0,4}(\d{3,7})/;
+// "maximum output tokens (65536)", "at most 32768", "no more than 8192",
+// "exceeds the maximum allowed value of 4096" (OpenRouter), "greater than the
+// maximum 16384" (Fireworks), "must be less than 8192" (DeepInfra). No bare
+// "exceeds N" or "less than N": those read any number in the message ("input
+// tokens exceeds 200", "exceeds 30000 TPM").
+const CEILING_PHRASE = '(?:less than or equal to|must be less than|at most|no (?:more|greater) than|(?:cannot|must not|can not) exceed|greater than the maximum|maximum output tokens|maximum allowed value|max output tokens|maximum value|output limit|maximum is)\\W{0,4}(?:of\\s+)?\\W{0,2}(\\d{3,7})';
+// The ceiling phrase must follow the parameter name within the same clause
+// (60 characters, no '.' or ';' in between), so a number that belongs to some
+// other limit in a message that merely mentions max_tokens is never learned.
+const CEILING = new RegExp(`\\b(?:max_tokens|max_completion_tokens|max_output_tokens|maxoutputtokens|max_new_tokens)\\b[^.;]{0,60}?${CEILING_PHRASE}`);
+// A learned ceiling below this is almost certainly a misread (a real output
+// limit this small is not something a client would hit by accident), and a
+// wrong low cap would truncate every reply on the model until restart.
+export const MIN_LEARNED_OUTPUT_CAP = 1024;
function capKey(platform: string, modelId: string): string {
return `${platform}:${modelId}`;
@@ -38,12 +50,13 @@ export function parseMaxTokensCeiling(message: unknown): number | null {
}
/** Learn the ceiling from a max_tokens rejection. Returns it, or null when the
- * error is not one. The lowest reading wins. */
+ * error is not one, or when the reading is below MIN_LEARNED_OUTPUT_CAP. The
+ * lowest reading wins. */
export function learnOutputCapFromError(route: { platform: string; modelId: string }, err: any): number | null {
const status = typeof err?.status === 'number' ? err.status : 0;
if (status !== 0 && status !== 400 && status !== 422) return null;
const ceiling = parseMaxTokensCeiling(err?.message);
- if (ceiling == null) return null;
+ if (ceiling == null || ceiling < MIN_LEARNED_OUTPUT_CAP) return null;
const key = capKey(route.platform, route.modelId);
const current = learned.get(key);
learned.set(key, current == null ? ceiling : Math.min(current, ceiling));