Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(ai-gateway): classify Anthropic spend-cap 400 as provider outage (#5653)

Ansh Grover4e6c53848e33
diff --git a/packages/ai-gateway/src/handlers/chat.ts b/packages/ai-gateway/src/handlers/chat.ts
index fa8dfe436..4e6c70d1d 100644
--- a/packages/ai-gateway/src/handlers/chat.ts
+++ b/packages/ai-gateway/src/handlers/chat.ts
@@ -131,6 +131,17 @@ export function isGeoBlocked(status: number, msg: string): boolean {
return status === 403 && GEO_BLOCK_PATTERN.test(msg);
}
+// Anthropic's org-level monthly spend cap ("You have reached your specified
+// API usage limits. You will regain access on ... at 00:00 UTC.", 400
+// invalid_request_error) — a provider-wide outage until the cap resets or is
+// raised, not a client bug. 4k+ identical Sentry events on 2026-07-31
+// (SCREENPIPE-AI-PROXY-30/-2P/-2W/-31) for one billing fact.
+const PROVIDER_USAGE_CAP_PATTERN = /reached your specified api usage limits/i;
+
+export function isProviderUsageCapped(status: number, msg: string): boolean {
+ return status === 400 && PROVIDER_USAGE_CAP_PATTERN.test(msg);
+}
+
export function isUserInputTooLarge(status: number, msg: string): boolean {
if (status !== 400 && status !== 413) return false;
return USER_INPUT_TOO_LARGE_PATTERNS.some((re) => re.test(msg));
@@ -261,6 +272,19 @@ async function tryModel(
throw error;
}
+ // Provider spend cap (Anthropic monthly limit) — cascade to another
+ // provider's model; if the whole chain is capped, tell the user what
+ // will work instead of leaking the raw provider JSON. One Sentry alert
+ // per request would drown the dashboard for a single billing fact, so
+ // skip it — the cost dashboards and model-health log still see it.
+ if (isProviderUsageCapped(status, msg)) {
+ error.transient = true;
+ error.userMessage = `${model} is temporarily at capacity (the provider's usage limit was reached). Pick Auto or a model from a different provider, or try again later.`;
+ console.warn(`${ctx}: ${model} provider usage cap hit (400), cascading`);
+ logModelOutcome(env, { model, outcome: 'error' }).catch(() => {});
+ throw error;
+ }
+
// Provider geo-blocks (OpenAI/Anthropic 403 by region) — expected
// per-region condition; tell the user what will work, keep Sentry quiet.
// No model name on purpose: the chain's last entry isn't what the user
diff --git a/packages/ai-gateway/src/test/chat-fallback.unit.test.ts b/packages/ai-gateway/src/test/chat-fallback.unit.test.ts
index 9e5ebcbe0..42c4b526f 100644
--- a/packages/ai-gateway/src/test/chat-fallback.unit.test.ts
+++ b/packages/ai-gateway/src/test/chat-fallback.unit.test.ts
@@ -7,6 +7,7 @@ import {
isTransient,
isUserInputTooLarge,
isGeoBlocked,
+ isProviderUsageCapped,
clientPayloadMessage,
MODEL_FALLBACKS,
TRANSIENT_STATUSES,
@@ -94,6 +95,22 @@ describe('chat handler — geo-block detection (SCREENPIPE-AI-PROXY-1C)', () =>
});
});
+describe('chat handler — provider usage-cap classification (SCREENPIPE-AI-PROXY-30/-2P)', () => {
+ it('detects the Anthropic monthly spend-cap 400', () => {
+ expect(
+ isProviderUsageCapped(
+ 400,
+ '400 {"type":"error","error":{"type":"invalid_request_error","message":"You have reached your specified API usage limits. You will regain access on 2026-08-01 at 00:00 UTC."},"request_id":"req_011CdZwqaamihBfwacKwXY4a"}',
+ ),
+ ).toBe(true);
+ });
+
+ it('leaves other 400s and non-400 statuses unclassified', () => {
+ expect(isProviderUsageCapped(400, 'invalid tool schema')).toBe(false);
+ expect(isProviderUsageCapped(429, 'You have reached your specified API usage limits.')).toBe(false);
+ });
+});
+
describe('chat handler — client payload classification (SCREENPIPE-AI-PROXY-1A / -1V)', () => {
it('maps the Gemini corrupt-image 400 to a clear user message', () => {
const msg = clientPayloadMessage(400, 'Failed to decode image data. Please make sure the image is valid.');