CodexBar icon indicating copy to clipboard operation
CodexBar copied to clipboard

Audit Pi cache migration for tiered models.dev pricing

Open iam-brain opened this issue 6 months ago • 4 comments

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.swift
    • extractUsage(...)
    • lines 508-523 on current main
  • But buildReport(...) priced the aggregate first:

    • old #884 code, Sources/CodexBarCore/PiSessionCostScanner.swift
    • around the currentPricingCost / costNanos block
    • behavior was:
      let currentPricingCost = computedCostUSD(... usage: packed ...)
      let costNanos = currentPricingCost ?? cachedPerMessageCost
      

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.usageSampleCount

    • Sources/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
  • .claude and .vertexai use artifact version 2

Open question

Should Pi use the same cache migration approach as Claude?

Pi still uses:

  • Sources/CodexBarCore/PiSessionCostCache.swift:3-15
  • PiSessionCostCacheIO.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.artifactVersion from 1 to 2
  • 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 usageSampleCount for 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

iam-brain avatar May 10 '26 17:05 iam-brain

@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 message rows final per-response usage records, not cumulative streaming chunks?
  • Can message.provider / message.model be treated as authoritative when present, with model_change only as fallback?
  • Does usage.totalTokens ever differ materially from input + 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.

iam-brain avatar May 10 '26 17:05 iam-brain

Looking into this, thanks for reporting

ngutman avatar May 10 '26 18:05 ngutman

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-v1 to pi-sessions-v2
  • forces old cache entries without usageSampleCount to 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 message rows with usage
  • every assistant usage row had provider and model
  • usage.totalTokens matched input + cacheRead + cacheWrite + output for all sampled assistant rows
  • no duplicate assistant message IDs
  • no exact duplicate timestamp + provider + model + usage tuple rows
  • I did not find a forkFrom-style field in this corpus; parentId appears 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.model look authoritative when present, with model_change remaining a reasonable fallback
  • totalTokens does 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.

ngutman avatar May 10 '26 18:05 ngutman

Great! I've yet to play around with Pi so I didn't want to make any assumptions there. Thanks

iam-brain avatar May 10 '26 19:05 iam-brain