Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(llm-proxy): map an upstream 429 to rate_limited and forward Retry-After

Alfredo Lopez5eb8d740e086
diff --git a/codex-llm-proxy/src/codexTransport.ts b/codex-llm-proxy/src/codexTransport.ts
index 817e6c71d..09d286914 100644
--- a/codex-llm-proxy/src/codexTransport.ts
+++ b/codex-llm-proxy/src/codexTransport.ts
@@ -396,11 +396,30 @@ async function dispatchUpstreamStream(
'upstream rejected the Codex credential'
)
}
+ if (response.status === 429) {
+ const retryAfter = retryAfterSeconds(response)
+ throw new CodexTransportError(
+ 'rate_limited',
+ 'upstream rate limited the Codex request',
+ retryAfter === undefined ? undefined : { retryAfterSeconds: retryAfter }
+ )
+ }
throw new CodexTransportError('provider_unavailable', 'upstream completion failed')
}
return consumeSse(response.body, input.onFrame, signal, names, deadline)
}
+// G1-1 (#720): Retry-After is forwarded only as whole seconds in 1..3600, the
+// rule mcp-host's gfsClient applies. An HTTP date or any other value is absent.
+const MAX_RETRY_AFTER_SECONDS = 3600
+
+function retryAfterSeconds(response: Response): number | undefined {
+ const raw = response.headers.get('retry-after')?.trim()
+ if (raw === undefined || !/^[1-9][0-9]{0,3}$/.test(raw)) return undefined
+ const seconds = Number(raw)
+ return seconds <= MAX_RETRY_AFTER_SECONDS ? seconds : undefined
+}
+
// R9-6: a non-success completion body is read only to find a context-window
// refusal. The recorded refusal object is well under 1 KiB; past this bound the
// body is cancelled and the status mapping stands.
diff --git a/codex-llm-proxy/src/server.ts b/codex-llm-proxy/src/server.ts
index 226acca78..eaa2038db 100644
--- a/codex-llm-proxy/src/server.ts
+++ b/codex-llm-proxy/src/server.ts
@@ -625,6 +625,11 @@ export function createProxyApps(
// replaced; res.json() keeps an existing content-type.
res.removeHeader('cache-control')
res.setHeader('content-type', 'application/json; charset=utf-8')
+ // G1-1: an upstream Retry-After reaches the Host only on this path; an
+ // SSE error frame carries the code alone.
+ const retryAfter =
+ err instanceof CodexTransportError ? err.details?.retryAfterSeconds : undefined
+ if (typeof retryAfter === 'number') res.setHeader('retry-after', String(retryAfter))
res.status(mapped.status).json({ error: mapped.code })
} finally {
stopHeartbeat?.()
@@ -781,6 +786,8 @@ const ATTEMPT_ERROR_STATUS: Record<string, number> = {
// A reserved body that was not read within BODY_READ_DEADLINE_MS (R9-M-B).
request_timeout: 408,
ticket_replayed: 409,
+ // An upstream 429 (G1-1, #720); its Retry-After travels as a header.
+ rate_limited: 429,
tool_call_limit_exceeded: 422,
invalid_tool_arguments: 422,
// The attempt ran for its whole stream budget. Retrying the same request
diff --git a/grok-llm-proxy/src/grokTransport.ts b/grok-llm-proxy/src/grokTransport.ts
index d8434a7b6..102bbbfaa 100644
--- a/grok-llm-proxy/src/grokTransport.ts
+++ b/grok-llm-proxy/src/grokTransport.ts
@@ -441,11 +441,30 @@ async function dispatchUpstreamStream(
'upstream entitlement denied the Grok request'
)
}
+ if (response.status === 429) {
+ const retryAfter = retryAfterSeconds(response)
+ throw new GrokTransportError(
+ 'rate_limited',
+ 'upstream rate limited the Grok request',
+ retryAfter === undefined ? undefined : { retryAfterSeconds: retryAfter }
+ )
+ }
throw new GrokTransportError('provider_unavailable', 'upstream completion failed')
}
return consumeSse(response.body, input.onFrame, signal, names, deadline)
}
+// G1-1 (#720): Retry-After is forwarded only as whole seconds in 1..3600, the
+// rule mcp-host's gfsClient applies. An HTTP date or any other value is absent.
+const MAX_RETRY_AFTER_SECONDS = 3600
+
+function retryAfterSeconds(response: Response): number | undefined {
+ const raw = response.headers.get('retry-after')?.trim()
+ if (raw === undefined || !/^[1-9][0-9]{0,3}$/.test(raw)) return undefined
+ const seconds = Number(raw)
+ return seconds <= MAX_RETRY_AFTER_SECONDS ? seconds : undefined
+}
+
// R10 (M1): a non-success completion body is read once, up to this bound, for
// the log hint and to find a context-window refusal. The recorded refusal is
// about 200 bytes; past the bound the read is cancelled, the prefix still feeds
diff --git a/grok-llm-proxy/src/server.ts b/grok-llm-proxy/src/server.ts
index 162541b28..939185be5 100644
--- a/grok-llm-proxy/src/server.ts
+++ b/grok-llm-proxy/src/server.ts
@@ -498,6 +498,11 @@ export function createProxyApps(
// replaced; res.json() keeps an existing content-type.
res.removeHeader('cache-control')
res.setHeader('content-type', 'application/json; charset=utf-8')
+ // G1-1: an upstream Retry-After reaches the Host only on this path; an
+ // SSE error frame carries the code alone.
+ const retryAfter =
+ err instanceof GrokTransportError ? err.details?.retryAfterSeconds : undefined
+ if (typeof retryAfter === 'number') res.setHeader('retry-after', String(retryAfter))
res.status(mapped.status).json({ error: mapped.code })
} finally {
stopHeartbeat?.()
@@ -654,6 +659,8 @@ const ATTEMPT_ERROR_STATUS: Record<string, number> = {
// A reserved body that was not read within BODY_READ_DEADLINE_MS (R9-M-B).
request_timeout: 408,
ticket_replayed: 409,
+ // An upstream 429 (G1-1, #720); its Retry-After travels as a header.
+ rate_limited: 429,
tool_call_limit_exceeded: 422,
tool_call_arguments_exceeded: 422,
invalid_tool_arguments: 422,
diff --git a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json
index 982e7b030..61c1a9872 100644
--- a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json
+++ b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json
@@ -61,6 +61,7 @@
"budget_denied",
"connection_unavailable",
"provider_unavailable",
+ "rate_limited",
"origin_denied",
"ticket_invalid",
"ticket_replayed",
diff --git a/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json b/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json
index 66141be98..a4af0ec41 100644
--- a/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json
+++ b/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json
@@ -75,6 +75,7 @@
"budget_denied",
"connection_unavailable",
"provider_unavailable",
+ "rate_limited",
"origin_denied",
"ticket_invalid",
"ticket_replayed",
diff --git a/tests/e2e/fixtures/package-lock.json b/tests/e2e/fixtures/package-lock.json
index 3f447f36c..dcb87e86f 100644
--- a/tests/e2e/fixtures/package-lock.json
+++ b/tests/e2e/fixtures/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.97",
+ "version": "1.0.98",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.97",
+ "version": "1.0.98",
"dependencies": {
"@kubernetes/client-node": "^1.0.0"
},
diff --git a/tests/e2e/fixtures/package.json b/tests/e2e/fixtures/package.json
index 8c211caa3..cd7c21746 100644
--- a/tests/e2e/fixtures/package.json
+++ b/tests/e2e/fixtures/package.json
@@ -1,6 +1,6 @@
{
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.97",
+ "version": "1.0.98",
"description": "E2E test fixtures and utilities for Clerum testing",
"type": "module",
"main": "index.ts",