Repository navigation
fix(usage): price Claude long-context requests per request, and price Haiku 5.5 - #30427
Conversation
… Haiku 5.5 Haiku 5.5 bills a whole request at higher rates once its prompt (input plus cache reads and writes) exceeds 100K tokens. Claude usage was only kept as daily totals, so that could not be priced and Haiku 5.5 stayed unpriced. The existing Sonnet 4.5/4 200K tier had the opposite problem: it was applied as a marginal threshold to each day's totals, so a day of short requests could be surcharged and a single long request was under-charged (the Claude side of STA-8117, fixed for Codex in #22360). Each turn is now classified when it is aggregated, against its own model's threshold, and daily and per-location rows carry the long-context subset as an optional `longContext` field. A streamed turn that grows across the threshold moves its whole share in the resume correction. estimateCostUsd takes one token object and bills that subset at the long-context rates and the rest at base, replacing the per-bucket tiering. Saved per-file projections carry a projection version; files from an older version are reparsed on the next scan, so history picks up the split without touching the signed cache schema.
Hashing it made older inline reports fail validation and show the 'could not be validated' error; the scanner's version check alone decides reparse.
2c99fa3 to
f37e38c
Compare
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to
|
ELI5
Claude Haiku 5.5 charges about five times more for a request whose prompt is over 100K tokens. Orca only kept a running total of tokens per day, so it couldn't tell which requests were big. That's why #30421 left Haiku 5.5 unpriced. Now Orca checks each request's size as it reads the logs and remembers how many tokens came from big ones. Haiku 5.5 gets a correct price.
What Changed
Before
After
How it works
longContextTurnTokenschecks its prompt size (input plus cache reads and writes) against that model's threshold fromMODEL_PRICING. If it is over, the request's tokens are also added to alongContextfield on the daily and per-location rows. The field only appears on rows that had such a request, so rows for other models stay the same size.longContext.estimateCostUsd(model, tokens)takes one token object and prices thelongContextpart at the higher rates and the rest at normal rates. This replaces the old per-bucket tiering and its "shared allowance across cache TTLs" workaround.projectionVersion. The scanner reparses any file saved by an older version, so existing history gets the new field on the next scan.Why
Linked Issue
Follow-up to #30338 (STA-11064), stacked on #30421.
Visual Proof
N/A. No UI change; only the dollar amounts the existing cost cards show.
Testing
Automated tests added/updated
I manually tested these changes locally (not run in the app)
scanner-long-context.test.ts, using real transcript files and the real scanner:I checked the last two tests catch breakage: each fails when its mechanism is disabled.
claude-model-pricing.test.ts: Haiku 5.5 and Sonnet 4.5 long-context rates; a day's total over 200K stays at the normal rate; models without a surcharge ignore the subset; a subset larger than its totals is capped.claude-usage-report-aggregation.test.ts: summary and per-model breakdown price Haiku 5.5's long-context subset.Locally:
pnpm tc, oxlint, oxfmt andcheck:code-quality:changedpass. The claude-usage, usage, automations and stats suites pass (82 files, 691 tests).Review
Agent skill upstream boundary
Notes
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)