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): grant enterprise top gateway allowance (#6837)

Ezra Ellette8663ecbef34d
diff --git a/packages/ai-gateway/src/services/cloudflare-ai-gateway.ts b/packages/ai-gateway/src/services/cloudflare-ai-gateway.ts
index 7005e9d8d..cfcf1ef76 100644
--- a/packages/ai-gateway/src/services/cloudflare-ai-gateway.ts
+++ b/packages/ai-gateway/src/services/cloudflare-ai-gateway.ts
@@ -81,6 +81,7 @@ export function isHostedChatGatewayEnabled(env: Pick<Env, 'HOSTED_CHAT_GATEWAY_M
function collapsePlan(auth: AuthResult): HostedChatPlan {
if (auth.service === true) return 'internal';
+ if (auth.accountPlan === 'enterprise') return 'business_ultra';
if (auth.accountPlan === 'business_max' || auth.accountPlan === 'business_ultra') {
return auth.accountPlan;
}
diff --git a/packages/ai-gateway/src/test/cloudflare-ai-gateway.unit.test.ts b/packages/ai-gateway/src/test/cloudflare-ai-gateway.unit.test.ts
index 7b4929413..cd639840d 100644
--- a/packages/ai-gateway/src/test/cloudflare-ai-gateway.unit.test.ts
+++ b/packages/ai-gateway/src/test/cloudflare-ai-gateway.unit.test.ts
@@ -50,7 +50,7 @@ describe('Cloudflare hosted-chat metadata', () => {
});
it('preserves Max and Ultra allowance tiers while collapsing catalog-equivalent plans', async () => {
- for (const accountPlan of ['business', 'team', 'enterprise'] as const) {
+ for (const accountPlan of ['business', 'team'] as const) {
const context = await buildHostedChatGatewayContext(auth({ accountPlan }), 'gpt-5.6-sol', 'background');
expect(context).toMatchObject({ plan: 'business', lane: 'frontier', workload: 'background' });
}
@@ -58,6 +58,8 @@ describe('Cloudflare hosted-chat metadata', () => {
const context = await buildHostedChatGatewayContext(auth({ accountPlan }), 'auto', 'background');
expect(context).toMatchObject({ plan: accountPlan, lane: 'auto', workload: 'background' });
}
+ const enterprise = await buildHostedChatGatewayContext(auth({ accountPlan: 'enterprise' }), 'auto', 'background');
+ expect(enterprise).toMatchObject({ plan: 'business_ultra', lane: 'auto', workload: 'background' });
const trial = await buildHostedChatGatewayContext(auth({ accountPlan: 'basic', hostedAiTrial: true }), 'auto', 'interactive');
expect(trial).toMatchObject({ plan: 'basic', trial: true });
const internal = await buildHostedChatGatewayContext(auth({ service: true, userId: undefined }), 'auto', 'background');
diff --git a/packages/ai-gateway/src/utils/auth.test.ts b/packages/ai-gateway/src/utils/auth.test.ts
index 68553eeda..3fee6e957 100644
--- a/packages/ai-gateway/src/utils/auth.test.ts
+++ b/packages/ai-gateway/src/utils/auth.test.ts
@@ -495,6 +495,32 @@ describe('validateAuth — verified identities only', () => {
});
});
+ it('uses the server-verified enterprise identity instead of the consumer billing tier', async () => {
+ verifyTokenMock.mockImplementation(async () => ({ sub: 'user_enterprise' }) as any);
+ globalThis.fetch = mock(async () => new Response(JSON.stringify({
+ success: true,
+ user: {
+ clerk_id: 'user_enterprise',
+ cloud_subscribed: true,
+ app_entitled: true,
+ subscription_plan: 'pro',
+ billing_plan: 'pro',
+ is_enterprise_user: true,
+ entitlement: { active: true, plan: 'pro', features: { app: true } },
+ },
+ }), { status: 200 })) as typeof fetch;
+
+ expect(await validateAuth(requestFor('eyJ.enterprise.clerk'), env)).toEqual({
+ isValid: true,
+ tier: 'subscribed',
+ accountPlan: 'enterprise',
+ deviceId: 'user_enterprise',
+ userId: 'user_enterprise',
+ clerkUserId: 'user_enterprise',
+ clerkUserIdVerified: true,
+ });
+ });
+
it('uses canonical Max and Ultra billing plans with desktop-compatible access labels', async () => {
for (const [plan, accountPlan, usageTier] of [
['pro_max', 'business_max', 'business_max'],
diff --git a/packages/ai-gateway/src/utils/auth.ts b/packages/ai-gateway/src/utils/auth.ts
index d8fa979da..c7ab1f540 100644
--- a/packages/ai-gateway/src/utils/auth.ts
+++ b/packages/ai-gateway/src/utils/auth.ts
@@ -181,6 +181,7 @@ interface ScreenpipeUserData {
app_entitled?: boolean;
subscription_plan?: string | null;
billing_plan?: string | null;
+ is_enterprise_user?: boolean;
hosted_ai_trial?: boolean;
entitlement?: {
active?: boolean;
@@ -324,11 +325,15 @@ function resolveAccountPlan(user: ScreenpipeUserData): AccountPlan {
accountPlan = billingPlan;
}
- return user.app_entitled === true &&
- user.entitlement?.active === true &&
- user.entitlement?.features?.app === true
- ? accountPlan
- : 'unknown';
+ if (
+ user.app_entitled !== true ||
+ user.entitlement?.active !== true ||
+ user.entitlement?.features?.app !== true
+ ) {
+ return 'unknown';
+ }
+
+ return user.is_enterprise_user === true ? 'enterprise' : accountPlan;
}
async function validateScreenpipeToken(token: string): Promise<ScreenpipeTokenResult> {