feat(usage): add canonical token accounting v2 - #549
Open
seakee wants to merge 4 commits into
Open
Conversation
Add CPA accounting-v2 parsing, validation, conservative legacy fallback, and non-overlapping token buckets. Preserve legacy compatibility fields while making canonical totals authoritative for downstream persistence and reporting.
Extend SQLite usage schemas, migration state, and background workers for canonical accounting buckets and safe saturating token math. Keep migrations resumable and fail closed for unsupported or malformed persisted accounting data.
Propagate canonical input, output, reasoning, cache, unclassified, and completeness fields through projection, monitoring, rollups, and pricing. Use conservative fallback and saturating aggregation so malformed rows remain visible without being incorrectly priced.
Normalize nested and flat accounting payloads across realtime monitoring, usage analytics, dashboard token mix, and demo fixtures. Expose reasoning, non-reasoning, cache, and unclassified buckets while warning when cost estimates exclude unclassified portions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adapt CPA token accounting v2 to the current
devarchitecture and make its canonicaltoken_breakdownthe source of truth for persisted usage, analytics, and cost estimation.Legacy usage remains compatible, while ambiguous or malformed accounting is preserved as unclassified and excluded from unsafe pricing.
Scope
Changes
User Impact
Users get consistent token totals and cost estimates across realtime events, monitoring, dashboard, and analytics.
Reasoning and cache buckets no longer overlap, and requests with ambiguous token semantics remain visible as unclassified instead of being guessed or incorrectly billed.
Compatibility / Runtime Notes
usage_events.Data / Security Notes
SQLite schema and derived rollup state are updated.
usage_eventsremains the source of truth, original legacy fields and raw provenance are retained, and malformed canonical claims fail closed into unclassified totals.No new credentials, secrets, external telemetry, or raw failure-body exposure are introduced.
Risk / Rollback
Risk level: Medium
Rollback notes:
Stop the Manager Server and restore a pre-upgrade SQLite backup before returning to an older binary. The migration is additive and preserves source usage events, but pricing and monitoring derived-state versions advance to accounting-v2 schemas and should be rebuilt by the matching application version.
Verification
Commands / evidence:
Screenshots / Recordings
N/A — this extends token data shown inside existing dashboard, analytics, and realtime layouts; component and presentation tests cover the changed states.
Docs
Docs decision:
No new setup or operator workflow is introduced. The next release notes should call out the automatic SQLite backfill, canonical token composition, and incomplete-cost semantics.
Related
N/A