Repository navigation
fix(accounting): book Result.Interaction in the savings record as the metrics do - #2148
Conversation
… metrics do On a rescued request, Cortex dropped Result.Interaction from the savings record, but the accountant metrics booked it as llm.savings.interaction.usd. So the dashboard net and the metrics net differed by the interaction: $0.0036 against $0.0077 on the accountant's exact case (#2139). - Extras.InteractionUSD takes accountant.Figure's InteractionUSD, at the size without the ops. SavingsBilled with no figure prices it at the billed size. Basis "usd" only, as the metrics. It is in no Saving. - event.Event.InteractionUSD (interaction_usd) carries it on the cost record. Figures.InteractionMicros, usage.Counts.InteractionMicros and the session summary and fold sum it. It is signed. - agentop: money.NetMicros adds it, and the saved line names it in agentop cost and the TUI. - docs/pricing.md and docs/session-api.md: the field and the net formula. Fixes #2139 Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com> Signed-off-by: DAVID BOAZ <DAVIDBO@il.ibm.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 57 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
clawgenti
left a comment
There was a problem hiding this comment.
Summary
The design is right and the wiring is complete: Result.Interaction is taken from accountant.Figure, kept out of every operator's Saving and out of avoidedMicros, and carried through the event wire, Figures/Counts, the ring, the ledger, the fold/summary JSON and agentop's two surfaces — with the additive/omitempty rules respected and IsZero, hasAccountantFigure and session.fields all updated. I checked the central claim independently rather than only through the PR's test, across every billing mode and both signs, and the record and the metrics agree to the micro. I found no must-fix; the three comments below are a docs formula that omits keep-alive, a rendering branch that still says "no saving, only a penalty" when the only figure is the interaction, and a merge collision with #2141.
Author: boazdavid (CONTRIBUTOR — returning external)
Areas reviewed: Go (core/accounting, core/cost/event, core/cost/usage, core/cost/ledger, core/session, core/plugins/inferenceparser, cmd/agentop), docs (pricing.md, session-api.md)
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (all checks)
Not checked:
- The whole
coremodule —core/plugins/allandcore/plugins/contextgurudo not build here (context-guru@v0.7.9andmodernc.org/sqliteare not in the module cache and the module proxy times out). - A full
go test ./...incore— the threeAmerica/HavanaDST tests incost/ledger/cost/usagefail on this machine, exactly as the PR body says they do onmain; I confirmed the same two failures at base333f52band ignored them. Host.Savingsend-to-end with a real rescue — the engine reports an interaction only in the ping-state case, which the PR body already notes is hard to build throughHost; I used the PR's owninteractionResultfixture and priced it through both paths instead.
Verified by: go test -race ./money/ in cmd/agentop; go test ./accounting/ ./cost/event/ ./cost/usage/ ./session/ ./plugins/inferenceparser/ in core; go vet and gofmt -l on the changed packages; and a probe of my own comparing Extras.InteractionUSD with llm.savings.interaction.usd for api / subscription / unknown billing and for an interaction whose caused side is larger — 0.004143450 / 0.004143450, 0 / 0, 0 / 0, -0.000330300 / -0.000330300. The probe was deleted afterwards.
Generated by repoman github-pr-review@af425c8.
| operators saved and what they caused, priced by the accountant, basis `usd` | ||
| only. Net saved is the difference and may be negative. Beside them, absent at | ||
| only. `interactionMicros` is the value of the tokens no operator owns on the | ||
| session's rescued requests (signed). Net saved is `avoidedMicros − |
There was a problem hiding this comment.
nit — this formula leaves out keep-alive, so a reader who applies it gets a different number from the dashboard. money.NetMicros is avoided − penalty − keepAlive + interaction (and the same section, two paragraphs down, says "agentop's SAVED column is net of keepAliveMicros").
Concrete case: avoidedMicros=100000, penaltyMicros=0, keepAliveMicros=20000, interactionMicros=40000 → this sentence gives $0.14, money.NetMicros and the SAVED column give $0.12. docs/pricing.md in this same PR states the full four-term formula.
Suggested fix: Net saved is avoidedMicros − penaltyMicros − keepAliveMicros + interactionMicros``, which is what the code does.
| // The interaction is in the net too: the tokens no operator owns on a rescued | ||
| // request (usage.Counts.InteractionMicros). | ||
| if t.InteractionMicros != 0 { | ||
| ka += " interaction " + money.Signed(t.InteractionMicros, unit) |
There was a problem hiding this comment.
nit — this branch is keyed on AvoidedMicros == 0 alone, so a window whose only figure is the interaction renders as ~no saving penalty $0.00 interaction $0.04 net $0.04 estimate; no saving, only a penalty. It has a saving ($0.04 of interaction), and the interaction is not a penalty.
Concrete case: usage.Counts{AvoidedMicros: 0, InteractionMicros: 40000} — reachable whenever a rescued request books an interaction but no operator's net saving (and, once #2141's gate lands, on a withheld request that still carries the interaction).
Suggested fix: branch on all three terms, e.g. t.AvoidedMicros == 0 && t.InteractionMicros <= 0, and drop "only a penalty" from the tail when an interaction is present — or word it "no operator saved" rather than "no saving".
| func TestEventWireCoversEveryField(t *testing.T) { | ||
| // Keep in step with the marshal in TestEventJSONTagsArePinned_EveryField. | ||
| const pinned = 25 | ||
| const pinned = 26 |
There was a problem hiding this comment.
suggestion — this pin collides with #2141. That PR also adds one field to event.Event (SavingsWithheld) and also bumps pinned from 25 to 26, and it edits the same marshal string in TestEventJSONTagsArePinned_EveryField, so the two branches conflict here and whichever lands second will see Event has 27 fields, 26 are pinned (this field plus savings_withheld), not a merge you can take as-is.
Suggested fix: when the second of the two lands, set pinned to the combined count and put both fields in the marshal — or, if you would rather not track it, note the expectation in the PR body so the reviewer of the second PR knows the number is not a mistake.
clawgenti
left a comment
There was a problem hiding this comment.
Summary
The savings record now carries Result.Interaction − InteractionCaused as Event.InteractionUSD, taken from accountant.Figure (or from the billed size when there is no figure). It is wired through Figures → usage.Counts → ledger / ring / session summary / fold, and money.NetMicros adds it. I traced every enumeration of the figure fields (Counts.Add, AddFigures, session.fields/setOn, fold JSON, hasAccountantFigure, the TUI fold and sessions pane) and found none missed. Figures.IsZero is struct equality, so the ring and ledger admission gates pick the new field up without changes. The new TestCountsAddFigures_CarriesEveryFigure reflection check is a good guard against the next field being dropped.
Against accountant v0.9.0: metrics.Sink books fig.InteractionUSD (and UnownedReRateUSD) into llm.savings.interaction.usd on BillingAPI only. Gating the record on basis == BasisUSD therefore matches, and the regression test pins the two figures to the micro. The new wire field is float64, written only by Cortex itself, and praxis does not emit or list cost-record fields, so the decode path has no new strictness risk.
I have two doc nits (inline) and no blockers. One optional follow-up: playedNet in core/accounting/savings_agree_test.go still sums only the savings, spends and unattributed break. Adding extras.InteractionUSD there would make the accountant-cases agreement harness cover the interaction as soon as an upstream case exercises it.
Author: boazdavid (CONTRIBUTOR association, repo admin permission — maintainer)
Areas reviewed: Go (core/accounting, cost/event, cost/usage, cost/ledger, session, inferenceparser; cmd/agentop money/TUI/cost), Docs
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing, with no failure annotations on Go CI (core), Go CI (authbridge agentop) or Rust (praxis). The exception is Go CI (core peerproc, macOS), which hit the 10-minute job timeout in Build and test cmd/cortex (local profile) (cancelled; Vet and Test steps passed). That looks like runner capacity, not this change.
Local verification: go vet clean on the changed packages. go test passes on core/accounting, cost/event, session, inferenceparser and agentop money/tui. The only failures are the America/Havana DST tests in cost/usage and cost/ledger and TestServiceInstall_RefusesABrokenConfig in cmd/agentop, which are environment-dependent and fail identically on base 333f52b.
| only. Net saved is the difference and may be negative. Beside them, absent at | ||
| only. `interactionMicros` is the value of the tokens no operator owns on the | ||
| session's rescued requests (signed). Net saved is `avoidedMicros − | ||
| penaltyMicros + interactionMicros` and may be negative. Beside them, absent at |
There was a problem hiding this comment.
nit: This formula leaves out keep-alive, but the same paragraph later says agentop's SAVED column "is net of keepAliveMicros", and money.NetMicros subtracts it. Now that the sentence spells out a formula, it would read more accurately as avoidedMicros − penaltyMicros − keepAliveMicros + interactionMicros, matching docs/pricing.md.
| | `avoidedMicros` | the `usd` of applied savings with basis `usd`. Gross, as before the accountant | | ||
| | `penaltyMicros` | their `penaltyUsd`. Net saved is `avoidedMicros − penaltyMicros − keepAliveMicros`, and may be negative; nothing floors it. The keeper's `rescue` is in `avoidedMicros`, so the pings that earned it come off the net | | ||
| | `penaltyMicros` | their `penaltyUsd`. Net saved is `avoidedMicros − penaltyMicros − keepAliveMicros + interactionMicros`, and may be negative; nothing floors it. The keeper's `rescue` is in `avoidedMicros`, so the pings that earned it come off the net | | ||
| | `interactionMicros` | the records' `interaction_usd`: money, basis `usd`, **signed**. In no operator's figure and not in `avoidedMicros`, but in the net saving. With it, the net saving equals the net of the `llm.savings.*` metrics for the same requests | |
There was a problem hiding this comment.
nit: "equals the net of the llm.savings.* metrics" is true for Cortex's sized pricer. Note, though, that the sink's interaction.usd also books UnownedReRateUSD, and Cortex records that in unattributed_break_usd, which the net does not read. The library calls it a guard that is 0 with a size-only pricer, so nothing to fix in code. A short qualifier here (or a pointer to unattributed_break_usd) would keep the claim exact if that ever changes.
Summary
On a rescued request, the savings record now books
Result.Interaction, as the accountant metrics do. Effect: the dashboard net and the metrics net agree. Before, they differed by the interaction.It takes the figure from
accountant.Figure, which #2147 added toHost.Savings.Fixes #2139
What changed
Extras.InteractionUSDisFigure'sInteractionUSD.Figureprices it at the prompt size without the operators' changes, the same size the metrics use.Saving. No operator owns it (accountant design §6.2).usdonly. The metrics price none on a subscription, so the record books none.SavingsBilledwith no figure prices it at the billed size, like every other dollar on that path.event.Event.InteractionUSD, wire taginteraction_usd. It is signed: positive saves.Figures.InteractionMicrosandusage.Counts.InteractionMicroshold it. So do the ledger rows, the ring, and the session summary and fold. It is not inavoidedMicros.money.NetMicrosis nowavoided − penalty − keep-alive + interaction. The saved line inagentop costand the TUI showsinteraction $X.docs/pricing.md(the field, the column, the net formula) anddocs/session-api.md.How it was tested
TestSavings_TheInteractionIsBookedAsTheMetricsBookIt. It uses the accountant's exact interaction case: a mask cuts 1201 tokens on a rescued request.SavingsBilledwith no figure,Figuressign,AddFigurescarries everyFiguresfield (a reflection check), the fold JSON round trip,withAccountant,money.NetMicrosand the saved line.go vetwithGOWORK=offis clean incoreandcmd/agentop.gofmt -lprints nothing.go test ./...incorepasses, except 3America/HavanaDST tests incost/ledgerandcost/usage. They fail onmaintoo, with this machine's time-zone data.scripts/dev/go-test-shards.sh -race cmd/agentoppasses (1273 tests). The agentop subpackages and the changedcorepackages pass with-race.Open questions
Interaction(dash/accountant_price.go:83), but its metrics book it. That is a separate fix in that repo.Technical notes
Host.Savings. The engine reports an interaction only in one exact rescue case, and that needs ping state that is hard to build throughHost. The test calls the same code afterCompute:accountant.Figure, thennewFigured, thensavingsBilled.usage.Counts, so the new column needs no schema change. Old rows decode to 0.docs/cortex-wiring.mdinrossoctl/accountant. W7.1 namesEvent.InteractionUSD: "counted in the session total, never charged to a plugin".Assisted-By: Claude (Anthropic AI) noreply@anthropic.com