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

update daily chat limit (#5324)

Ezra Ellette216cda9041f0
diff --git a/apps/screenpipe-app-tauri/lib/chat/__tests__/provider-errors.test.ts b/apps/screenpipe-app-tauri/lib/chat/__tests__/provider-errors.test.ts
index 9c6edf3d2..642312e3b 100644
--- a/apps/screenpipe-app-tauri/lib/chat/__tests__/provider-errors.test.ts
+++ b/apps/screenpipe-app-tauri/lib/chat/__tests__/provider-errors.test.ts
@@ -56,13 +56,14 @@ describe("provider error copy", () => {
}
});
- it("maps the lifetime free-chat wall to upgrade-or-BYOK copy", () => {
+ it("maps the daily free-chat wall to tomorrow-or-BYOK copy", () => {
const msg = buildProviderErrorMessage(
'{"error":"free_chat_limit_exceeded","limit":2}',
{ provider: "screenpipe-cloud", model: "auto" },
);
expect(msg).toContain("2 free hosted AI messages");
- expect(msg).toContain("Upgrade");
+ expect(msg).toContain("tomorrow");
+ expect(msg).toContain("upgrade");
expect(msg).toContain("Ollama");
expect(msg).toContain("Claude");
expect(msg).toContain("Codex");
diff --git a/apps/screenpipe-app-tauri/lib/chat/__tests__/quota-errors.test.ts b/apps/screenpipe-app-tauri/lib/chat/__tests__/quota-errors.test.ts
index e9be6183e..eef80771a 100644
--- a/apps/screenpipe-app-tauri/lib/chat/__tests__/quota-errors.test.ts
+++ b/apps/screenpipe-app-tauri/lib/chat/__tests__/quota-errors.test.ts
@@ -46,9 +46,10 @@ describe("classifyQuotaError", () => {
});
describe("buildDailyLimitMessage", () => {
- it("shows the lifetime free message wall without retry copy", () => {
+ it("shows the daily free message wall without immediate retry copy", () => {
const message = buildDailyLimitMessage("free_chat_limit_exceeded");
expect(message).toContain("2 free hosted AI messages");
+ expect(message).toContain("tomorrow");
expect(message).toContain("Claude");
expect(message).toContain("Codex");
});
diff --git a/apps/screenpipe-app-tauri/lib/chat/provider-errors.ts b/apps/screenpipe-app-tauri/lib/chat/provider-errors.ts
index 6be94d780..998625a58 100644
--- a/apps/screenpipe-app-tauri/lib/chat/provider-errors.ts
+++ b/apps/screenpipe-app-tauri/lib/chat/provider-errors.ts
@@ -129,7 +129,7 @@ export function buildProviderErrorMessage(
if (isHostedScreenpipeProvider(provider)) {
if (normalized.includes("free_chat_limit_exceeded")) {
- return "You've used your 2 free hosted AI messages. Upgrade to keep chatting, or switch your AI preset to Ollama, Claude, Codex, or your own provider key.";
+ return "You've used today's 2 free hosted AI messages. Try again tomorrow, upgrade, or switch your AI preset to Ollama, Claude, Codex, or your own provider key.";
}
if (normalized.includes("free_chat_turn_request_limit_exceeded")) {
return "This free message reached its 8-step agent limit. Upgrade for longer agent runs, or switch your AI preset to your own provider.";
@@ -138,7 +138,7 @@ export function buildProviderErrorMessage(
return "Hosted AI for background pipes requires a paid plan. You can still run this pipe with Ollama or your own provider key.";
}
if (normalized.includes("free_chat_client_update_required")) {
- return "Update screenpipe to use your 2 free hosted AI messages.";
+ return "Update screenpipe to use your 2 daily free hosted AI messages.";
}
if (normalized.includes("not allowed")) {
return `Model is restricted on your current plan. Please switch to a free model or upgrade your account.`;
diff --git a/apps/screenpipe-app-tauri/lib/chat/quota-errors.ts b/apps/screenpipe-app-tauri/lib/chat/quota-errors.ts
index e69297c72..e65ddc16a 100644
--- a/apps/screenpipe-app-tauri/lib/chat/quota-errors.ts
+++ b/apps/screenpipe-app-tauri/lib/chat/quota-errors.ts
@@ -8,7 +8,7 @@
export function buildDailyLimitMessage(errorStr: string): string {
try {
if (errorStr.includes("free_chat_limit_exceeded")) {
- return "You've used your 2 free hosted AI messages. Upgrade to keep chatting, or switch your AI preset to Ollama, Claude, Codex, or your own provider key.";
+ return "You've used today's 2 free hosted AI messages. Try again tomorrow, upgrade, or switch your AI preset to Ollama, Claude, Codex, or your own provider key.";
}
if (errorStr.includes("free_chat_turn_request_limit_exceeded")) {
return "This free message reached its 8-step agent limit. Upgrade for longer agent runs, or switch your AI preset to your own provider.";
diff --git a/packages/ai-gateway/src/index.ts b/packages/ai-gateway/src/index.ts
index c348bdfb8..085713c18 100644
--- a/packages/ai-gateway/src/index.ts
+++ b/packages/ai-gateway/src/index.ts
@@ -105,7 +105,7 @@ function paidHostedAiRouteError(auth: AuthResult): Response | null {
: 'free_plan_alternate_hosted_ai_disabled',
message: auth.tier === 'anonymous'
? 'Sign in to use screenpipe hosted AI.'
- : 'The two-message free preview is available in screenpipe chat. Use your own Claude, Codex, Ollama, or provider credentials for unlimited local/BYOK use.',
+ : 'The daily two-message free allowance is available in screenpipe chat. Use your own Claude, Codex, Ollama, or provider credentials for unlimited local/BYOK use.',
});
}
@@ -324,8 +324,8 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionCo
}
// Reserve only after every other request gate has passed, but before the
- // first upstream byte can incur cost. D1 failures fail closed for free
- // hosted chat; subscribed users never enter this branch.
+ // first upstream byte can incur cost. Metering storage failures fail open;
+ // the durable-object RPM limiter and daily cost cap remain in force.
let freeChatLease: FreeChatLease | null = null;
if (freeChat.mode === 'metered') {
const reservation = await reserveFreeChatRequest(env, freeChat);
diff --git a/packages/ai-gateway/src/services/free-chat-limit.integration.test.ts b/packages/ai-gateway/src/services/free-chat-limit.integration.test.ts
index 72950c319..ffe004547 100644
--- a/packages/ai-gateway/src/services/free-chat-limit.integration.test.ts
+++ b/packages/ai-gateway/src/services/free-chat-limit.integration.test.ts
@@ -7,8 +7,8 @@ import { Miniflare } from 'miniflare';
import type { Env } from '../types';
import {
FREE_CHAT_COST_RESERVATION_MICRO_USD,
+ FREE_CHAT_DAILY_BUDGET_MICRO_USD,
FREE_CHAT_IN_FLIGHT_LEASE_SECONDS,
- FREE_CHAT_LIFETIME_BUDGET_MICRO_USD,
FREE_CHAT_MAX_PROVIDER_CALLS_PER_MESSAGE,
acquireFreeChatLease,
releaseFreeChatLease,
@@ -61,15 +61,18 @@ describe('free chat reservation against workerd D1', () => {
await miniflare.dispose();
});
- it('atomically grants only two distinct lifetime turns', async () => {
+ it('atomically grants only two distinct turns per UTC day', async () => {
+ const dayOne = new Date('2026-07-14T12:00:00.000Z');
+ const dayTwo = new Date('2026-07-15T12:00:00.000Z');
const results = await Promise.all(
Array.from({ length: 12 }, (_, index) =>
- reserveFreeChatTurn(env, metered('user-d1-turns', `turn-${index}`)),
+ reserveFreeChatTurn(env, metered('user-d1-turns', `turn-${index}`), dayOne),
),
);
expect(results.filter((result) => result.allowed)).toHaveLength(2);
expect(results.filter((result) => !result.allowed)).toHaveLength(10);
+ expect(await reserveFreeChatTurn(env, metered('user-d1-turns', 'turn-next-day'), dayTwo)).toEqual({ allowed: true });
});
it('atomically bounds parallel requests for one existing turn', async () => {
@@ -97,7 +100,7 @@ describe('free chat reservation against workerd D1', () => {
expect(results.filter((result) => result.allowed)).toHaveLength(1);
expect(results.filter((result) => !result.allowed)).toHaveLength(11);
const winner = results.find((result) => result.allowed);
- if (winner?.allowed) await releaseFreeChatLease(env, winner.lease);
+ if (winner?.allowed && winner.lease) await releaseFreeChatLease(env, winner.lease);
expect((await acquireFreeChatLease(env, turn, now)).allowed).toBe(true);
});
@@ -110,40 +113,41 @@ describe('free chat reservation against workerd D1', () => {
const db = env.DB;
const turnRow = await db.prepare(
- 'SELECT daily_count FROM usage WHERE device_id = ?',
- ).bind('turn-ordered').first<{ daily_count: number }>();
+ 'SELECT daily_count FROM usage WHERE device_id LIKE ?',
+ ).bind('turn-ordered:%').first<{ daily_count: number }>();
expect(turnRow?.daily_count).toBe(1);
const budgetRow = await db.prepare(
- "SELECT daily_count FROM usage WHERE user_id = ? AND tier = 'free_chat_budget_v1'",
+ "SELECT daily_count FROM usage WHERE user_id = ? AND tier LIKE 'free_chat_budget_v2:%'",
).bind('user-d1-ordered').first<{ daily_count: number }>();
expect(budgetRow?.daily_count).toBe(FREE_CHAT_COST_RESERVATION_MICRO_USD);
- if (first.allowed) await releaseFreeChatLease(env, first.lease);
+ if (first.allowed && first.lease) await releaseFreeChatLease(env, first.lease);
});
- it('releases the lease and skips budget when the lifetime turn claim fails', async () => {
+ it('releases the lease and skips budget when the daily turn claim fails', async () => {
const userId = 'user-d1-exhausted';
- await reserveFreeChatTurn(env, metered(userId, 'turn-used-1'));
- await reserveFreeChatTurn(env, metered(userId, 'turn-used-2'));
+ const now = new Date('2026-07-14T12:00:00.000Z');
+ await reserveFreeChatTurn(env, metered(userId, 'turn-used-1'), now);
+ await reserveFreeChatTurn(env, metered(userId, 'turn-used-2'), now);
const rejected = await reserveFreeChatRequest(
env,
metered(userId, 'turn-rejected-3'),
- new Date('2026-07-14T12:00:00.000Z'),
+ now,
);
expect(rejected.allowed).toBe(false);
if (!rejected.allowed) expect(rejected.error.code).toBe('free_chat_limit_exceeded');
const budgetRow = await env.DB.prepare(
- "SELECT daily_count FROM usage WHERE user_id = ? AND tier = 'free_chat_budget_v1'",
+ "SELECT daily_count FROM usage WHERE user_id = ? AND tier LIKE 'free_chat_budget_v2:%'",
).bind(userId).first<{ daily_count: number }>();
expect(budgetRow).toBeNull();
const nextLease = await acquireFreeChatLease(
env,
metered(userId, 'turn-rejected-3'),
- new Date('2026-07-14T12:00:00.000Z'),
+ now,
);
expect(nextLease.allowed).toBe(true);
- if (nextLease.allowed) await releaseFreeChatLease(env, nextLease.lease);
+ if (nextLease.allowed && nextLease.lease) await releaseFreeChatLease(env, nextLease.lease);
});
it('reclaims an expired lease and ignores the stale generation release', async () => {
@@ -155,19 +159,20 @@ describe('free chat reservation against workerd D1', () => {
const afterExpiry = new Date(start.getTime() + (FREE_CHAT_IN_FLIGHT_LEASE_SECONDS + 1) * 1000);
const replacement = await acquireFreeChatLease(env, turn, afterExpiry);
expect(replacement.allowed).toBe(true);
- if (first.allowed) await releaseFreeChatLease(env, first.lease);
+ if (first.allowed && first.lease) await releaseFreeChatLease(env, first.lease);
const overlapping = await acquireFreeChatLease(env, turn, afterExpiry);
expect(overlapping.allowed).toBe(false);
- if (replacement.allowed) await releaseFreeChatLease(env, replacement.lease);
+ if (replacement.allowed && replacement.lease) await releaseFreeChatLease(env, replacement.lease);
});
- it('atomically caps conservative lifetime spend reservations', async () => {
+ it('atomically caps conservative daily spend reservations', async () => {
const turn = metered('user-d1-budget', 'turn-budget');
- const reservationLimit = FREE_CHAT_LIFETIME_BUDGET_MICRO_USD
+ const now = new Date('2026-07-14T12:00:00.000Z');
+ const reservationLimit = FREE_CHAT_DAILY_BUDGET_MICRO_USD
/ FREE_CHAT_COST_RESERVATION_MICRO_USD;
const results = await Promise.all(
- Array.from({ length: reservationLimit + 8 }, () => reserveFreeChatBudget(env, turn)),
+ Array.from({ length: reservationLimit + 8 }, () => reserveFreeChatBudget(env, turn, now)),
);
expect(results.filter((result) => result.allowed)).toHaveLength(reservationLimit);
diff --git a/packages/ai-gateway/src/services/free-chat-limit.test.ts b/packages/ai-gateway/src/services/free-chat-limit.test.ts
index 67e5b5f94..68f27efc2 100644
--- a/packages/ai-gateway/src/services/free-chat-limit.test.ts
+++ b/packages/ai-gateway/src/services/free-chat-limit.test.ts
@@ -6,8 +6,8 @@ import { describe, expect, it } from 'bun:test';
import type { AuthResult, Env, RequestBody } from '../types';
import {
FREE_CHAT_COST_RESERVATION_MICRO_USD,
+ FREE_CHAT_DAILY_BUDGET_MICRO_USD,
FREE_CHAT_IN_FLIGHT_LEASE_SECONDS,
- FREE_CHAT_LIFETIME_BUDGET_MICRO_USD,
FREE_CHAT_MAX_IMAGE_BYTES,
FREE_CHAT_MAX_IMAGES,
FREE_CHAT_MAX_MESSAGES,
@@ -613,18 +613,23 @@ describe('validateFreeChatRequestLimits', () => {
});
describe('reserveFreeChatTurn', () => {
- it('allows exactly two account-wide logical turns and blocks the third', async () => {
+ it('allows exactly two account-wide logical turns per UTC day', async () => {
const db = new FakeD1();
const env = envWith(db);
+ const dayOne = new Date('2026-07-14T12:00:00.000Z');
+ const dayTwo = new Date('2026-07-15T00:00:00.000Z');
- expect(await reserveFreeChatTurn(env, metered('user-a', 'turn-a'))).toEqual({ allowed: true });
- expect(await reserveFreeChatTurn(env, metered('user-a', 'turn-b'))).toEqual({ allowed: true });
- const third = await reserveFreeChatTurn(env, metered('user-a', 'turn-c'));
+ expect(await reserveFreeChatTurn(env, metered('user-a', 'turn-a'), dayOne)).toEqual({ allowed: true });
+ expect(await reserveFreeChatTurn(env, metered('user-a', 'turn-b'), dayOne)).toEqual({ allowed: true });
+ const third = await reserveFreeChatTurn(env, metered('user-a', 'turn-c'), dayOne);
expect(third.allowed).toBe(false);
if (!third.allowed) {
expect(third.error.status).toBe(429);
expect(third.error.code).toBe('free_chat_limit_exceeded');
+ expect(third.error.message).toContain("today's 2");
}
+
+ expect(await reserveFreeChatTurn(env, metered('user-a', 'turn-c'), dayTwo)).toEqual({ allowed: true });
});
it('allows at most eight provider calls inside either logical turn', async () => {
@@ -659,7 +664,8 @@ describe('reserveFreeChatTurn', () => {
FREE_CHAT_MAX_PROVIDER_CALLS_PER_MESSAGE,
);
expect(results.filter((result) => !result.allowed)).toHaveLength(4);
- expect(db.rows.get('turn-parallel')?.dailyCount).toBe(
+ const turnRow = Array.from(db.rows.entries()).find(([key]) => key.startsWith('turn-parallel:'))?.[1];
+ expect(turnRow?.dailyCount).toBe(
FREE_CHAT_MAX_PROVIDER_CALLS_PER_MESSAGE,
);
});
@@ -678,7 +684,7 @@ describe('reserveFreeChatTurn', () => {
expect(results.filter((result) => !result.allowed)).toHaveLength(1);
});
- it('fails closed for free hosted chat when D1 is unavailable', async () => {
+ it('fails open for free hosted chat when D1 is unavailable', async () => {
const db = new FakeD1();
db.fail = true;
@@ -686,11 +692,7 @@ describe('reserveFreeChatTurn', () => {
envWith(db),
metered('user-a', 'turn-a'),
);
- expect(result.allowed).toBe(false);
- if (!result.allowed) {
- expect(result.error.status).toBe(503);
- expect(result.error.code).toBe('free_chat_limit_unavailable');
- }
+ expect(result).toEqual({ allowed: true });
});
});
@@ -705,11 +707,11 @@ describe('free chat capacity reservations', () => {
const overlapping = await reserveFreeChatRequest(env, turn, now);
expect(overlapping.allowed).toBe(false);
- const turnRow = db.rows.get('turn-ordered');
+ const turnRow = Array.from(db.rows.entries()).find(([key]) => key.startsWith('turn-ordered:'))?.[1];
expect(turnRow?.dailyCount).toBe(1);
- const budget = Array.from(db.rows.values()).find((row) => row.tier === 'free_chat_budget_v1');
+ const budget = Array.from(db.rows.values()).find((row) => row.tier.startsWith('free_chat_budget_v2:'));
expect(budget?.dailyCount).toBe(FREE_CHAT_COST_RESERVATION_MICRO_USD);
- if (first.allowed) await releaseFreeChatLease(env, first.lease);
+ if (first.allowed && first.lease) await releaseFreeChatLease(env, first.lease);
});
it('allows only one in-flight request per account and allows another after release', async () => {
@@ -724,7 +726,7 @@ describe('free chat capacity reservations', () => {
expect(overlapping.allowed).toBe(false);
if (!overlapping.allowed) expect(overlapping.error.code).toBe('free_chat_request_in_flight');
- if (first.allowed) await releaseFreeChatLease(env, first.lease);
+ if (first.allowed && first.lease) await releaseFreeChatLease(env, first.lease);
expect((await acquireFreeChatLease(env, turn, now)).allowed).toBe(true);
});
@@ -739,35 +741,41 @@ describe('free chat capacity reservations', () => {
const afterExpiry = new Date(start.getTime() + (FREE_CHAT_IN_FLIGHT_LEASE_SECONDS + 1) * 1000);
const replacement = await acquireFreeChatLease(env, turn, afterExpiry);
expect(replacement.allowed).toBe(true);
- if (first.allowed) await releaseFreeChatLease(env, first.lease);
+ if (first.allowed && first.lease) await releaseFreeChatLease(env, first.lease);
const third = await acquireFreeChatLease(env, turn, afterExpiry);
expect(third.allowed).toBe(false);
- if (replacement.allowed) await releaseFreeChatLease(env, replacement.lease);
+ if (replacement.allowed && replacement.lease) await releaseFreeChatLease(env, replacement.lease);
});
- it('atomically caps the conservative lifetime budget', async () => {
+ it('atomically caps the conservative daily budget and resets the next day', async () => {
const db = new FakeD1();
const env = envWith(db);
const turn = metered('user-budget', 'turn-budget');
- const reservations = FREE_CHAT_LIFETIME_BUDGET_MICRO_USD / FREE_CHAT_COST_RESERVATION_MICRO_USD;
+ const dayOne = new Date('2026-07-14T12:00:00.000Z');
+ const dayTwo = new Date('2026-07-15T12:00:00.000Z');
+ const reservations = FREE_CHAT_DAILY_BUDGET_MICRO_USD / FREE_CHAT_COST_RESERVATION_MICRO_USD;
const results = await Promise.all(
- Array.from({ length: reservations + 4 }, () => reserveFreeChatBudget(env, turn)),
+ Array.from({ length: reservations + 4 }, () => reserveFreeChatBudget(env, turn, dayOne)),
);
expect(results.filter((result) => result.allowed)).toHaveLength(reservations);
expect(results.filter((result) => !result.allowed)).toHaveLength(4);
- const budget = Array.from(db.rows.values()).find((row) => row.tier === 'free_chat_budget_v1');
- expect(budget?.dailyCount).toBe(FREE_CHAT_LIFETIME_BUDGET_MICRO_USD);
+ const dayOneBudget = Array.from(db.rows.values()).find((row) => row.tier === 'free_chat_budget_v2:2026-07-14');
+ expect(dayOneBudget?.dailyCount).toBe(FREE_CHAT_DAILY_BUDGET_MICRO_USD);
+ expect(await reserveFreeChatBudget(env, turn, dayTwo)).toEqual({ allowed: true });
+ const dayTwoBudget = Array.from(db.rows.values()).find((row) => row.tier === 'free_chat_budget_v2:2026-07-15');
+ expect(dayTwoBudget?.dailyCount).toBe(FREE_CHAT_COST_RESERVATION_MICRO_USD);
});
- it('fails closed when lease or budget storage is unavailable', async () => {
+ it('fails open when lease or budget storage is unavailable', async () => {
const db = new FakeD1();
db.fail = true;
const env = envWith(db);
const turn = metered('user-fail', 'turn-fail');
- expect((await acquireFreeChatLease(env, turn)).allowed).toBe(false);
- expect((await reserveFreeChatBudget(env, turn)).allowed).toBe(false);
+ expect(await acquireFreeChatLease(env, turn)).toEqual({ allowed: true, lease: null });
+ expect(await reserveFreeChatBudget(env, turn)).toEqual({ allowed: true });
+ expect(await reserveFreeChatRequest(env, turn)).toEqual({ allowed: true, lease: null });
});
});
diff --git a/packages/ai-gateway/src/services/free-chat-limit.ts b/packages/ai-gateway/src/services/free-chat-limit.ts
index 8be938bb0..8afbc6426 100644
--- a/packages/ai-gateway/src/services/free-chat-limit.ts
+++ b/packages/ai-gateway/src/services/free-chat-limit.ts
@@ -27,12 +27,11 @@ export const FREE_CHAT_IN_FLIGHT_LEASE_SECONDS = 10 * 60;
// check. Keep this in sync with FREE_PREVIEW_WATERFALL in handlers/chat.ts.
export const FREE_CHAT_COST_RESERVATION_MICRO_USD = 75_000;
// Deliberately independent from the turn/call constants: increasing those
-// later cannot silently raise the lifetime cash ceiling.
-export const FREE_CHAT_LIFETIME_BUDGET_MICRO_USD = 1_200_000;
+// later cannot silently raise the daily cash ceiling.
+export const FREE_CHAT_DAILY_BUDGET_MICRO_USD = 1_200_000;
-const FREE_CHAT_USAGE_TIER = 'free_chat_turn_v1';
-const FREE_CHAT_LAST_RESET = 'lifetime';
-const FREE_CHAT_BUDGET_TIER = 'free_chat_budget_v1';
+const FREE_CHAT_USAGE_TIER_PREFIX = 'free_chat_turn_v2';
+const FREE_CHAT_BUDGET_TIER_PREFIX = 'free_chat_budget_v2';
const FREE_CHAT_LEASE_TIER = 'free_chat_in_flight_v1';
const INTERNAL_TITLE_SESSION_PREFIX = '__title:';
@@ -58,7 +57,7 @@ export type FreeChatLease = {
};
export type FreeChatLeaseReservation =
- | { allowed: true; lease: FreeChatLease }
+ | { allowed: true; lease: FreeChatLease | null }
| { allowed: false; error: FreeChatLimitError };
export function hasPaidHostedAiPlan(auth: AuthResult): boolean {
@@ -77,6 +76,10 @@ function getSessionAffinity(request: Request): string | null {
return request.headers.get('x-session-affinity')?.trim() || null;
}
+function utcDay(now: Date): string {
+ return now.toISOString().slice(0, 10);
+}
+
function stableStringify(value: unknown): string {
if (value === null || typeof value !== 'object') {
return JSON.stringify(value);
@@ -389,7 +392,7 @@ export async function prepareFreeChatTurn(
// Free and paid Basic deliberately share the `logged_in` model/rate tier.
// The server-verified commercial plan is the only safe discriminator for the
- // lifetime preview. Missing/conflicting plan truth fails closed instead of
+ // daily allowance. Missing/conflicting plan truth fails closed instead of
// either granting free inference or accidentally charging a paid customer.
if (hasPaidHostedAiPlan(auth)) return { mode: 'bypass' };
if (auth.accountPlan !== 'free') {
@@ -507,8 +510,12 @@ function providerCallLimitError(): FreeChatReservation {
export async function reserveFreeChatTurn(
env: Env,
preflight: Extract<FreeChatPreflight, { mode: 'metered' }>,
+ now: Date = new Date(),
): Promise<FreeChatReservation> {
const { turnKey, userId } = preflight;
+ const day = utcDay(now);
+ const dailyTurnKey = `${turnKey}:${day}`;
+ const usageTier = `${FREE_CHAT_USAGE_TIER_PREFIX}:${day}`;
try {
const increment = async () => env.DB.prepare(`
@@ -516,9 +523,9 @@ export async function reserveFreeChatTurn(
SET daily_count = daily_count + 1, updated_at = CURRENT_TIMESTAMP
WHERE device_id = ? AND user_id = ? AND tier = ? AND daily_count < ?
`).bind(
- turnKey,
+ dailyTurnKey,
userId,
- FREE_CHAT_USAGE_TIER,
+ usageTier,
FREE_CHAT_MAX_PROVIDER_CALLS_PER_MESSAGE,
).run();
@@ -527,7 +534,7 @@ export async function reserveFreeChatTurn(
const existing = await env.DB.prepare(`
SELECT daily_count FROM usage
WHERE device_id = ? AND user_id = ? AND tier = ?
- `).bind(turnKey, userId, FREE_CHAT_USAGE_TIER)
+ `).bind(dailyTurnKey, userId, usageTier)
.first<{ daily_count: number }>();
if (existing) {
if (existing.daily_count >= FREE_CHAT_MAX_PROVIDER_CALLS_PER_MESSAGE) {
@@ -548,12 +555,12 @@ export async function reserveFreeChatTurn(
SELECT COUNT(*) FROM usage WHERE user_id = ? AND tier = ?
) < ?
`).bind(
- turnKey,
+ dailyTurnKey,
userId,
- FREE_CHAT_LAST_RESET,
- FREE_CHAT_USAGE_TIER,
+ day,
+ usageTier,
userId,
- FREE_CHAT_USAGE_TIER,
+ usageTier,
FREE_CHAT_MESSAGE_LIMIT,
).run();
@@ -566,7 +573,7 @@ export async function reserveFreeChatTurn(
const racedExisting = await env.DB.prepare(`
SELECT daily_count FROM usage
WHERE device_id = ? AND user_id = ? AND tier = ?
- `).bind(turnKey, userId, FREE_CHAT_USAGE_TIER)
+ `).bind(dailyTurnKey, userId, usageTier)
.first<{ daily_count: number }>();
if (racedExisting) return providerCallLimitError();
@@ -575,21 +582,14 @@ export async function reserveFreeChatTurn(
error: {
status: 429,
code: 'free_chat_limit_exceeded',
- message: `You've used your ${FREE_CHAT_MESSAGE_LIMIT} free hosted AI messages. Upgrade to keep chatting, or use your own AI provider.`,
+ message: `You've used today's ${FREE_CHAT_MESSAGE_LIMIT} free hosted AI messages. Try again tomorrow, upgrade, or use your own AI provider.`,
},
};
} catch (error) {
console.error('free chat limit unavailable', error);
- // This gate protects a lifetime cost boundary. Free hosted requests fail
- // closed when D1 is unavailable; paid users bypass this function entirely.
- return {
- allowed: false,
- error: {
- status: 503,
- code: 'free_chat_limit_unavailable',
- message: 'Free hosted chat is temporarily unavailable. Try again shortly.',
- },
- };
+ // Availability wins over metering during a D1 incident. The durable-object
+ // RPM limiter and normal daily cost cap still run before this gate.
+ return { allowed: true };
}
}
@@ -597,17 +597,6 @@ async function accountResourceKey(kind: 'budget' | 'lease', userId: string): Pro
return `free-chat:${kind}:v1:${await sha256Hex(userId)}`;
}
-function capacityUnavailable(): { allowed: false; error: FreeChatLimitError } {
- return {
- allowed: false,
- error: {
- status: 503,
- code: 'free_chat_capacity_unavailable',
- message: 'Free hosted chat is temporarily unavailable. Try again shortly.',
- },
- };
-}
-
/** Release only the exact lease generation held by this response. */
export async function releaseFreeChatLease(env: Env, lease: FreeChatLease): Promise<void> {
try {
@@ -684,19 +673,22 @@ export async function acquireFreeChatLease(
}
return { allowed: true, lease: { key: leaseKey, userId, expiresAt } };
} catch (error) {
- console.error('free chat lease unavailable', error);
- return capacityUnavailable();
+ console.error('free chat lease unavailable, failing open', error);
+ return { allowed: true, lease: null };
}
}
-/** Atomically reserve conservative lifetime spend before upstream work. */
+/** Atomically reserve conservative daily spend before upstream work. */
export async function reserveFreeChatBudget(
env: Env,
preflight: Extract<FreeChatPreflight, { mode: 'metered' }>,
+ now: Date = new Date(),
): Promise<FreeChatReservation> {
const { userId } = preflight;
+ const day = utcDay(now);
try {
- const budgetKey = await accountResourceKey('budget', userId);
+ const budgetKey = await accountResourceKey('budget', `${userId}:${day}`);
+ const budgetTier = `${FREE_CHAT_BUDGET_TIER_PREFIX}:${day}`;
const incrementBudget = async () => env.DB.prepare(`
UPDATE usage
SET daily_count = daily_count + ?, updated_at = CURRENT_TIMESTAMP
@@ -706,8 +698,8 @@ export async function reserveFreeChatBudget(
FREE_CHAT_COST_RESERVATION_MICRO_USD,
budgetKey,
userId,
- FREE_CHAT_BUDGET_TIER,
- FREE_CHAT_LIFETIME_BUDGET_MICRO_USD,
+ budgetTier,
+ FREE_CHAT_DAILY_BUDGET_MICRO_USD,
FREE_CHAT_COST_RESERVATION_MICRO_USD,
).run();
@@ -721,10 +713,10 @@ export async function reserveFreeChatBudget(
budgetKey,
userId,
FREE_CHAT_COST_RESERVATION_MICRO_USD,
- FREE_CHAT_LAST_RESET,
- FREE_CHAT_BUDGET_TIER,
+ day,
+ budgetTier,
FREE_CHAT_COST_RESERVATION_MICRO_USD,
- FREE_CHAT_LIFETIME_BUDGET_MICRO_USD,
+ FREE_CHAT_DAILY_BUDGET_MICRO_USD,
).run();
budgetClaimed = changed(insert);
}
@@ -737,16 +729,16 @@ export async function reserveFreeChatBudget(
allowed: false,
error: {
status: 429,
- code: 'free_chat_budget_exceeded',
- message: 'The free hosted AI preview budget has been used. Upgrade to keep chatting, or use your own AI provider.',
+ code: 'free_chat_daily_budget_exceeded',
+ message: "Today's free hosted AI budget has been used. Try again tomorrow, upgrade, or use your own AI provider.",
},
};
}
return { allowed: true };
} catch (error) {
- console.error('free chat budget unavailable', error);
- return capacityUnavailable();
+ console.error('free chat budget unavailable, failing open', error);
+ return { allowed: true };
}
}
@@ -762,14 +754,15 @@ export async function reserveFreeChatRequest(
): Promise<FreeChatLeaseReservation> {
const leaseReservation = await acquireFreeChatLease(env, preflight, now);
if (!leaseReservation.allowed) return leaseReservation;
+ if (!leaseReservation.lease) return leaseReservation;
- const turnReservation = await reserveFreeChatTurn(env, preflight);
+ const turnReservation = await reserveFreeChatTurn(env, preflight, now);
if (!turnReservation.allowed) {
await releaseFreeChatLease(env, leaseReservation.lease);
return turnReservation;
}
- const budgetReservation = await reserveFreeChatBudget(env, preflight);
+ const budgetReservation = await reserveFreeChatBudget(env, preflight, now);
if (!budgetReservation.allowed) {
await releaseFreeChatLease(env, leaseReservation.lease);
return budgetReservation;
diff --git a/packages/ai-gateway/src/test/anthropic-route-auth.unit.test.ts b/packages/ai-gateway/src/test/anthropic-route-auth.unit.test.ts
index ffb09f6a2..897f83692 100644
--- a/packages/ai-gateway/src/test/anthropic-route-auth.unit.test.ts
+++ b/packages/ai-gateway/src/test/anthropic-route-auth.unit.test.ts
@@ -127,7 +127,7 @@ describe('/anthropic/v1/messages authentication and model policy', () => {
const outer = await response.json() as { error: string };
const error = JSON.parse(outer.error);
expect(error.error).toBe('free_plan_alternate_hosted_ai_disabled');
- expect(error.message).toContain('two-message free preview');
+ expect(error.message).toContain('daily two-message free allowance');
expect(upstreamFetch).toHaveBeenCalledTimes(1);
});
});
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.ts
index c199ff446..f896e333e 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
@@ -228,7 +228,7 @@ describe('/v1/chat/completions free-plan route policy', () => {
const textToSpeech = await handleRequest(request(auth, '/v1/text-to-speech'), env, ctx);
// These are speech features with their own quotas/input validation. They
- // deliberately do not consume or bypass hosted-chat lifetime turns.
+ // deliberately do not consume or bypass hosted-chat daily turns.
expect(listen.status).toBe(200);
expect(realtime.status).toBe(426);
expect(transcribe.status).toBe(400);
diff --git a/packages/ai-gateway/src/types.ts b/packages/ai-gateway/src/types.ts
index 7308ee332..bf2467372 100644
--- a/packages/ai-gateway/src/types.ts
+++ b/packages/ai-gateway/src/types.ts
@@ -221,7 +221,7 @@ export type UserTier = 'anonymous' | 'logged_in' | 'subscribed';
// Server-verified commercial plan. This is intentionally separate from
// UserTier: Free and paid Basic both keep the existing `logged_in` model/rate
-// tier, but only Free receives the two-message hosted-AI preview.
+// tier, but only Free receives the daily two-message hosted-AI allowance.
export type AccountPlan =
| 'free'
| 'basic'
diff --git a/packages/ai-gateway/src/utils/auth.ts b/packages/ai-gateway/src/utils/auth.ts
index 15ef5819d..bd3290834 100644
--- a/packages/ai-gateway/src/utils/auth.ts
+++ b/packages/ai-gateway/src/utils/auth.ts
@@ -322,7 +322,7 @@ function resolveAccountPlan(user: ScreenpipeUserData): AccountPlan {
// Plan labels alone are stale advisory data in older rows. Require the fresh
// /api/user entitlement tuple to agree end-to-end so a refunded account with
- // users.plan=standard/pro cannot bypass the lifetime Free limit.
+ // users.plan=standard/pro cannot bypass the daily Free limit.
if (!accountPlan || !entitlementPlan || accountPlan !== entitlementPlan) {
return 'unknown';
}