Skip to content

feat(cost): daily ceiling on paid AI-engine calls - #165

Merged
razel-hash merged 3 commits into
mainfrom
lane/aeo-ai-daily-cap
Nov 16, 2026
Merged

razel-hash merged 3 commits into
mainfrom
lane/aeo-ai-daily-cap

Conversation

@razel-hash

Copy link
Copy Markdown
Collaborator

What

A daily ceiling on paid AI-engine calls from all visitors together, on top of the per-visitor and per-audit limits.

  • src/lib/aiBudget.ts: AEO_DAILY_AI_CALL_CAP calls per UTC day, default 2000 (about 48 full audits); an unusable value means the default.
  • /api/audit/start reserves an audit's worst case, 41 calls (one to write the questions, then 4 engines × 10), after validation and before the audit row exists. Custom questions reserve 4 per question after their own limits and after the stored-results shortcut.
  • One row per reservation in the existing limiter table (ai-calls:<n>), so it grows by reservations, not calls. Each row counts for at most 41.
  • Fails closed: if the reservation can't be written or counted, or a day has more reservations than one read returns, the visitor gets "Free audits have reached their limit for today. Please try again later." (AI_DAILY_LIMIT in the logs).
  • Staff are counted but never refused.

Pipeline: /tdd (aiBudget.test new module red first; start and custom-prompts reservation and refusal cases 4 red; one-row, staff, row-size-cap cases red before their fixes) → code-reviewer subagent → /security-review

Risk tier: high (cost control on public routes)

Risk

  • On by default. If real traffic is near 48 audits a day, visitors start seeing the limit message. Raise it with AEO_DAILY_AI_CALL_CAP, or revert.
  • The counts read the limiter table by key and date; the (ip, created_at) index is in draft 007, not confirmed live. Without it the count is a table scan; a failed count refuses (fails closed).
  • An Inngest retry of the probe step can spend a second time without a new reservation (existing pipeline behaviour), so actual spend can exceed the ceiling on retried audits.

Code review: the code-reviewer subagent confirmed the 41 figure and the check order (invalid, paused, gated and rate-limited requests reserve nothing). It raised row growth (fixed: one row per reservation), staff being refused and a wrong "tomorrow" (fixed: staff counted not refused; message says "later"). Index presence and the retry double-spend are listed under Risk.

Security review: no findings at confidence ≥ 8. A forged limiter key could only add one row of at most 41 calls.

Checks: npx vitest run 265/265 in 25 files; npx tsc --noEmit clean; eslint on changed files 0 errors.

🤖 Generated with Claude Code

razel-hash and others added 2 commits November 17, 2026 07:20
AEO_DAILY_AI_CALL_CAP (default 2000 per UTC day). /start reserves an audit's worst
case (41) before the audit row exists; custom prompts reserve 4 per question after
their own limits. Reserve-then-count in the shared limiter table; fails closed with
a plain message (AI_DAILY_LIMIT).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…utral retry message

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
campaign-creators-aeo-auditor Ready Ready Preview Nov 16, 2026 11:50pm UTC

Request Review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@razel-hash
razel-hash merged commit 8a0b97a into main Nov 16, 2026
3 checks passed

This branch was successfully deployed

1 active deployment
Preview — 73115136 Deployed Nov 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant