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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(ai-gateway): resolve anonymous usage as free (#5701)
Louis Beaumont2c294792bea2
diff --git a/packages/ai-gateway/src/index.ts b/packages/ai-gateway/src/index.tsindex 84c037be8..4e9a80b72 100644--- a/packages/ai-gateway/src/index.ts+++ b/packages/ai-gateway/src/index.ts@@ -294,12 +294,20 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionCo// Usage status endpoint - returns current usage without incrementingif (path === '/v1/usage' && request.method === 'GET') {+ // Anonymous auth results deliberately carry an `unknown` account plan:+ // there is no server-verified customer record. The usage endpoint still+ // represents anonymous traffic as the Free product, so resolve that one+ // safe fallback explicitly. Keep every authenticated unknown plan+ // fail-closed instead of accidentally granting paid capacity.+ const usageAccountPlan = authResult.tier === 'anonymous' && authResult.accountPlan === 'unknown'+ ? 'free'+ : authResult.accountPlan;const status = await getUsageStatus(env,authResult.deviceId,usageTier,authResult.userId,- authResult.accountPlan,+ usageAccountPlan,);// Enrich with cost-based limit flag (NOT the raw $ numbers — those// are our internal margin and shouldn't leak to any client/user).@@ -310,12 +318,12 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionColet monthlyCap: number;try {maxCost = getPlanDailyCostCap(- authResult.accountPlan,+ usageAccountPlan,env,authResult.hostedAiTrial === true,);monthlyCap = getPlanMonthlyCostCap(- authResult.accountPlan,+ usageAccountPlan,env,authResult.hostedAiTrial === true,);@@ -339,7 +347,7 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionCo// Admission still fails closed. The status route stays available but// marks usage unknown instead of pretending the customer spent zero.}- const includedCredits = getHostedAiIncludedCredits(authResult.accountPlan);+ const includedCredits = getHostedAiIncludedCredits(usageAccountPlan);const usedCredits = monthlyCost === null ? null : Math.ceil(monthlyCost * 100);const enriched = {...status,@@ -347,14 +355,14 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionCoupgrade_eligible: isHostedAiUpgradeEligible(authResult),upsell_banner: status.upsell_banner === true && isHostedAiUpgradeEligible(authResult),hosted_ai: {- plan: getHostedAiPlan(authResult.accountPlan) ?? 'unknown',+ plan: getHostedAiPlan(usageAccountPlan) ?? 'unknown',trial: authResult.hostedAiTrial === true,included_credits: includedCredits,used_credits: usedCredits,remaining_credits: usedCredits === null? null: Math.max(0, includedCredits - usedCredits),- model_access: [...getHostedAiAllowedModels(authResult.accountPlan)],+ model_access: [...getHostedAiAllowedModels(usageAccountPlan)],upgrade_url: isHostedAiUpgradeEligible(authResult)? 'https://screenpi.pe/account/billing': null,diff --git a/packages/ai-gateway/src/test/free-chat-route.unit.test.ts b/packages/ai-gateway/src/test/free-chat-route.unit.test.tsindex 0992e16b9..a7bafd827 100644--- a/packages/ai-gateway/src/test/free-chat-route.unit.test.ts+++ b/packages/ai-gateway/src/test/free-chat-route.unit.test.ts@@ -97,6 +97,38 @@ describe('/v1/chat/completions free-plan route policy', () => {expect(globalThis.fetch).not.toHaveBeenCalled();});+ it.each([+ ['without credentials', {}],+ ['after an invalid bearer falls back to anonymous', { Authorization: 'Bearer invalid-canary' }],+ ])('returns the Free usage policy %s', async (+ _label: string,+ headers: Record<string, string>,+ ) => {+ const response = await handleRequest(new Request('https://gateway.test/v1/usage', {+ headers,+ }), env, ctx);+ const body = await response.json() as {+ tier: string;+ cost_limit_reached: boolean;+ hosted_ai: {+ plan: string;+ included_credits: number;+ model_access: string[];+ };+ };++ expect(response.status).toBe(200);+ expect(body).toMatchObject({+ tier: 'anonymous',+ cost_limit_reached: false,+ hosted_ai: {+ plan: 'free',+ included_credits: 10,+ },+ });+ expect(body.hosted_ai.model_access).toContain('auto');+ });+it('lets the trusted runner bearer reach hosted background inference without a human user id', async () => {const response = await handleRequest(request({