Repository navigation
Add prompt caching, default to Sonnet 5.5, and offer Haiku 5.5 - #49
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>
Every Claude call now goes through an AI SDK middleware that sets two Anthropic cache breakpoints: the last system message (covers tool definitions and the system prompt) and the last message (rolling, so each step and each turn reads the previous prefix from cache). The production key showed zero cache reads over two months, so every step paid full input price. Wire tests capture the request body and check the breakpoints land. Sonnet 5 is the default model in the server, shared settings and the client store, per Ibraheem; Opus stays a per-chat choice. The budget note no longer calls tier 1 "the Opus budget", since a Sonnet-only student can use it up too. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AbJwLPGAUb8rEEDz2ZR2iq
DIodide
left a comment
There was a problem hiding this comment.
Reviewed b66133a against feat/weekly-usage-limit. No blocking findings in this change.
Checked shared model selection/migration, tier-two preservation of Haiku, middleware application to normal and downgraded models, tool-result/streaming cache markers, and per-request long-context pricing. The Sonnet 5.5 cache-read rate and both Haiku 5.5 pricing tiers match Anthropic's current documentation.
Validation in an isolated checkout with example development variables: npm ci, npx wrangler types, npm run typecheck, npm test (78 passed), npm run build, and git diff --check all passed. Live inference and actual cache hits were not exercised. The SDK emits its documented Haiku compatibility warning.
This approval covers #49. Its base is the still-open #34 branch, which depends on #33; landing the full feature on main still requires handling that stack.
DIodide
left a comment
There was a problem hiding this comment.
Re-reviewed after bringing current main through the parent stack. No blocking findings or implementation fixes needed. Prompt-cache markers, model migration/defaults, tier-two Haiku selection, and per-request pricing retain their intended behavior alongside current main.
Validation: Wrangler types, typecheck, all 111 tests, production build, and git diff --check passed. The built Worker ledger smoke test also passed. Live inference/cache-hit verification remains outside these mocked/local checks. Approved for landing after #33 and #34.
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.
DIodide
left a comment
There was a problem hiding this comment.
Rechecked after #34 merged and this PR retargeted to main. The squash-merge ancestry conflicts were resolved by retaining the already-reviewed #49 changes. Verified that this head has exactly the same file tree as 8f1ed52 (111 local tests, typecheck, build, Worker ledger smoke test, and GitHub CI passed). No implementation changed. Approved; waiting for the new head CI before merge.
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.
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 typesnpm run typechecknpm test, 78 passed and 0 failednpm run buildgit diff --checkTests 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.