Audit Pi cache migration for tiered models.dev pricing
Context
Models.dev pricing landed across #863, #881, #883, #884, and #886.
#886 changed Claude/Pi cost reporting to preserve tiered pricing boundaries. This raised a Pi cache migration question.
What changed
Old Pi behavior
After #884, Pi parsed cost per assistant message, then report-building preferred recomputing cost from the aggregated day/model bucket:
-
Per-message cost was computed while parsing:
Sources/CodexBarCore/PiSessionCostScanner.swiftextractUsage(...)- lines 508-523 on current main
-
But
buildReport(...)priced the aggregate first:- old #884 code,
Sources/CodexBarCore/PiSessionCostScanner.swift - around the
currentPricingCost/costNanosblock - behavior was:
let currentPricingCost = computedCostUSD(... usage: packed ...) let costNanos = currentPricingCost ?? cachedPerMessageCost
- old #884 code,
That is fine for flat pricing, but wrong for tiered pricing. price(sum(tokens)) can differ from sum(price(request)).
New Pi behavior
#886 added sample-count tracking:
-
PiPackedUsage.usageSampleCountSources/CodexBarCore/PiSessionCostCache.swift:71-79
-
New parse path marks each parsed usage sample:
Sources/CodexBarCore/PiSessionCostScanner.swift:515-523
-
New report path only trusts cached per-message cost when complete:
Sources/CodexBarCore/PiSessionCostScanner.swift:624-635
-
Merge logic carries the sample count forward:
Sources/CodexBarCore/PiSessionCostScanner.swift:727-744
Claude comparison
Claude now has the same core idea:
-
Packed Claude usage stores:
- token totals
- cached cost
- sample count
- priced sample count
Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner+Claude.swift:51-64
-
Claude report uses cached per-request cost only when every sample was priced:
Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner+Claude.swift:655-676
Claude also has a cache artifact bump:
Sources/CodexBarCore/Vendored/CostUsage/CostUsageCache.swift:4-12.claudeand.vertexaiuse artifact version2
Open question
Should Pi use the same cache migration approach as Claude?
Pi still uses:
Sources/CodexBarCore/PiSessionCostCache.swift:3-15PiSessionCostCacheIO.artifactVersion = 1- cache file
pi-sessions-v1.json
Old v1 Pi caches decode usageSampleCount as nil. Unchanged session files can also skip reparsing by mtime/size:
Sources/CodexBarCore/PiSessionCostScanner.swift:194-200
That means existing Pi cache data may not get the new completeness metadata until a file changes or the user forces a rescan.
Proposal
Decide whether Pi should bump its cache artifact version to 2, matching the Claude migration pattern.
If yes:
- Change
PiSessionCostCacheIO.artifactVersionfrom1to2 - Ensure new caches write
usageSampleCount - Add a regression test that old v1 Pi cache data is ignored/rebuilt
- Confirm Pi Claude usage prices per message, not per aggregated day/model bucket
If no:
- Revert or rework the Pi-side #886 changes back to the prior aggregate-pricing model
- Remove or avoid relying on
usageSampleCountfor Pi cache correctness - Document why Pi should price aggregated day/model buckets instead of per-message cached costs
- Add a regression test that captures the intended old behavior so future changes do not silently reintroduce the per-message model
- Confirm this is acceptable for tiered Claude pricing through Pi sessions, since aggregate pricing can differ from per-request pricing
@ngutman since you added the original Pi session cost-history support in #653, could you sanity-check the Pi assumptions here?
The short version: #886 moved Pi Claude pricing toward per-assistant-message cached costs so tiered Claude pricing does not get recomputed from an aggregated day/model bucket. The open question is whether a Pi assistant message row is the right pricing boundary, and whether Pi session forks/copies can duplicate previously billed rows across files.
Things we are trying to confirm:
- Are persisted Pi assistant
messagerows final per-response usage records, not cumulative streaming chunks? - Can
message.provider/message.modelbe treated as authoritative when present, withmodel_changeonly as fallback? - Does
usage.totalTokensever differ materially frominput + cacheRead + cacheWrite + output, and if so which should drive cost? - Can
forkFrom/ copied session history duplicate prior assistant rows into a new session file, creating double-count risk if we scan every JSONL file independently?
Current leaning: bump Pi to pi-sessions-v2 now to invalidate old caches that lack usageSampleCount, keep per-message pricing for tiered Claude, and consider a follow-up row-based Pi cache similar to Claude if forks/copies need dedupe.
Looking into this, thanks for reporting
Thanks for the sanity-check prompt.
I investigated this against the local Pi session corpus and opened #889 for the immediate cache issue.
What #889 does:
- bumps the Pi cost cache artifact from
pi-sessions-v1topi-sessions-v2 - forces old cache entries without
usageSampleCountto be ignored - adds a regression test where a v1 cache contains aggregate-priced Claude usage, then verifies the scanner rebuilds per-assistant-message costs instead
Local Pi validation:
- scanned 743 Pi session files
- found 60,274 assistant
messagerows with usage - every assistant usage row had
providerandmodel usage.totalTokensmatchedinput + cacheRead + cacheWrite + outputfor all sampled assistant rows- no duplicate assistant message IDs
- no exact duplicate
timestamp + provider + model + usage tuplerows - I did not find a
forkFrom-style field in this corpus;parentIdappears to be message ancestry rather than copied-session billing identity
So my current read is:
- per-assistant-message pricing is the right boundary for now, especially for Claude long-context tier pricing
message.provider/message.modellook authoritative when present, withmodel_changeremaining a reasonable fallbacktotalTokensdoes not appear to diverge from the component token counts in this corpus- I would not block the v2 cache fix on fork/copy dedupe unless we find a real duplicated-session example
If Pi forks/copies do turn out to duplicate previously billed assistant rows across files, I think that should be a follow-up row-level dedupe/cache change, likely keyed by message id when present and falling back to a stable tuple like provider + model + timestamp + usage.
Great! I've yet to play around with Pi so I didn't want to make any assumptions there. Thanks