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(providers): parse compound retry durations like "try again in 1m26s" (#1500) (#1501)

Aymen Jeddouf8ef4ebaa9ee
diff --git a/server/src/__tests__/providers/stated-retry.test.ts b/server/src/__tests__/providers/stated-retry.test.ts
index 5eacddf7..6eab14de 100644
--- a/server/src/__tests__/providers/stated-retry.test.ts
+++ b/server/src/__tests__/providers/stated-retry.test.ts
@@ -63,6 +63,19 @@ describe('parseStatedRetryMs — prose', () => {
expect(parseStatedRetryMs('try again in 500ms')).toBe(500);
});
+ it('sums compound durations the way Groq and OpenAI write them', () => {
+ const body = { error: { message: 'Rate limit reached on requests per day (RPD): Limit 1000, Used 1000, Requested 1. Please try again in 1m26s. Need more requests? Upgrade to Dev Tier today' } };
+ expect(parseStatedRetryMs(body)).toBe(86_000);
+ expect(parseStatedRetryMs('Please try again in 7m12s.')).toBe(432_000);
+ expect(parseStatedRetryMs('Please try again in 6m0.512s.')).toBe(360_512);
+ expect(parseStatedRetryMs('try again in 2h3m')).toBe(7_380_000);
+ expect(parseStatedRetryMs('retry after 2 minutes 30 seconds')).toBe(150_000);
+ });
+
+ it('ignores a number that is not followed by a unit', () => {
+ expect(parseStatedRetryMs('please try again in 5 or so')).toBeUndefined();
+ });
+
it('requires an explicit retry phrase, so a stray number is never mistaken for a back-off', () => {
expect(parseStatedRetryMs('You have used 30 seconds of compute')).toBeUndefined();
expect(parseStatedRetryMs({ error: { message: 'model gpt-4 is 30s slower than usual' } })).toBeUndefined();
diff --git a/server/src/providers/base.ts b/server/src/providers/base.ts
index 27151f9a..4f35e875 100644
--- a/server/src/providers/base.ts
+++ b/server/src/providers/base.ts
@@ -47,10 +47,12 @@ export function parseRetryAfterMs(value: string | null | undefined): number | un
* digits, an optional fraction, then a literal `s` ("17s", "1.5s", "0.25s"). */
const PROTOBUF_DURATION = /^(\d+(?:\.\d+)?)s$/;
-/** "Please try again in 7.66s" / "retry after 30 seconds" / "try again in 2m".
- * Anchored on an explicit retry phrase so an unrelated number in an error
- * message can never be mistaken for a back-off. */
-const PROSE_RETRY = /(?:try again|retry)\s+(?:in|after)\s+(\d+(?:\.\d+)?)\s*(ms|s|sec|secs|second|seconds|m|min|mins|minute|minutes|h|hour|hours)\b/i;
+/** "Please try again in 7.66s" / "retry after 30 seconds" / "try again in 2m",
+ * and compound Go-style durations like Groq's "try again in 1m26s" or
+ * "6m0.512s". Anchored on an explicit retry phrase so an unrelated number in
+ * an error message can never be mistaken for a back-off. */
+const PROSE_RETRY = /(?:try again|retry)\s+(?:in|after)\s+(\d+(?:\.\d+)?\s*[a-z]+(?:\s*\d+(?:\.\d+)?\s*[a-z]+)*)/i;
+const DURATION_PART = /(\d+(?:\.\d+)?)\s*([a-z]+)/gi;
const UNIT_MS: Record<string, number> = {
ms: 1,
@@ -118,8 +120,13 @@ export function parseStatedRetryMs(body: unknown): number | undefined {
const text = typeof body === 'string' ? body : safeStringify(body);
const prose = text ? PROSE_RETRY.exec(text) : null;
if (prose) {
- const unit = UNIT_MS[prose[2].toLowerCase()];
- if (unit) return clampRetryMs(Number(prose[1]) * unit);
+ let total: number | undefined;
+ for (const [, amount, unitName] of prose[1].matchAll(DURATION_PART)) {
+ const unit = UNIT_MS[unitName.toLowerCase()];
+ if (!unit) break; // "try again in 5 or so" — stop at the first non-unit word
+ total = (total ?? 0) + Number(amount) * unit;
+ }
+ if (total !== undefined) return clampRetryMs(total);
}
return undefined;
}