Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(branding): fail fast when TypeSafe asks Jev calls to back off (#5179)

Abimael Martell9d18f53ecf28
diff --git a/apps/api/src/__tests__/lib/branding/jev.test.ts b/apps/api/src/__tests__/lib/branding/jev.test.ts
index e8f4f4041..8db817cbd 100644
--- a/apps/api/src/__tests__/lib/branding/jev.test.ts
+++ b/apps/api/src/__tests__/lib/branding/jev.test.ts
@@ -307,7 +307,17 @@ describe("branding with Jev", () => {
expect(request.model).toBe("jev-latest");
expect(options).toEqual({
timeout: 5000,
- retry: { maxRetries: 1, apiTimeoutError: false },
+ retry: {
+ maxRetries: 1,
+ apiTimeoutError: false,
+ httpStatuses: new Set([
+ 408,
+ ...Array.from({ length: 100 }, (_, i) => 500 + i).filter(
+ s => s !== 529,
+ ),
+ ]),
+ maxRetryAfterMs: 1000,
+ },
});
expect(Object.keys(request.questions.logo.criteria)).toEqual([
"logo_0",
@@ -401,6 +411,46 @@ describe("branding with Jev", () => {
expect(result.buttonClassification.primaryButtonIndex).toBe(-1);
});
+ it("fails fast when TypeSafe asks to back off, and retries a 503 once without waiting it out", async () => {
+ // The options Jev passes, run through the real SDK against a fake server.
+ mocks.systemOne.mockResolvedValueOnce(jevResponse());
+ await enhanceBrandingWithLLM(baseInput(new CostTracking()));
+ const [request, options] = mocks.systemOne.mock.calls[0];
+ const { TypeSafeClient } =
+ await vi.importActual<typeof import("@typesafe-ai/sdk")>(
+ "@typesafe-ai/sdk",
+ );
+ const call = async (status: number) => {
+ const fetch = vi.fn(
+ async () =>
+ new Response(JSON.stringify({ error: { message: "busy" } }), {
+ status,
+ headers: {
+ "content-type": "application/json",
+ "retry-after": "10",
+ },
+ }),
+ );
+ const client = new TypeSafeClient({
+ apiKey: "test",
+ logLevel: "off",
+ fetch,
+ });
+ const started = Date.now();
+ await expect(client.systemOne(request, options)).rejects.toThrow();
+ return { attempts: fetch.mock.calls.length, ms: Date.now() - started };
+ };
+
+ for (const status of [529, 429]) {
+ const r = await call(status);
+ expect(r.attempts).toBe(1);
+ expect(r.ms).toBeLessThan(500);
+ }
+ const r = await call(503);
+ expect(r.attempts).toBe(2);
+ expect(r.ms).toBeLessThan(2000); // not the 10 s Retry-After
+ });
+
it("stops calling Jev for a minute after repeated failures, then tries again", async () => {
vi.useFakeTimers({ toFake: ["Date"] });
vi.setSystemTime(Date.parse("2026-10-08T12:00:00Z"));
diff --git a/apps/api/src/lib/branding/jev.ts b/apps/api/src/lib/branding/jev.ts
index 9dcdfea81..cd691eeaf 100644
--- a/apps/api/src/lib/branding/jev.ts
+++ b/apps/api/src/lib/branding/jev.ts
@@ -3,6 +3,7 @@ import type {
JsonValue,
NoulResponse,
Question,
+ RequestOptions,
SystemOneResult,
} from "@typesafe-ai/sdk";
import { formatHex, hsl, parse } from "culori";
@@ -32,6 +33,21 @@ const JEV_MODEL = "jev-latest";
const JEV_INPUT_USD_PER_MTOK = 0.042;
const DEFAULT_TIMEOUT_MS = 5000;
+// One quick retry, for a transient server error only. A call that ran out of
+// time, or a 429 or 529 asking us to back off, is answered by the LLM instead:
+// waiting can take longer than the whole LLM fallback, and adds load to a
+// struggling TypeSafe. A server-requested delay over a second is not waited
+// out either (the SDK backs off ~0.5 s instead).
+const JEV_RETRY: RequestOptions["retry"] = {
+ maxRetries: 1,
+ apiTimeoutError: false,
+ httpStatuses: new Set([
+ 408,
+ ...Array.from({ length: 100 }, (_, i) => 500 + i).filter(s => s !== 529),
+ ]),
+ maxRetryAfterMs: 1000,
+};
+
// Circuit breaker: after this many calls in a row fail (timeouts, 5xx), skip
// Jev for the cooldown and answer with the LLM, so an outage doesn't add the
// timeout to every branding request. After the cooldown calls go through
@@ -936,9 +952,7 @@ export async function enhanceBrandingWithJev(
},
{
timeout: config.BRANDING_JEV_TIMEOUT_MS ?? DEFAULT_TIMEOUT_MS,
- // A call that ran out of time is answered by the LLM instead:
- // retrying it doubles the wait, and the load on a slow TypeSafe.
- retry: { maxRetries: 1, apiTimeoutError: false },
+ retry: JEV_RETRY,
},
);
setSpanAttributes(span, {