Skip to content

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

TypeScript

fix(mcp-host): read the control-api limiter 429 as rate_limited (#720)

Alfredo Lopez5ef206448180
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index b8b9e59cf..0d5c41119 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.450",
+ "version": "0.1.451",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.450",
+ "version": "0.1.451",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index c706445fe..1a73f94db 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.450",
+ "version": "0.1.451",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/llm/codexLlmProxyClient.ts b/mcp-host/src/llm/codexLlmProxyClient.ts
index 2059a1919..115c6a342 100644
--- a/mcp-host/src/llm/codexLlmProxyClient.ts
+++ b/mcp-host/src/llm/codexLlmProxyClient.ts
@@ -3,7 +3,7 @@ import {
parseCodexCompletionRequest,
} from '@clerum/llm-provider-attempt-contract'
import { fetchCauseCode, isConnectPhaseFailure } from './controlPlaneReachability'
-import { retryAfterMs } from './retryAfter'
+import { rateLimitedCode, retryAfterMs } from './retryAfter'
export const CODEX_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/codex/completions'
@@ -147,15 +147,16 @@ export class CodexLlmProxyClient {
return this.streamOnce(input, false)
}
const payload = (await response.json().catch(() => ({}))) as Record<string, unknown>
- // A 429 with no JSON code comes from a limiter in front of the proxy:
- // a rate limit, not a provider outage (G1-6, #720).
+ // A 429 is a rate limit, not a provider outage (G1-6, G1-11, #720): a
+ // limiter in front of the proxy may answer no JSON or a reason phrase,
+ // so only a machine code a 429 carries replaces `rate_limited`.
const code =
response.status === 413
? 'payload_too_large'
- : typeof payload.error === 'string'
- ? payload.error
- : response.status === 429
- ? 'rate_limited'
+ : response.status === 429
+ ? rateLimitedCode(payload.error)
+ : typeof payload.error === 'string'
+ ? payload.error
: 'provider_unavailable'
throw new CodexProxyError(
code,
diff --git a/mcp-host/src/llm/codexSubscription.ts b/mcp-host/src/llm/codexSubscription.ts
index f8273364d..387615b1b 100644
--- a/mcp-host/src/llm/codexSubscription.ts
+++ b/mcp-host/src/llm/codexSubscription.ts
@@ -661,9 +661,12 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {
} catch (err) {
// G1-9 (#720): a 429 that advised a short Retry-After is retried once,
// after that delay, under a new authorize: a redeemed ticket cannot be
- // reused. A caller that pins the attempt index owns its own retries.
+ // reused. The 429 may come from the proxy or from control-api's
+ // authorize limiter (G1-11). A caller that pins the attempt index owns
+ // its own retries.
const waitMs =
- context.providerAttemptIndex === undefined && err instanceof CodexProxyError
+ context.providerAttemptIndex === undefined &&
+ (err instanceof CodexProxyError || err instanceof CodexAuthorizeError)
? rateLimitRetryDelayMs(err.code, err.retryAfterMs)
: undefined
if (waitMs === undefined) throw err
diff --git a/mcp-host/src/llm/grokLlmProxyClient.ts b/mcp-host/src/llm/grokLlmProxyClient.ts
index 62fa8dc27..990c26fa2 100644
--- a/mcp-host/src/llm/grokLlmProxyClient.ts
+++ b/mcp-host/src/llm/grokLlmProxyClient.ts
@@ -1,5 +1,5 @@
import { fetchCauseCode, isConnectPhaseFailure } from './controlPlaneReachability'
-import { retryAfterMs } from './retryAfter'
+import { rateLimitedCode, retryAfterMs } from './retryAfter'
export const GROK_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/grok/completions'
@@ -136,15 +136,17 @@ export class GrokLlmProxyClient {
const payload = (await response.json().catch(() => ({}))) as Record<string, unknown>
// A 413 with no JSON code comes from the gateway in front of the proxy
// (nginx `client_max_body_size`): a size refusal of this request, never a
- // provider outage (#739). A code the 413 carries still wins. A 429 with
- // no JSON code likewise comes from a limiter: a rate limit (G1-6, #720).
+ // provider outage (#739). A code the 413 carries still wins. A 429 is a
+ // rate limit (G1-6, G1-11, #720): a limiter may answer no JSON or a
+ // reason phrase, so only a machine code a 429 carries replaces
+ // `rate_limited`.
const code =
- typeof payload.error === 'string'
- ? payload.error
- : response.status === 413
- ? 'payload_too_large'
- : response.status === 429
- ? 'rate_limited'
+ response.status === 429
+ ? rateLimitedCode(payload.error)
+ : typeof payload.error === 'string'
+ ? payload.error
+ : response.status === 413
+ ? 'payload_too_large'
: 'provider_unavailable'
throw new GrokProxyError(
code,
diff --git a/mcp-host/src/llm/grokSubscription.ts b/mcp-host/src/llm/grokSubscription.ts
index 9eae0436d..cca247a76 100644
--- a/mcp-host/src/llm/grokSubscription.ts
+++ b/mcp-host/src/llm/grokSubscription.ts
@@ -510,9 +510,12 @@ export class GrokSubscriptionProvider implements SingleTurnProvider {
} catch (err) {
// G1-9 (#720): a 429 that advised a short Retry-After is retried once,
// after that delay, under a new authorize: a redeemed ticket cannot be
- // reused. A caller that pins the attempt index owns its own retries.
+ // reused. The 429 may come from the proxy or from control-api's
+ // authorize limiter (G1-11). A caller that pins the attempt index owns
+ // its own retries.
const waitMs =
- context.providerAttemptIndex === undefined && err instanceof GrokProxyError
+ context.providerAttemptIndex === undefined &&
+ (err instanceof GrokProxyError || err instanceof CodexAuthorizeError)
? rateLimitRetryDelayMs(err.code, err.retryAfterMs)
: undefined
if (waitMs === undefined) throw err
diff --git a/mcp-host/src/llm/providerAttemptAuthorizer.ts b/mcp-host/src/llm/providerAttemptAuthorizer.ts
index f5e9630f8..53caa7549 100644
--- a/mcp-host/src/llm/providerAttemptAuthorizer.ts
+++ b/mcp-host/src/llm/providerAttemptAuthorizer.ts
@@ -5,6 +5,7 @@ import {
requestBodyLimitBytes,
} from '@clerum/llm-provider-attempt-contract'
import { fetchCauseCode, isConnectPhaseFailure } from './controlPlaneReachability'
+import { rateLimitedCode, retryAfterMs } from './retryAfter'
export const AUTHORIZE_PATH = '/api/v1/mcp-host/llm/provider-attempts/authorize'
@@ -19,7 +20,9 @@ export const AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES = ENVELOPE_ALLOWANCE_BYTES
export class CodexAuthorizeError extends Error {
constructor(
readonly code: string,
- message: string
+ message: string,
+ // G1-11 (#720): the delay a 429 advised, read from its Retry-After.
+ readonly retryAfterMs?: number
) {
super(message)
this.name = 'CodexAuthorizeError'
@@ -140,21 +143,24 @@ export class ProviderAttemptAuthorizer {
// Every 413 is a size refusal of this request, never a provider outage
// (#731): control-api answers `payload_too_large`, and the gateway in
// front of it (nginx `client_max_body_size`) answers with no JSON code.
- // A 429 with no JSON code comes from a limiter in front of control-api:
- // a rate limit, not a provider outage (G1-6, #720).
+ // A 429 is a rate limit, not a provider outage (G1-6, G1-11, #720):
+ // control-api's own authorize limiters answer a reason phrase and a
+ // gateway limiter answers no JSON, so only a machine code a 429 carries
+ // replaces `rate_limited`.
const code =
response.status === 413
? 'payload_too_large'
- : typeof payload.error === 'string'
- ? payload.error
- : response.status === 429
- ? 'rate_limited'
+ : response.status === 429
+ ? rateLimitedCode(payload.error)
+ : typeof payload.error === 'string'
+ ? payload.error
: 'provider_unavailable'
throw new CodexAuthorizeError(
code,
code === 'payload_too_large'
? 'Codex request is too large; use fewer or smaller images, or reduce context'
- : `authorize failed with ${response.status}`
+ : `authorize failed with ${response.status}`,
+ response.status === 429 ? retryAfterMs(response) : undefined
)
}
for (const key of LEAK_KEYS) {
diff --git a/mcp-host/src/llm/rateLimitRetry.ts b/mcp-host/src/llm/rateLimitRetry.ts
index c52c247e1..3a4875f3f 100644
--- a/mcp-host/src/llm/rateLimitRetry.ts
+++ b/mcp-host/src/llm/rateLimitRetry.ts
@@ -8,8 +8,9 @@ import { withAbort } from '../core/adapters/abortableLlmPort'
export const RATE_LIMIT_RETRY_MAX_WAIT_MS = 30_000
/**
- * The wait before the single retry of a proxy error, or `undefined` when it is
- * not retried: only a `rate_limited` that carried a Retry-After within
+ * The wait before the single retry of a proxy or authorize error, or
+ * `undefined` when it is not retried: only a `rate_limited` that carried a
+ * Retry-After within
* {@link RATE_LIMIT_RETRY_MAX_WAIT_MS}. `control_plane_unavailable` and every
* other code are never retried here.
*/
diff --git a/mcp-host/src/llm/retryAfter.ts b/mcp-host/src/llm/retryAfter.ts
index 73714db50..241d69fa2 100644
--- a/mcp-host/src/llm/retryAfter.ts
+++ b/mcp-host/src/llm/retryAfter.ts
@@ -1,9 +1,25 @@
// G1-6 (#720): the delay a 429 advises, in milliseconds. Only delta-seconds
// from 1 to 3600 are accepted, the rule `gfsClient.ts` applies to gfsc's
// limiter; an HTTP-date, a fraction or anything out of range is treated as
-// absent, never guessed.
+// absent, never guessed. The proxies parse the upstream header with the same
+// rule in their own copies (`retryAfterSeconds` in `codexTransport.ts` and
+// `grokTransport.ts`, #799); a change here goes there too.
const MAX_RETRY_AFTER_SECONDS = 3600
+// The shape of a code in a JSON `error`: what control-api, the proxies and
+// their gateways answer. A reason phrase is not one.
+const MACHINE_CODE = /^[a-z][a-z0-9_]*$/
+
+/**
+ * The code of a 429 (G1-11, #720): the machine code its JSON `error` carries
+ * (`rate_limited`, `budget_denied`, …), else `rate_limited`. control-api's own
+ * limiters answer `{ "error": "Too Many Requests" }`, a reason phrase, and a
+ * gateway limiter answers no JSON at all; both are a rate limit.
+ */
+export function rateLimitedCode(error: unknown): string {
+ return typeof error === 'string' && MACHINE_CODE.test(error) ? error : 'rate_limited'
+}
+
export function retryAfterMs(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