Skip to content

fix(usage): price Claude long-context requests per request, and price Haiku 5.5 - #30427

Merged
Jinwoo-H merged 2 commits into
mainfrom
sta-11064-haiku-5-5-long-context
Oct 18, 2026
Merged

Jinwoo-H merged 2 commits into
mainfrom
sta-11064-haiku-5-5-long-context

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​227 $\color{#cf222e}{\Huge{\mathbf{−}}}$​50 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​177
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​209 $\color{#cf222e}{\Huge{\mathbf{−}}}$​134 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​75

Stacked on #30421. Review and merge that first; this PR's diff is only the second commit.

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

  • Haiku 5.5 is priced: $0.10 / $0.50, and $0.50 / $2.50 for requests over 100K. Cache rates are on the same scale.
  • Sonnet 4.5 / 4 apply their surcharge only to requests that were actually over 200K, and to the whole request.
  • Day-by-day totals and the Usage screen layout don't change; only the cost estimates do.

How it works

  • Per-request check. When a request is read from the transcript, longContextTurnTokens checks its prompt size (input plus cache reads and writes) against that model's threshold from MODEL_PRICING. If it is over, the request's tokens are also added to a longContext field 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.
  • Growing requests. Claude Code logs the same request several times as it streams. If a later copy pushes a request over the threshold, the resume correction moves the whole request into longContext.
  • Pricing. estimateCostUsd(model, tokens) takes one token object and prices the longContext part 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.
  • Rebuilding old data. Each saved per-file result now records projectionVersion. The scanner reparses any file saved by an older version, so existing history gets the new field on the next scan.

Why

  • Why per request: it's how Anthropic bills, and the same approach Codex already uses (fix(usage): price Codex long context per request, not per aggregate #22360). The alternative was pricing Haiku 5.5 at its normal rate and undercounting big requests silently, which is the kind of quiet error fix(usage): price Claude models by exact version, add Sonnet 5.5, flag unpriced models #30421 removes.
  • Why a per-file version instead of bumping the cache schema: the Claude cache schema version (6 and 7) also controls cache signing and migration, with tests for forged and legacy files. Bumping it for a pricing change would have meant rewriting that security logic. The per-file version only decides whether a file is reparsed, and the next change of this kind just bumps the number. It is deliberately kept out of the file's integrity hash, because hashing it would make caches saved by older builds fail validation and show users an error.

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:

    • only requests over their own model's threshold are recorded: Haiku 5.5 at 101K yes, 99K no; Sonnet 4.5 at 151K no; Sonnet 4.6 at 501K no, since it has no surcharge;
    • a resumed request that grows from 91K to 121K moves into the long-context subset, and the result matches a fresh full scan;
    • a file saved by an older version is reparsed.
  • 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 and check:code-quality:changed pass. The claude-usage, usage, automations and stats suites pass (82 files, 691 tests).

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • First launch after update: every Claude transcript is reparsed once, as happened when 1-hour cache writes were added (fix(claude-usage): bill 1-hour cache writes at 2x base input #16878). Until that scan finishes, Haiku 5.5 and Sonnet 4.5 history shows normal-rate costs.
  • Sonnet 4.5's 200K threshold: Anthropic's current pricing page no longer lists the Sonnet 4.5 / 4 long-context tier. The threshold and rates are carried over unchanged, and the whole-request reading follows how Anthropic described that tier when it was listed.
  • Automation run costs: priced from session location totals with the session's model, as before. The long-context share follows each request's own model.
  • Platforms and remote: the logic is the same on every platform. It reads local Claude transcripts only, so remote/SSH sessions are unaffected. The saved-row change is local cache only, and an absent field reads as "no long-context requests".

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

… 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.
@Jinwoo-H
Jinwoo-H changed the base branch from sta-11064-sonnet-5-5-pricing to main October 18, 2026 23:45
@Jinwoo-H
Jinwoo-H force-pushed the sta-11064-haiku-5-5-long-context branch from 2c99fa3 to f37e38c Compare October 18, 2026 23:45
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d3aee044-8030-442c-8a9c-f3a22400664a
📥 Commits

Reviewing files that changed from the base of the PR and between 04ce3d3 and f37e38c.

📒 Files selected for processing: 12
  • src/main/claude-usage/claude-billable-tokens.ts
  • src/main/claude-usage/claude-model-pricing.test.ts
  • src/main/claude-usage/claude-model-pricing.ts
  • src/main/claude-usage/claude-usage-automation-attribution.ts
  • src/main/claude-usage/claude-usage-report-aggregation.test.ts
  • src/main/claude-usage/claude-usage-report-aggregation.ts
  • src/main/claude-usage/scanner-long-context.test.ts
  • src/main/claude-usage/scanner.ts
  • src/main/claude-usage/store.test.ts
  • src/main/claude-usage/transcript-usage-projection.ts
  • src/main/claude-usage/types.ts
  • src/main/claude-usage/usage-aggregation.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

📝 Walkthrough

The change adds long-context token tracking to Claude usage aggregates and persisted projections. It adds model-specific long-context pricing for eligible models and updates cost estimation to accept billable-token aggregates. Report aggregation and automation attribution now pass these aggregates to cost estimation.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to f37e3

No actionable issue is established from the available evidence. The change is mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 19.23% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 26 functions across 12 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly identifies the main change: per-request pricing for Claude long-context usage and pricing support for Haiku 5.5.
Description check Passed The description is complete and follows the required template. It explains the user impact, implementation, rationale, linked issue, testing, compatibility considerations, and the absence of UI change…
Full details: Description check

Explanation

The description is complete and follows the required template. It explains the user impact, implementation, rationale, linked issue, testing, compatibility considerations, and the absence of UI changes. It also clearly states that manual app testing was not performed and that the full checklist commands were not all run.

  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Jinwoo-H
Jinwoo-H merged commit f8a5d3e into main Oct 18, 2026
39 of 59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant