Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import {
getCodexDualWindowCooldownMs,
getCodexModelScope,
parseCodexQuotaHeaders,
} from "../../executors/codex.ts";
import { updateCodexScopedQuotaState } from "@/lib/db/providers";
import type { CodexQuotaScope } from "../../config/codexQuotaScopes.ts";
export interface PersistCodexChildQuotaResult {
readonly scope: CodexQuotaScope;
readonly providerSpecificData: Record<string, unknown>;
readonly exhaustionLog: string | null;
}
/** Parse and atomically persist one virtual child's quota response evidence. */
export async function persistCodexChildQuotaResponse(params: {
connectionId: string;
model: string;
headers: Record<string, string>;
status: number;
fallbackRateLimitedUntil?: string | null;
}): Promise<PersistCodexChildQuotaResult | null> {
if (params.model.trim().length === 0) return null;
const quota = parseCodexQuotaHeaders(params.headers);
if (!quota) return null;
const scope = getCodexModelScope(params.model);
const quotaState = {
usage5h: quota.usage5h,
limit5h: quota.limit5h,
resetAt5h: quota.resetAt5h,
usage7d: quota.usage7d,
limit7d: quota.limit7d,
resetAt7d: quota.resetAt7d,
observedAt: new Date().toISOString(),
};
let exhaustedWindow: "5h" | "7d" | undefined;
let rateLimitedUntil: string | undefined;
if (params.status === 429) {
const exhausted = getCodexDualWindowCooldownMs(quota);
if (exhausted.cooldownMs > 0 && exhausted.window !== "none") {
exhaustedWindow = exhausted.window;
rateLimitedUntil =
exhausted.window === "7d" ? (quota.resetAt7d ?? undefined) : (quota.resetAt5h ?? undefined);
} else if (params.fallbackRateLimitedUntil) {
rateLimitedUntil = params.fallbackRateLimitedUntil;
}
}
const providerSpecificData = await updateCodexScopedQuotaState(params.connectionId, scope, {
quotaState,
exhaustedWindow: exhaustedWindow ?? null,
...(rateLimitedUntil
? {
rateLimitedUntil,
rateLimitSource: exhaustedWindow ? ("quota_reset" as const) : ("fallback" as const),
}
: params.status === 200
? { rateLimitedUntil: null }
: {}),
});
if (!providerSpecificData) return null;
return {
scope,
providerSpecificData,
exhaustionLog:
exhaustedWindow && rateLimitedUntil
? `Quota exhaustion on ${exhaustedWindow} window, cooldown until ${rateLimitedUntil}`
: null,
};
}