Skip to content

fix: account for usage quality and provider-specific costs per event - #253

Draft
ramapitecusment wants to merge 11 commits into
router-for-me:devfrom
ramapitecusment:fix/usage-accounting-integrity
Draft

ramapitecusment wants to merge 11 commits into
router-for-me:devfrom
ramapitecusment:fix/usage-accounting-integrity

Conversation

@ramapitecusment

@ramapitecusment ramapitecusment commented Sep 12, 2026

Copy link
Copy Markdown

Review status: This is the broader accounting/delivery proposal, pending agreement on the core delivery design in router-for-me/CLIProxyAPI#5765. The first independent core normalization fix is router-for-me/CLIProxyAPI#5817.

Usage costs are currently calculated after SQL grouping with a universal context threshold, losing additivity and provider-specific billing behavior. The desktop also ignores authoritative v2 breakdowns, cache lifetimes and media cost dimensions, and can present incomplete usage as fully priced. For example, Gemini 2.5 Pro with 10k input, 1k visible output and 9k thinking is $0.1125 under the checked tariff, rather than the previous $0.0225.

This change normalizes v2 usage, retains raw measurements and their quality, and values each event before summing. Known valuations are snapshotted at ingestion. Model-specific context/tier rules, Claude 5m/1h cache writes, Gemini thinking/audio, OpenAI text/image/audio/cache splits, exact reported xAI media amounts and cumulative video polling are handled separately. Unsupported or missing dimensions return an explicit unknown amount instead of a zero-cost or falsely complete estimate. Model matching is restricted; unknown media variants cannot inherit text-model rates. Manual tariffs can be scoped to provider and upstream URL, and failed price synchronization preserves the last good cache.

The collector consumes the companion core's durable journal, commits a SQLite inbox before ACK, and deduplicates by event ID while preserving repeated request IDs. Raw credentials are fingerprinted before inbox persistence. The UI uses unique row IDs and exposes endpoint/transport/kind filters, logical generation counts, measurement quality and cost coverage. Desktop health probes now request bounded output and collect final usage for Chat, Responses, Claude and Gemini, including partial failure records.

Validation / TDD

  • Recorded red-to-green regressions for the original arithmetic/quality findings, health parsing, modality pricing, unpriced hosted tools and credential persistence.
  • Native regression coverage includes context boundaries, mixed grouping, response-tier precedence, historical snapshots, provider-specific manual tariffs, replay, repeated polling and unknown-price behavior.
  • Rust: 534 passed, 0 failed (cargo test --manifest-path src-tauri/Cargo.toml, with a real macOS TMPDIR).
  • Frontend: 305 passed, 0 failed (bun test).
  • bun run build (TypeScript + Vite) passed.
  • Fixed existing backup-test symlink cleanup for Unix while preserving Windows junction handling; no backup production behavior changed.

Core dependency: router-for-me/CLIProxyAPI#5765. This desktop update works with old cores via legacy fallback, but complete source coverage and durable delivery require the companion core. The current dev pin is 7.3.1. That core already provides the existing canonical token breakdown, but does not contain the proposed journal/ACK endpoints and extended billing metadata from the companion PR. Complete durable delivery still requires a release containing the agreed core changes and a subsequent pin update through the normal release workflow.

Limits and corrected assumptions: API-rate estimates are not subscription invoices. Lost historical usage, missing cache lifetimes, opaque WebRTC media, ambiguous hosted-tool tariffs and arbitrary plugin traffic cannot be reconstructed. Existing unsnapshotted records may still use available current prices; the new DeepSeek schedule is not applied retrospectively. See docs/usage-accounting.md for the route/source matrix, valuation statuses, official pricing sources and deployment contract.

Independent review follow-up: Fixed missing provider/base-URL IPC fields; actual Claude speed versus Priority commitment pricing; eligible regional processing uplift; terminal/error/NDJSON health parsing and provider-specific cache/tool-input counters; propagation of actual response tiers; interrupted schema migration recovery; and the previously unpopulated event-kind/generation counters. SQLite now uses synchronous FULL so ACK follows a durable WAL commit. New regressions were observed failing before fixes, and the full updated suites pass. These follow-up changes also correct the earlier claim that the event counters were already populated.

Integration with current dev: Preserves upstream Keeper token normalization, Redis queue fallback and management-key rotation. Legacy Gemini normalization runs once; schema-v2 breakdown remains authoritative. A regression reproduced 19,000 output tokens instead of 10,000 before removing the duplicate fold. Added integration coverage for the single fold, unknown producer contracts and v2 precedence. The merged dev pin is core 7.3.1.

ramapitecusment and others added 10 commits September 12, 2026 18:43
- require explicit model discovery before saving DeepSeek connections
- preserve selected models when refreshing the catalog
- mark model lists stale when connection details change
- add model refresh support to easy mode
- apply built-in DeepSeek reasoning levels consistently
- Separate selected and unselected models with independent search and bulk actions
- Fix selection initialization and ignore stale discovery responses
- Improve keyboard navigation and responsive layouts
- Update translations and add model selection UI regression tests
- Align TPS and token normalization with Keeper
- Add Redis usage queue batch pulling with HTTP fallback
- Prevent cache and invalid token double counting
- Refresh usage details immediately after new records arrive
- Detect Web UI secret and port changes in the usage collector
- Drop stale Redis subscriptions authenticated with the old key
- Reset queue source selection and reconnect with the latest configuration
@ramapitecusment
ramapitecusment changed the base branch from main to dev September 14, 2026 05:40
@ramapitecusment
ramapitecusment marked this pull request as draft September 14, 2026 05:40
@ramapitecusment

Copy link
Copy Markdown
Author

@lzt404 I have updated this proposal against current dev and retargeted it to dev. The integration preserves the upstream Keeper normalizer, Redis fallback and management-key rotation. Tests caught a double Gemini reasoning fold introduced by combining the two implementations; that is fixed, with new regressions. Current validation: 534 Rust tests, 305 Bun tests, TypeScript check and production frontend build pass.

I am keeping this broader proposal in draft while separating review units and clarifying the core delivery contract. The first independent core correction is router-for-me/CLIProxyAPI#5817 (DeepSeek cache hits, existing schema, three production lines). The broader core implementation remains in draft at router-for-me/CLIProxyAPI#5765.

For this GUI, I propose separating per-event/provider pricing from durable ingestion and endpoint/transport coverage. Would you prefer reviewing the pricing portion first, with journal/ACK integration held until the core design is agreed? The pin now follows dev at 7.3.1; the description explains exactly which proposed core capabilities are still dependencies.

@lzt404
lzt404 force-pushed the dev branch 4 times, most recently from d1d1f8d to 7b72d97 Compare September 20, 2026 16:33
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.

2 participants