Skip to content

Cap platform AI spend so nobody can use our credits without a limit - #360

Merged
AQIB-NAWAB merged 1 commit into
mainfrom
fix/ai-credit-abuse
Dec 3, 2026
Merged

AQIB-NAWAB merged 1 commit into
mainfrom
fix/ai-credit-abuse

Conversation

@AQIB-NAWAB

Copy link
Copy Markdown
Collaborator

Why

An audit of every feature that calls a model with the platform's own key (Anthropic, OpenRouter or Gemini) found 5 ways to spend our credits without a limit.

What changed

  • Short-answer grading on the platform key (learners with no key of their own) had no daily cap. Parallel submits all passed the attempt check, and each one was paid. Now every call is reserved in a new PlatformAiCall table before the model runs, under an advisory lock: 40 per learner per day, 6 per minute, 3,000 per day for the whole platform. Learners who use their own key are not capped.
  • Suggested help replies had no cap. Now: 60 per mentor per day and 500 for the whole platform.
  • Chapter questions and practice quizzes gave the question back on any failure, even after the model had answered and been paid. A focus written to break the JSON reply gave unlimited free quizzes. Now the question stays counted once the provider has answered. It is only given back when the call failed before any work.
  • AI review drafts: the daily cap counted only successful drafts, so paid failures and "Try again" were free. Failed tries now count too.
  • Open answers had no size limit (up to 1 MB went into review drafts). They are now limited to 20,000 characters on submit, and the review draft input is cut to the same size.

New env vars, all with defaults: SHORT_ANSWER_PLATFORM_DAILY_LIMIT, SHORT_ANSWER_PLATFORM_GLOBAL_DAILY_LIMIT, HELP_REPLY_DRAFT_DAILY_LIMIT, HELP_REPLY_DRAFT_GLOBAL_DAILY_LIMIT.

Migration: 20261203120000_platform_ai_calls adds the PlatformAiCall table.

Already safe, unchanged

The home page demo, capstone checks and chapter question limits already had caps. AI interviews and personalized prompts use the learner's own key.

Not covered

  • A mentor pressing "Try again" over and over on one failed draft still counts once.
  • Many accounts each get the per-learner cap. The platform-wide caps bound the total.

Testing

  • tsc --noEmit passes.
  • Server tests: 1,725 pass. 2 fail: the dev-login tests in tests/api/auth.test.ts, because the local scratch database had no seed accounts. They are not related to this change.
  • New tests/services/platform-ai-usage.test.ts sends 8 parallel reservations and checks that exactly the daily number gets through. It also covers the global cap, the per-minute cap and release.
  • Updated the Q&A stream, practice quiz and short-answer tests to the new refund rule.

🤖 Generated with Claude Code

- Short-answer grading on the platform key: counted before each call in a
  new PlatformAiCall table, per learner per day, per minute and platform-wide,
  under a lock so parallel requests cannot all pass.
- Suggested help replies: the same caps, per mentor and platform-wide.
- Chapter questions and practice quizzes keep the question counted once the
  model has answered, so a crafted focus that breaks the reply no longer
  gives free calls.
- AI review drafts: failed tries count toward the daily cap.
- Open answers are limited to 20,000 characters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AQIB-NAWAB
AQIB-NAWAB merged commit 91429e1 into main Dec 3, 2026
3 checks passed
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