Repository navigation
Weekly per-student model budget (PrincetonCourses tiers) - #34
Conversation
Each turn now ends with one structured pi.usage log line: model, steps, uncached input, cache reads and writes, output, reasoning, web searches, estimated USD and wall time, with the student as a keyed pseudonym instead of a NetID. This is the data for the PI cost analysis and the basis for a per-student usage limit. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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>
DIodide
left a comment
There was a problem hiding this comment.
Reviewed the weekly ledger, tier selection, exhausted-budget notice, and UI status after updating the stack with current main. No blocking findings. This remains a between-turn budget, with the documented fail-open behavior if the ledger cannot be read.
Validation: Wrangler types, typecheck, all 85 tests, production build, and git diff --check passed. I also exercised the actual built Worker/SQLite ledger locally on the combined stack: spend persisted across calls, two chats shared one student ledger, a second student remained isolated, and the $3/$4 tier transitions worked. No production data or live model calls were used.
DIodide
left a comment
There was a problem hiding this comment.
Rechecked after #33 was squash-merged into main. The base-history update has an identical file tree to the reviewed and locally tested head; no implementation changed. Approval stands. Waiting for the new head CI before merging in stack order.
PI currently defaults to Opus and sends reusable prompt prefixes without cache markers. This makes Sonnet 5.5 the default and adds Haiku 5.5, including the request in [TIG-311](https://linear.app/tigerapps/issue/TIG-311/add-prompt-caching-and-switch-pi-to-sonnet-55#comment-f3fd05b3). - Apply five-minute cache breakpoints to the system prefix and final conversation block for every Claude call, including tool-result follow-ups and budget downgrades. - Share model selection between browser preferences and the server. Preserve existing Opus choices and migrate old Sonnet 5 or retired choices to Sonnet 5.5. - Keep Haiku selected when the weekly budget restricts Opus to Sonnet. Leave the existing dollar allowances unchanged. - Add Haiku pricing, applying its higher rates only to individual requests above 100,000 prompt tokens. Correct Sonnet 5.5 cache-read pricing using [Anthropic's price list](https://platform.claude.com/docs/en/about-claude/pricing). Stacked on #34, which depends on #33. Merge order is #33, #34, then this PR. Validation passed with the repository's example development variables. - `npx wrangler types` - `npm run typecheck` - `npm test`, 78 passed and 0 failed - `npm run build` - `git diff --check` Tests capture real SDK request bodies for all three models, including streaming and tool results, and cover budget selection and Haiku pricing thresholds. Provider responses are mocked. Live inference, signed-in UI behavior, and actual cache hits have not been verified. The installed Anthropic SDK accepts Haiku 5.5 through its compatibility path and emits an informational 128,000-token output-limit warning. Implemented with Codex assistance. --------- Co-authored-by: Aikhan Jumashukurov <117706338+aikhanj@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: DIodide <ibraheem.amin3@gmail.com>
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