feat(api): add opt-in API-key provider quota-policy bypass scope (#5731)

Adds an opt-in per-API-key scope (policy:bypass-provider-quota) that lets a key skip provider/account-side quota cutoffs during routing. Operator USD budgets/usage limits still enforced unconditionally (fail-closed, before the bypass). Default-off; UI toggle + badge in API Manager. Integrated into release/v3.8.43.
This commit is contained in:
WITALO ROCHA
2026-06-30 23:45:29 -03:00
committed by GitHub
parent f669335218
commit dafeb42baf
9 changed files with 150 additions and 15 deletions

View File

@@ -87,6 +87,7 @@ import { RequestTelemetry, recordTelemetry } from "../../shared/utils/requestTel
import { generateRequestId } from "../../shared/utils/requestId";
import { logAuditEvent } from "../../lib/compliance/index";
import { enforceApiKeyPolicy } from "../../shared/utils/apiKeyPolicy";
import { hasProviderQuotaBypassScope } from "../../shared/constants/apiKeyPolicyScopes";
import { cloneLogPayload } from "@/lib/logPayloads";
import { handleInternalUsageCommand } from "@/lib/usage/internalUsageCommand";
import {
@@ -330,6 +331,7 @@ export async function handleChat(
return policy.rejection;
}
const apiKeyInfo = policy.apiKeyInfo;
const bypassProviderQuotaPolicy = hasProviderQuotaBypassScope(apiKeyInfo?.scopes);
telemetry.endPhase();
// Guardrail pre-call pipeline — prompt injection, PII masking, and future custom rules.
@@ -627,6 +629,7 @@ export async function handleChat(
sessionKey: sessionAffinityKey,
...(target?.allowRateLimitedConnection ? { allowRateLimitedConnections: true } : {}),
...(target?.connectionId ? { forcedConnectionId: target.connectionId } : {}),
...(bypassProviderQuotaPolicy ? { bypassQuotaPolicy: true } : {}),
}
);
if (!creds || creds.allRateLimited) return false;
@@ -642,6 +645,18 @@ export async function handleChat(
]);
const relayConfig =
combo.strategy === "context-relay" ? resolveComboConfig(combo, settings) : null;
const relayOptions =
combo.strategy === "context-relay" || bypassProviderQuotaPolicy
? {
...(combo.strategy === "context-relay"
? {
sessionId,
config: relayConfig,
}
: {}),
...(bypassProviderQuotaPolicy ? { bypassProviderQuotaPolicy: true } : {}),
}
: undefined;
telemetry.endPhase();
// Context-relay keeps generation in combo.ts, but handoff injection lives here
@@ -706,13 +721,7 @@ export async function handleChat(
settings,
allCombos,
apiKeyAllowedConnections: apiKeyInfo?.allowedConnections ?? null,
relayOptions:
combo.strategy === "context-relay"
? {
sessionId,
config: relayConfig,
}
: undefined,
relayOptions,
signal: request?.signal ?? null,
correlationId: reqId,
});
@@ -956,6 +965,7 @@ async function handleSingleModelChat(
return runtimeOptions.providerId;
})();
const forceLiveComboTest = runtimeOptions.forceLiveComboTest === true;
const bypassProviderQuotaPolicy = hasProviderQuotaBypassScope(apiKeyInfo?.scopes);
const hasForcedConnection =
typeof runtimeOptions.forcedConnectionId === "string" &&
runtimeOptions.forcedConnectionId.trim().length > 0;
@@ -1099,6 +1109,9 @@ async function handleSingleModelChat(
bypassQuotaPolicy: true,
}
: {}),
...(!forceLiveComboTest && bypassProviderQuotaPolicy
? { bypassQuotaPolicy: true }
: {}),
...(runtimeOptions.forcedConnectionId
? { forcedConnectionId: runtimeOptions.forcedConnectionId }
: {}),