Repository navigation
Cap platform AI spend so nobody can use our credits without a limit - #360
Merged
Merged
Conversation
- 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
PlatformAiCalltable 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.focuswritten 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.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_callsadds thePlatformAiCalltable.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
Testing
tsc --noEmitpasses.tests/api/auth.test.ts, because the local scratch database had no seed accounts. They are not related to this change.tests/services/platform-ai-usage.test.tssends 8 parallel reservations and checks that exactly the daily number gets through. It also covers the global cap, the per-minute cap and release.🤖 Generated with Claude Code