Skip to content

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

1.5 KB
Raw Blame
import { shouldMarkAccountExhaustedFrom429 } from "@omniroute/open-sse/services/accountFallback.ts";
import {
  getCachedClaudeQuotaScopeDecision,
  markAccountExhaustedFrom429,
} from "@/domain/quotaCache";
import type { FailureKind } from "@/shared/utils/classify429";
import { shouldIsolateProbeFailures } from "@/shared/utils/probeOrigin";

export async function maybeMarkChatAccountExhaustedFrom429(input: {
  connectionId: string;
  provider: string;
  model: string;
  status: number;
  errorText: string;
  failureKind: FailureKind | undefined;
  passthroughModels: boolean | undefined;
}): Promise<void> {
  // Mark account as quota-exhausted only for explicit long-window quota signals.
  // A plain 429/high-traffic response should trigger fallback/cooldown, not poison
  // quotaCache as exhausted for 5 minutes while usage quota may still be available.
  if (
    input.status !== 429 ||
    !shouldMarkAccountExhaustedFrom429(
      input.provider,
      input.model,
      input.passthroughModels,
      input.failureKind,
      input.errorText
    ) ||
    getCachedClaudeQuotaScopeDecision({
      connectionId: input.connectionId,
      provider: input.provider,
      status: input.status,
      errorText: input.errorText,
      model: input.model,
    }).evidence !== "none"
  ) {
    return;
  }

  // T-PROBE: a probe must not poison the 5min quotaCache for real traffic (#9817).
  if (await shouldIsolateProbeFailures()) return;
  markAccountExhaustedFrom429(input.connectionId, input.provider);
}