Skip to content

fix: estimate cost when stored zero is placeholder#4

Merged
Cateds merged 3 commits into
Cateds:masterfrom
alanzchen:fix/zero-cost-fallback
Apr 13, 2026
Merged

fix: estimate cost when stored zero is placeholder#4
Cateds merged 3 commits into
Cateds:masterfrom
alanzchen:fix/zero-cost-fallback

Conversation

@alanzchen
Copy link
Copy Markdown
Contributor

Why: some OpenCode setups store zero cost for nonzero-token responses, which makes oc-stats show zero cost everywhere because it always trusts stored cost values. Fix: estimate pricing when stored cost is zero on nonzero-token events by default, add a --keep-zero flag to preserve the original behavior, and cover the new behavior with tests and docs.

Copilot AI review requested due to automatic review settings March 24, 2026 20:10
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a cost-reporting edge case where OpenCode stores cost: 0 as a placeholder despite nonzero token usage, causing oc-stats to display $0 everywhere by always trusting stored costs. It introduces a default behavior to estimate costs when a stored zero looks like a placeholder, with an opt-out CLI flag to preserve the previous “trust stored zero” behavior.

Changes:

  • Add ZeroCostBehavior and centralized update_price_summary helper to resolve stored-vs-estimated cost consistently.
  • Thread zero_cost_behavior through analytics snapshot + UI app wiring, and add --keep-zero CLI flag.
  • Add/adjust tests and document the new default + opt-out behavior in the README.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/utils/pricing.rs Introduces ZeroCostBehavior and update_price_summary with unit tests for placeholder-zero handling.
src/ui/app.rs Stores and propagates zero_cost_behavior into snapshot recomputation.
src/main.rs Adds --keep-zero flag and maps it to ZeroCostBehavior passed into the app.
src/analytics/model_stats.rs Switches model/provider cost accumulation to the shared update_price_summary helper.
src/analytics/mod.rs Updates snapshot building to use update_price_summary and threads zero_cost_behavior through chart builders; updates/adds tests.
README.md Documents the new default estimation behavior and the --keep-zero opt-out.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Cateds
Copy link
Copy Markdown
Owner

Cateds commented Mar 26, 2026

There's a clear counterexample: model calls under OpenAI Codex subscription. They would have issues with this strategy.

When using OAuth to log into Codex, all cost values for openai provider model calls (like gpt-5.4) are correctly recorded as 0. Treating 0 as a placeholder would fallback to models.dev's OpenAI API-based pricing logic. The result would be a discrepancy exactly matching my Codex usage cost compared to what opencode stats and opencode stats --models showed when I was debugging. The current logic is at least consistent with OpenCode's own statistics in my case.

I'm curious what you mean specifically by "some OpenCode setups store zero cost for nonzero-token responses". The previous default behavior should display something like $5.09 + ? for unknown providers (though after fully trusting 0, that fallback code path did become dead code). Do you have a more specific example? I haven't verified whether configuring cost in opencode.json would also write to the database's cost field.

@alanzchen
Copy link
Copy Markdown
Contributor Author

My motivation was using this with my OpenAI Codex subscription. I wanted to estimate how much it would have cost me if I didn't have the subscription. That being said, I agree that a more consistent design would be the current behavior by default but add an --ignore-zeros option.

Cateds added 2 commits April 14, 2026 00:50
Preserve existing behavior by default and add --ignore-zero for setups that write placeholder zero costs. Reuse the same pricing path in daily aggregation so analytics views stay consistent.
@Cateds
Copy link
Copy Markdown
Owner

Cateds commented Apr 13, 2026

I'm sorry for the delay. I changed it to default Keep Zero, and then optionally ignore Zero.

@Cateds Cateds merged commit 540b526 into Cateds:master Apr 13, 2026
1 check 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.

3 participants