:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): classify the gateway's per-user limit (#111208)
Brandon38b215b64523
diff --git a/packages/agent/packages/agent-contracts/src/errors.test.ts b/packages/agent/packages/agent-contracts/src/errors.test.tsindex 25d517551170..527b204d1551 100644--- a/packages/agent/packages/agent-contracts/src/errors.test.ts+++ b/packages/agent/packages/agent-contracts/src/errors.test.ts@@ -144,6 +144,16 @@ describe("classifyGatewayLimitError", () => {],["Rate limit exceeded: User sustained rate limit exceeded", "org_limit"],["Cloud usage limit reached", "org_limit"],+ [+ // The Go gateway's per-user window, OpenAI dialect with its code.+ `API Error: 429 {"error":{"message":"user limit exceeded: hour:opus55","type":"rate_limit_error","code":"user_limit_exceeded"}}`,+ "user_limit",+ ],+ [+ // The Anthropic dialect drops the code and keeps the detail.+ `API Error: 429 {"type":"error","error":{"type":"rate_limit_error","message":"user limit exceeded: org_day"}}`,+ "user_limit",+ ],])("classifies %j as %s", (message, expected) => {expect(classifyGatewayLimitError(message)).toBe(expected);});@@ -299,6 +309,13 @@ describe("classifyPromptFailure", () => {false,],["invalid model", undefined, "unknown", false],+ // A 429 also reads as transient; the user limit must win so the turn is not retried into it.+ [+ "API Error: 429 user limit exceeded: hour:opus55",+ undefined,+ "usage_limit",+ false,+ ],] as const)("classifies %j as %s", (message, errorType, kind, retryable) => {expect(classifyPromptFailure(new Error(message), undefined, errorType),diff --git a/packages/agent/packages/agent-contracts/src/errors.ts b/packages/agent/packages/agent-contracts/src/errors.tsindex c2b2e0741f15..b652f7306de5 100644--- a/packages/agent/packages/agent-contracts/src/errors.ts+++ b/packages/agent/packages/agent-contracts/src/errors.ts@@ -77,11 +77,15 @@ const RATE_LIMIT_PATTERNS = [export type GatewayLimitCause =| "model_gate"| "model_unavailable"- | "org_limit";+ | "org_limit"+ | "user_limit";const MODEL_GATE_CODE_REGEX = /"code"\s*:\s*"model_gate"/;const AI_GATEWAY_MODEL_GATE_REGEX =/\b(?:model_not_allowed|effort_not_allowed)\b/i;+// A per-user or per-org spend window: temporary, so never a billing action.+// The Anthropic dialect drops the code and keeps only the detail text.+const AI_GATEWAY_USER_LIMIT_REGEX = /\buser[_ ]limit[_ ]exceeded\b/i;const AI_GATEWAY_ORG_LIMIT_REGEX =/\b(?:cap_exceeded|token_cap_exceeded|insufficient_credits|credit_bucket_exhausted|budget_exceeded)\b/i;// Go writes these phrases for outages and auth failures too, so they count@@ -200,6 +204,9 @@ export function classifyGatewayLimitError() {return "model_gate";}+ if (matchesRegex(AI_GATEWAY_USER_LIMIT_REGEX)) {+ return "user_limit";+ }if (matches(ORG_LIMIT_PATTERNS) ||matchesRegex(AI_GATEWAY_ORG_LIMIT_REGEX) ||diff --git a/products/desktop/packages/core/src/billing/usageLimitContent.test.ts b/products/desktop/packages/core/src/billing/usageLimitContent.test.tsindex d33333da17ed..932f076f6054 100644--- a/products/desktop/packages/core/src/billing/usageLimitContent.test.ts+++ b/products/desktop/packages/core/src/billing/usageLimitContent.test.ts@@ -95,4 +95,18 @@ describe("usageLimitContent", () => {expect(content.description).toContain("organization administrator");expect(content.actionLabel).toBeNull();});++ it.each([true, false] as const)(+ "never sends a billing prompt for a user limit (canManageBilling=%s)",+ (canManageBilling) => {+ const content = usageLimitContent({+ cause: "user_limit",+ resetLabel: null,+ subscribed: true,+ canManageBilling,+ });+ expect(content.description).toContain("Switch to a smaller model");+ expect(content.actionLabel).toBeNull();+ },+ );});diff --git a/products/desktop/packages/core/src/billing/usageLimitContent.ts b/products/desktop/packages/core/src/billing/usageLimitContent.tsindex 343ab466c20b..5b0685550c98 100644--- a/products/desktop/packages/core/src/billing/usageLimitContent.ts+++ b/products/desktop/packages/core/src/billing/usageLimitContent.ts@@ -44,6 +44,16 @@ export function usageLimitContent(args: {};}+ if (cause === "user_limit") {+ return {+ title: "Usage limit reached",+ description:+ "You've reached a usage limit for now. Hourly model limits reset within the hour and daily limits after a day. Switch to a smaller model to keep working.",+ actionLabel: null,+ dismissLabel: "Got it",+ };+ }+if (cause === "org_limit") {if (!canManageBilling) {return {diff --git a/products/desktop/packages/shared/src/analytics-events.ts b/products/desktop/packages/shared/src/analytics-events.tsindex d6c4fc450130..7dd039227e46 100644--- a/products/desktop/packages/shared/src/analytics-events.ts+++ b/products/desktop/packages/shared/src/analytics-events.ts@@ -1426,7 +1426,11 @@ export type UpgradePromptClickedSurface =| "billing_announcement"| "model_picker";-type UpgradePromptCause = "model_gate" | "model_unavailable" | "org_limit";+type UpgradePromptCause =+ | "model_gate"+ | "model_unavailable"+ | "org_limit"+ | "user_limit";export interface UpgradePromptShownProperties {surface: UpgradePromptShownSurface;