Skip to content

fix(credits): derive agent/quota/cycle surfaces from the ledger (retire counter drift)#35

Merged
WayforthOfficial merged 1 commit into
mainfrom
fix/ledger-derive-credit-surfaces
Jun 21, 2026
Merged

fix(credits): derive agent/quota/cycle surfaces from the ledger (retire counter drift)#35
WayforthOfficial merged 1 commit into
mainfrom
fix/ledger-derive-credit-surfaces

Conversation

@WayforthOfficial

Copy link
Copy Markdown
Owner

Whole denormalized-counter drift class in one pass (after invoice #32, pioneer #33).

1. Agent CREDITS (30D). The detail endpoint get_agent exposed no 30-day credits field → dashboard showed 0. Added a ledger-derived credits_30d to get_agent, and made list_agents.credits_30d ledger-derived (was SUM(agent_runs.credits_total), a denormalized column that can lag).
2. compute_calls_remaining (pre-money quota) read monthly_calls_count (drifts low → over-states remaining → under-charge). Now = allotment − credits_used_this_cycle() (ledger, per user). New shared helper.
3. The "X this cycle" subtitle + /billing/balance forecast also used monthly_calls_count → now ledger-derived.

"Used this cycle" = settled spend (execution+cross_rail+cloud_compute); transient agent_reserve holds excluded (release on completion; would double-count).

Validation (read-only, real prod data — staging lacks this data, so the exact queries were checked against prod): get_agent credits_30d=6 (was 0); compute_calls_remaining 239,560→239,112 (counter under-counted by 448). Read-only display change: no writes, no schema, no enforcement-path change (check_and_deduct_credits on credits_balance is unchanged).

🤖 Generated with Claude Code

…re drift)

Whole denormalized-counter drift class in one pass (after invoice #32, pioneer
#33). Each surface now reads the immutable credit_transactions ledger.

1. Agent CREDITS (30D).
   - get_agent (/cloud/agents/{id}, the DETAIL page) exposed NO 30-day credits
     field at all, so the dashboard had nothing to read and showed 0 even though
     runs billed correctly. Added a ledger-derived credits_30d (sum of the
     agent's run debits over 30d; cloud-run debits are tagged with the run id).
   - list_agents credits_30d previously summed agent_runs.credits_total (a
     denormalized per-run column that can lag/zero). Now ledger-derived too.

2. compute_calls_remaining() — the pre-money quota figure — read
   api_keys.monthly_calls_count, which drifts LOW (never counts NULL-key /search
   debits; LLM path under-increments), over-stating remaining (under-charge). Now
   = plan allotment − credits_used_this_cycle (ledger sum, per user). New shared
   helper credits_used_this_cycle() is the single source.

3. The "X this cycle" usage subtitle and the /billing/balance forecast also read
   monthly_calls_count. Both now use credits_used_this_cycle().

"Used this cycle" = settled spend (execution + cross_rail + cloud_compute);
transient agent_reserve holds are excluded (they release on run completion and
would otherwise double-count the run's execution debits).

monthly_calls_count is now display-vestigial (still written by _increment_calls,
read by nothing) — can be dropped in a later migration.

Validated read-only against real prod data: get_agent credits_30d=6 (was 0);
compute_calls_remaining 239,560→239,112 (counter under-counted by 448).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@WayforthOfficial WayforthOfficial merged commit 10137f9 into main Jun 21, 2026
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