Repository navigation
Conversation
Copies PrincetonCourses' ask-gateway tiers in dollars: $3 on any model, then Sonnet 5 only for $1 more, then PI stops answering until enough spend ages out of a rolling 7-day window. Spend is recorded per turn on the student's desk object and pruned on write, so nothing wakes the desk on a timer. An exhausted student gets the limit note from a stand-in model that never calls Anthropic. Amounts are Worker vars. Co-Authored-By: Claude Opus 5.5 (1M context) <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.
Stacked on #33, which adds the per-turn cost this budget counts. Review #33 first. Once it merges, this PR will retarget to
main.Per Ibraheem: the limit is in dollars, copies PrincetonCourses' limit, and uses a weekly window only.
The limit
PrincetonCourses' ask gateway (
tiger-junction/apps/ask-gateway/app/usage_tracker.py) gives each student:PrincetonCourses uses fixed 8-hour windows. PI uses one rolling 7-day window, so each turn's cost stops counting exactly a week after it was spent.
wrangler.jsonc(USAGE_TIER1_USD,USAGE_TIER2_USD), with no code change needed.How it works
u-<netid>-desk) gets api_usageSQLite table (at,micros,chat). After every turn, the chat records its cost from Log token usage and cost for every chat turn #33 there. That includes failed or stopped turns, since they were still billed.notice-model.ts) that streams the limit note ("You've used this week's PI allowance. It frees up Thu, Oct 15, 3:10 PM.") and never calls Anthropic. No MCP connections open, and nothing is billed.Files
src/shared/usage-limit.ts(new): budget defaults,budgetFrom(env),usageStatus(rolling window, tier, when the next tier frees up), note textsrc/server/notice-model.ts(new): the zero-cost stand-in modelsrc/server/pi.ts: budget check and step-down inbeforeTurn, recording inlogTurnUsage, desk RPCsusageRecord/usageStatussrc/client/pages/ChatPage.tsx: the notewrangler.jsonc:USAGE_TIER1_USD: "3",USAGE_TIER2_USD: "1"tests/usage-limit.test.ts: tiers, rolling window edges, reset times, env parsing, Eastern time formatting, and the stand-in model streaming the note with zero usageTesting
npm run typecheck,npm test(52 passing) andnpm run buildall pass.getModel()runs beforebeforeTurn, and that amodel,maxStepsandactiveToolsreturned frombeforeTurnare whatstreamTextgets.pi_usagerow lands.🤖 Generated with Claude Code