ADR-033: AI Spend Guardrails
Context
Every AI demo and the concierge call Gemini on Vertex AI, paid per token, from public endpoints. The site runs on a near-zero budget. Vertex quotas can't express a monthly dollar limit, the per-minute quota has no bucket for the eu multi-region the site uses, and a billing budget only emails after the money is spent. Spend has to be bounded by the application, in layers.
Decision
Independent layers, cheapest and earliest first:
1. Model choice: only Gemini Flash and Flash-Lite; any other model id falls back to the default (ADR-029). The cost and ADR demos are off unless switched on.
2. Per IP: 30 requests / 15 min on /api/ai/* and 10 KB request bodies (ADR-019).
3. App Check runs before the daily budget is charged, so refused requests cost nothing.
4. Site-wide: 300 model requests per UTC day across all instances, counted in Firestore (in memory if Firestore fails), answered with 429 and Retry-After to midnight UTC. Always on. Zero-LLM routes (/api/ai/copilot/resume) skip the cap, and handlers that finish without calling the model (cache hits, input-filtered challenge attempts, or 400/403/404 validation rejections) automatically refund the budget slot.
5. Per request: deadlines for the first chunk and the whole stream; the concierge is capped at four model rounds per turn with low thinking.
6. Results, translations, and embeddings are cached for 30 days; Cloud Run runs at most 3 instances in prod.
7. After the fact: a $20 (prod) / $5 (dev) billing budget with alerts at 50%, 90%, 100% and a 200% forecast.
Consequences
Benefits:
* The worst-case daily spend is known in advance.
* A distributed burst hits the site-wide cap instead of the bill; a single abuser hits the per-IP limit first.
* Real visitors keep working through bot traffic, because unattested requests don't count.
Trade-offs:
* A busy day can exhaust the cap for everyone until midnight UTC.
* The cap counts requests, not tokens.
* The billing budget only alerts; nothing stops spending automatically.
Alternatives considered
1. Rely on the billing budget alone: Rejected — it reports spend after it happens.
2. Vertex AI quota override as the main cap: Rejected — no quota bucket exists for the eu multi-region, and quotas limit rate, not daily totals.
3. Disable billing automatically at the budget: Rejected — it takes the whole project offline and reacts to delayed billing data.
4. Token-based accounting: Rejected for now — more accurate but more complex than this traffic needs.