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

reset text cash budget without deleting incident spend

Louis Beaumont806fc349c7c5
diff --git a/packages/ai-gateway/src/index.ts b/packages/ai-gateway/src/index.ts
index 5b6f86442..a31baea87 100644
--- a/packages/ai-gateway/src/index.ts
+++ b/packages/ai-gateway/src/index.ts
@@ -24,6 +24,7 @@ import {
releaseDailyCostLease,
reserveDailyCostCap,
withDailyCostSettlement,
+ getDailyUserCostForCap,
} from './services/cost-cap';
import {
FREE_CHAT_MAX_PROVIDER_CALLS_PER_MESSAGE,
@@ -223,7 +224,7 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionCo
// are our internal margin and shouldn't leak to any client/user).
// Stored query credits do not raise the cash ceiling. Credit-funded
// provider spend needs consumptive accounting before it can safely do so.
- const dailyCost = await getDailyUserCost(env, authResult.deviceId);
+ const dailyCost = await getDailyUserCostForCap(env, authResult.deviceId);
const maxCost = getTierDailyCostCap(authResult.tier, env);
const enriched = {
...status,
diff --git a/packages/ai-gateway/src/services/cost-cap.ts b/packages/ai-gateway/src/services/cost-cap.ts
index ce007279c..44fe50348 100644
--- a/packages/ai-gateway/src/services/cost-cap.ts
+++ b/packages/ai-gateway/src/services/cost-cap.ts
@@ -6,12 +6,14 @@ import { Env } from '../types';
import { addCorsHeaders, createErrorResponse } from '../utils/cors';
import { withResponseFinalizer } from '../utils/response-finalizer';
import {
+ getDailyUserCost,
getDailyUserCostOrThrow,
getTierDailyCostCap,
isZeroCostModel,
} from './cost-tracker';
const COST_LEASE_TIER = 'daily_cost_in_flight_v1';
+const COST_BASELINE_TIER = 'daily_cost_baseline_v1';
export const DAILY_COST_LEASE_SECONDS = 10 * 60;
export type DailyCostLease = {
@@ -34,6 +36,81 @@ async function sha256Hex(value: string): Promise<string> {
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('');
}
+function utcDay(now: Date = new Date()): string {
+ return now.toISOString().slice(0, 10);
+}
+
+function configuredCostCapEpoch(env: Env): string | null {
+ const epoch = env.COST_CAP_EPOCH?.trim();
+ return epoch && epoch.length <= 128 ? epoch : null;
+}
+
+/**
+ * Return spend incurred after the configured cash-cap epoch.
+ *
+ * Emergency model/cap changes can otherwise strand every account that already
+ * spent above the new ceiling earlier in the same UTC day. We preserve the
+ * original per-account accumulator for audit and snapshot it into a separate
+ * namespaced usage row. Future decisions subtract that immutable daily
+ * baseline. Changing COST_CAP_EPOCH starts another reversible budget epoch;
+ * removing it restores the full UTC-day accounting view.
+ */
+export async function getDailyUserCostForCapOrThrow(
+ env: Env,
+ deviceId: string,
+ now: Date = new Date(),
+): Promise<number> {
+ const epoch = configuredCostCapEpoch(env);
+ if (!epoch) return getDailyUserCostOrThrow(env, deviceId);
+
+ const day = utcDay(now);
+ const baselineKey = `daily-cost:baseline:v1:${await sha256Hex(`${epoch}:${deviceId}`)}`;
+ const readBaseline = () => env.DB.prepare(`
+ SELECT daily_cost_usd AS baseline
+ FROM usage
+ WHERE device_id = ? AND tier = ? AND cost_day = ?
+ `).bind(baselineKey, COST_BASELINE_TIER, day).first<{ baseline: number }>();
+
+ let baselineRow = await readBaseline();
+ if (!baselineRow) {
+ const currentCost = await getDailyUserCostOrThrow(env, deviceId);
+ // Reuse the epoch row on later UTC days, but never overwrite a baseline
+ // that another request already established for this day.
+ await env.DB.prepare(`
+ UPDATE usage
+ SET user_id = ?, daily_count = 0, last_reset = ?, cost_day = ?,
+ daily_cost_usd = ?, updated_at = CURRENT_TIMESTAMP
+ WHERE device_id = ? AND tier = ?
+ AND (cost_day IS NULL OR cost_day <> ?)
+ `).bind(
+ deviceId, day, day, currentCost, baselineKey, COST_BASELINE_TIER, day,
+ ).run();
+ await env.DB.prepare(`
+ INSERT OR IGNORE INTO usage
+ (device_id, user_id, daily_count, last_reset, tier, cost_day, daily_cost_usd)
+ VALUES (?, ?, 0, ?, ?, ?, ?)
+ `).bind(
+ baselineKey, deviceId, day, COST_BASELINE_TIER, day, currentCost,
+ ).run();
+ baselineRow = await readBaseline();
+ if (!baselineRow) throw new Error('daily cost baseline unavailable');
+ }
+
+ const currentCost = await getDailyUserCostOrThrow(env, deviceId);
+ return Math.max(0, currentCost - Number(baselineRow.baseline || 0));
+}
+
+export async function getDailyUserCostForCap(env: Env, deviceId: string): Promise<number> {
+ try {
+ return await getDailyUserCostForCapOrThrow(env, deviceId);
+ } catch (error) {
+ console.error('effective daily cost read failed', error);
+ // Keep the status endpoint available with the legacy full-day view. Actual
+ // request admission still uses the throwing helper above and fails closed.
+ return getDailyUserCost(env, deviceId);
+ }
+}
+
function capResponse(tier: string): Response {
const resetsAt = new Date();
resetsAt.setUTCHours(24, 0, 0, 0);
@@ -87,7 +164,8 @@ export async function reserveDailyCostCap(
let lease: DailyCostLease | null = null;
try {
- const key = `daily-cost:lease:v1:${await sha256Hex(deviceId)}`;
+ const leaseEpoch = configuredCostCapEpoch(env) ?? 'legacy';
+ const key = `daily-cost:lease:v1:${await sha256Hex(`${leaseEpoch}:${deviceId}`)}`;
const nowIso = now.toISOString();
const expiresAt = new Date(now.getTime() + DAILY_COST_LEASE_SECONDS * 1000).toISOString();
const claim = async () => env.DB.prepare(`
@@ -116,7 +194,7 @@ export async function reserveDailyCostCap(
}
lease = { key, deviceId, expiresAt };
- const dailyCost = await getDailyUserCostOrThrow(env, deviceId);
+ const dailyCost = await getDailyUserCostForCapOrThrow(env, deviceId, now);
if (dailyCost >= getTierDailyCostCap(tier, env)) {
await releaseDailyCostLease(env, lease);
return { allowed: false, response: capResponse(tier) };
@@ -169,7 +247,7 @@ export async function enforceDailyCostCap(
): Promise<Response | null> {
if (isZeroCostModel(model)) return null;
try {
- const dailyCost = await getDailyUserCostOrThrow(env, deviceId);
+ const dailyCost = await getDailyUserCostForCapOrThrow(env, deviceId);
return dailyCost >= getTierDailyCostCap(tier, env) ? capResponse(tier) : null;
} catch {
return unavailableResponse();
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 44fdf7c93..b0644ab41 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
@@ -233,4 +233,53 @@ describe('usage reservations against workerd D1', () => {
await releaseDailyCostLease(env, replacement.lease);
}
});
+
+ it('preserves incident spend while enforcing a fresh post-epoch cash budget', async () => {
+ const deviceId = 'user-d1-cost-epoch';
+ const day = new Date().toISOString().slice(0, 10);
+ const now = new Date();
+ env.COST_CAP_EPOCH = 'incident-v2';
+ env.MAX_DAILY_COST_PER_USER = '0.5'; // subscribed cap = $3.50
+ await env.DB.prepare(`
+ INSERT INTO usage (device_id, last_reset, tier, cost_day, daily_cost_usd)
+ VALUES (?, ?, 'subscribed', ?, 40)
+ `).bind(deviceId, day, day).run();
+
+ const first = await reserveDailyCostCap(env, deviceId, 'subscribed', 'claude-sonnet-5', now);
+ expect(first.allowed).toBe(true);
+ if (!first.allowed || !first.lease) throw new Error('expected fresh epoch lease');
+ await releaseDailyCostLease(env, first.lease);
+
+ const baseline = await env.DB.prepare(`
+ SELECT daily_cost_usd, cost_day FROM usage
+ WHERE tier = 'daily_cost_baseline_v1' AND user_id = ?
+ `).bind(deviceId).first<{ daily_cost_usd: number; cost_day: string }>();
+ expect(baseline).toEqual({ daily_cost_usd: 40, cost_day: day });
+
+ await env.DB.prepare(`UPDATE usage SET daily_cost_usd = 43.49 WHERE device_id = ?`)
+ .bind(deviceId).run();
+ const underCap = await reserveDailyCostCap(env, deviceId, 'subscribed', 'claude-sonnet-5', now);
+ expect(underCap.allowed).toBe(true);
+ if (underCap.allowed && underCap.lease) await releaseDailyCostLease(env, underCap.lease);
+
+ await env.DB.prepare(`UPDATE usage SET daily_cost_usd = 43.5 WHERE device_id = ?`)
+ .bind(deviceId).run();
+ const atCap = await reserveDailyCostCap(env, deviceId, 'subscribed', 'claude-sonnet-5', now);
+ expect(atCap.allowed).toBe(false);
+ if (!atCap.allowed) expect(await atCap.response.text()).toContain('daily_cost_limit_exceeded');
+
+ // The forensic accumulator remains intact; only the admission view subtracts
+ // the immutable epoch baseline.
+ const preserved = await env.DB.prepare(`
+ SELECT daily_cost_usd FROM usage WHERE device_id = ?
+ `).bind(deviceId).first<{ daily_cost_usd: number }>();
+ expect(preserved?.daily_cost_usd).toBe(43.5);
+
+ // A deliberately changed epoch starts a new reversible budget without
+ // deleting either the incident ledger or the previous baseline.
+ env.COST_CAP_EPOCH = 'incident-v3';
+ const nextEpoch = await reserveDailyCostCap(env, deviceId, 'subscribed', 'claude-sonnet-5', now);
+ expect(nextEpoch.allowed).toBe(true);
+ if (nextEpoch.allowed && nextEpoch.lease) await releaseDailyCostLease(env, nextEpoch.lease);
+ });
});
diff --git a/packages/ai-gateway/src/types.ts b/packages/ai-gateway/src/types.ts
index 3f07a196c..ecee252cc 100644
--- a/packages/ai-gateway/src/types.ts
+++ b/packages/ai-gateway/src/types.ts
@@ -209,6 +209,13 @@ export interface Env {
LIMIT_SUBSCRIBED_DAILY?: string;
LIMIT_SUBSCRIBED_RPM?: string;
LIMIT_IP_DAILY?: string;
+ /**
+ * Stable incident/repricing identifier for text-AI cash caps. When changed,
+ * the gateway snapshots each account's existing UTC-day spend and enforces
+ * the new cap only on spend incurred after that snapshot.
+ */
+ COST_CAP_EPOCH?: string;
+ MAX_DAILY_COST_PER_USER?: string;
// Per-minute RPM for free (weight-0) models — a separate, much higher bucket
// so heavy free usage never trips the low paid-model limit. Tunable per tier.
LIMIT_ANONYMOUS_FREE_RPM?: string;
diff --git a/packages/ai-gateway/wrangler.toml b/packages/ai-gateway/wrangler.toml
index c127ba39c..9d212e7c7 100644
--- a/packages/ai-gateway/wrangler.toml
+++ b/packages/ai-gateway/wrangler.toml
@@ -58,10 +58,12 @@ SELF_HOSTED_TRANSCRIPTION_URL = "https://audio.screenpi.pe"
# deliberate default across redeploys; FLEX_TIER_ENABLED remains the master switch.
GEMINI_FLEX_INTERACTIVE = "false"
-# 2026-07-31 emergency spend containment. Fable is rerouted to Sonnet in code;
-# reject other frontier-priced background work and bound the tier multipliers
-# to $0.032 anonymous / $0.064 Free+Basic / $0.70 Business per UTC day.
-MAX_DAILY_COST_PER_USER = "0.1"
+# 2026-07-31 emergency spend containment. Fable and Opus are rerouted to Sonnet
+# in code; reject frontier-priced background work. The epoch preserves today's
+# incident ledger while giving legitimate chats a fresh post-containment budget.
+# Effective text-AI caps: $0.16 anonymous / $0.32 Free+Basic / $3.50 Business.
+COST_CAP_EPOCH = "anthropic-incident-2026-07-31-v2"
+MAX_DAILY_COST_PER_USER = "0.5"
MODEL_GATING_ENABLED = "true"
PIPE_FRONTIER_POLICY = "reject"