Skip to content

fix(ai): spend ledger overprices tiered models and prices calls at lookup time #661

Closed

Description

@dan-petty

Summary

Found in the release/v0.2.23 review (line numbers at 7fdaa9d).

  1. Tiered models are overpriced by 1.5 to 2x. Per-million rates come from a fake usage of 1M input and 1M output tokens (src/devops_cli/ai/spend/pricing.py:103, and again at :240 in _build_catalog). That usage crosses genai-prices' long-context tiers, so every call is recorded at >200K-token prices. Reproduced with the pinned genai-prices:

    Model Recorded (1M/1M tier) Correct for a 20K/2K call
    claude-sonnet-4-5 $6.00 / $22.50 $3.00 / $15.00
    gemini-2.5-pro $2.50 / $15.00 $1.25 / $10.00

    36 of 1,593 models are affected. The overstated costs flow into spend.db, list-pricing, benchmark median_cost_usd and counterfactuals.

  2. Prices depend on the time of the lookup, not the time of the request. calc_price is called without genai_request_timestamp. Time-windowed prices such as DeepSeek's off-peak rate follow the clock at lookup.

  3. devops ai cost update-pricing has no lasting effect. It only swaps genai-prices' in-memory snapshot (pricing.py:266-283, src/devops_cli/commands/ai_cost.py:566-594). Nothing is saved, and later commands use the bundled snapshot again. --source <path> fails, although the help text promises "URL or file path".

  4. Price overrides depend on the working directory. PricingRegistry resolves settings.data.dir against the current directory (pricing.py:134-140), while the ledger now uses resolve_data_path. Overrides set from a subdirectory or a linked worktree are invisible to reviews run elsewhere.

  5. cost roi --days N understates the daily rate. It divides by N even when only part of the window has data (src/devops_cli/ai/spend/payoff.py:14-15).

  6. The --days filters include an extra day. They compare ISO timestamps (...T...+00:00) as text against SQLite datetime('now') (YYYY-MM-DD HH:MM:SS) (src/devops_cli/ai/spend/ledger.py:80, :98, :114, :129, :142, :157). This existed on main, but the new queries copy it.

  7. The column migration can race (unconfirmed). The served_by/stage migration checks for the columns, then runs ALTER outside a transaction (ledger.py:229-233). Two processes opening an old spend.db can race. The second fails with "duplicate column name", which can re-send an LLM call that already succeeded.

  8. --format prometheus ignores the report options. It drops --days, --reference-model and --hardware-cost (ai_cost.py:340-344, :415-419; prometheus.py:133).

Remediation Plan

  1. Price each call from its real usage and request time with calc_price(usage, model, genai_request_timestamp=...). Where per-million rates are displayed, derive them from a representative small usage and label them as such.
  2. Pin a timestamp in the pricing tests, so they no longer depend on the clock.
  3. Persist fetched snapshots under the data directory and load them at startup. Support a file path for --source.
  4. Resolve pricing overrides through resolve_data_path.
  5. Divide by the observed span when it is shorter than --days.
  6. Compare timestamps in one format.
  7. Make the migration idempotent under concurrency: run it in a transaction and tolerate "duplicate column" errors.
  8. Pass the report options through to the Prometheus exporter.
  9. Add tests for each item.

Activity

  1. added this to the v0.2.23 milestone on Sep 28, 2026
  2. added
    type/bug
    scope/ai
    priority/p1-high
    on Sep 28, 2026
  3. dan-petty commented on Sep 28, 2026

    @dan-petty
    OwnerAuthor

    Closed by #676, merged into release/v0.2.23.

    GitHub only acts on closing keywords when a pull request merges into the default branch, so this was closed by devops gh issues close-merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/aitype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions