Skip to content

fix(accounting): book Result.Interaction in the savings record as the metrics do - #2148

Merged
boazdavid merged 1 commit into
mainfrom
fix/2139-interaction-on-main
Nov 26, 2026
Merged

boazdavid merged 1 commit into
mainfrom
fix/2139-interaction-on-main

Conversation

@boazdavid

Copy link
Copy Markdown
Contributor

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 to Host.Savings.

Fixes #2139

What changed

  1. Savings record. Extras.InteractionUSD is Figure's InteractionUSD. Figure prices it at the prompt size without the operators' changes, the same size the metrics use.
    • It is in no operator's Saving. No operator owns it (accountant design §6.2).
    • Basis usd only. The metrics price none on a subscription, so the record books none.
    • SavingsBilled with no figure prices it at the billed size, like every other dollar on that path.
  2. Cost record. event.Event.InteractionUSD, wire tag interaction_usd. It is signed: positive saves.
  3. Totals. Figures.InteractionMicros and usage.Counts.InteractionMicros hold it. So do the ledger rows, the ring, and the session summary and fold. It is not in avoidedMicros.
  4. agentop. money.NetMicros is now avoided − penalty − keep-alive + interaction. The saved line in agentop cost and the TUI shows interaction $X.
  5. Docs. docs/pricing.md (the field, the column, the net formula) and docs/session-api.md.

How it was tested

  • Regression test: TestSavings_TheInteractionIsBookedAsTheMetricsBookIt. It uses the accountant's exact interaction case: a mask cuts 1201 tokens on a rescued request.
    • With the fix: the record books $0.004143. The metrics book $0.004143.
    • Without the fix: the record books $0.000000. The test fails.
  • More tests: subscription (no interaction in either output), SavingsBilled with no figure, Figures sign, AddFigures carries every Figures field (a reflection check), the fold JSON round trip, withAccountant, money.NetMicros and the saved line.
  • go vet with GOWORK=off is clean in core and cmd/agentop. gofmt -l prints nothing.
  • go test ./... in core passes, except 3 America/Havana DST tests in cost/ledger and cost/usage. They fail on main too, with this machine's time-zone data.
  • scripts/dev/go-test-shards.sh -race cmd/agentop passes (1273 tests). The agentop subpackages and the changed core packages pass with -race.

Open questions

  • Subscription units. Should a subscription request book the interaction in usage-limit units? The metrics book nothing there, so this PR books nothing.
  • context-guru. Its dashboard also ignores Interaction (dash/accountant_price.go:83), but its metrics book it. That is a separate fix in that repo.

Technical notes

  • The regression test does not go through Host.Savings. The engine reports an interaction only in one exact rescue case, and that needs ping state that is hard to build through Host. The test calls the same code after Compute: accountant.Figure, then newFigured, then savingsBilled.
  • The ledger row embeds usage.Counts, so the new column needs no schema change. Old rows decode to 0.
  • W-numbers are sections of docs/cortex-wiring.md in rossoctl/accountant. W7.1 names Event.InteractionUSD: "counted in the session total, never charged to a plugin".

Assisted-By: Claude (Anthropic AI) noreply@anthropic.com

… 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>
@boazdavid
boazdavid requested a review from a team as a code owner November 26, 2026 10:21
@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a11d2ea1-ef05-4150-b1a1-1d9025be75f2
Commits

Reviewing files that changed from the base of the PR and between 333f52b and 34d4451.

Files selected for processing: 21

  • cmd/agentop/cmd_cost.go
  • cmd/agentop/money/savings.go
  • cmd/agentop/money/savings_test.go
  • cmd/agentop/tui/linked.go
  • cmd/agentop/tui/sessions_pane.go
  • cmd/agentop/tui/spend_drawer.go
  • core/accounting/interaction_test.go
  • core/accounting/savings.go
  • core/cost/event/event.go
  • core/cost/event/event_test.go
  • core/cost/ledger/query.go
  • core/cost/usage/usage.go
  • core/cost/usage/usage_test.go
  • core/plugins/inferenceparser/accounting.go
  • core/plugins/inferenceparser/accounting_test.go
  • core/session/figures.go
  • core/session/fold.go
  • core/session/fold_test.go
  • core/session/summary.go
  • docs/pricing.md
  • docs/session-api.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 core module — core/plugins/all and core/plugins/contextguru do not build here (context-guru@v0.7.9 and modernc.org/sqlite are not in the module cache and the module proxy times out).
  • A full go test ./... in core — the three America/Havana DST tests in cost/ledger/cost/usage fail on this machine, exactly as the PR body says they do on main; I confirmed the same two failures at base 333f52b and ignored them.
  • Host.Savings end-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 through Host; I used the PR's own interactionResult fixture 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.

Comment thread docs/session-api.md
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 −

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread cmd/agentop/cmd_cost.go
// 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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread docs/session-api.md
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread 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 |

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@boazdavid
boazdavid merged commit b8be12c into main Nov 26, 2026
43 of 44 checks passed
@boazdavid
boazdavid deleted the fix/2139-interaction-on-main branch November 26, 2026 11:36
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix(accounting): book Result.Interaction the same way in the savings record and the metrics

2 participants