Skip to content

fix(cost): accumulate a session's spend split request by request - #729

Merged
huichen merged 2 commits into
mainfrom
fix/cost-split-per-model
Sep 20, 2026
Merged

huichen merged 2 commits into
mainfrom
fix/cost-split-per-model

Conversation

@huichen

@huichen huichen commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

Problem

The session-spend panel's four per-category rows disagreed with its own total as soon as a conversation switched models — sometimes by orders of magnitude.

  • The total was already accumulated per request (credit_cost when the provider reports it, otherwise that request's model prices), so it was never wrong.
  • The per-category rows were derived in get_state: the session's token totals × the current model's rates. Everything the session had ever spent got re-priced at the last model's rates.

Real example from a local session database (future/deepseek-flash → azure/gpt-6-astra → back):

amount
total (provider-billed, accumulated) ¥1.2992
rows before (current model = flash) ¥0.0046
rows after (each request at its own model) ¥1.2992

Fix

Charge the split where the total is already charged — once per request, at the rates of the model that served it — and persist it with the other counters so a restart keeps it.

  • Cost::parts() becomes the single definition of the "non-cached input" billing rule; estimate() sums it.
  • CostSplit accumulates [input, output, cache_read, cache_write]; Loop carries it beside cumulative_cost and charges it at the same two sites (per LLM call, and the compaction summary request).
  • RpcSession persists it in the run-end / manual-compaction session_info metadata and restores it in switch_session.
  • get_state returns the accumulated split; a session with none (written by an older agent) keeps the previously derived figure as a legacy fallback.

No wire or proto change — the four fields already exist.

Tests

  • A session that runs on a model priced ¥1/M and then one priced ¥100/M bills ¥101, not ¥200. Forcing the old derivation makes the test fail with exactly 200.0 vs 101.0.
  • get_state prefers the accumulated split over re-pricing the totals (fails with 200.0 vs 101.0 when the fallback is forced).
  • The compaction summary request adds to the split rather than replacing it, and a restart restores it (both fail when the write/restore is disabled).

cargo fmt -p future-agent --check, cargo clippy -p future-agent --all-targets -- -D warnings, cargo test -p future-agent (1790 passed).

`get_state`'s per-category amounts were derived from the session's token
totals at the *current* model's rates. A conversation that switches models
re-priced everything it had ever spent at the last model's rates, so the four
rows could disagree with the total by orders of magnitude (the total itself is
already accumulated per request and was never wrong).

Charge the split where the total is charged — once per request, at the rates of
the model that served it — and persist it with the other counters so a restart
keeps it. Sessions with no accumulated split keep the derived figure as a
legacy fallback.

Measured against the local session database: a session that ran on
future/deepseek-flash, then azure/gpt-6-astra, then back reported rows summing
to ¥0.0046 next to a ¥1.2992 total; per-request pricing reproduces ¥1.2992.
@huichen
huichen enabled auto-merge (squash) September 20, 2026 01:01
@huichen
huichen disabled auto-merge September 20, 2026 01:03
@huichen
huichen merged commit 5a1985b into main Sep 20, 2026
12 checks passed
@huichen
huichen deleted the fix/cost-split-per-model branch September 20, 2026 01:06
huichen added a commit that referenced this pull request Sep 20, 2026
…le history (#735)

#732 replayed a session's journal only when its `session_info` carried no split
at all. But the first build that accumulated a split (#729) started accumulating
wherever a session already was, so sessions that existed then got a split that
covers only the tail of their history — and a split that exists is exactly what
suppressed the replay.

A live example: a session showing ¥8.4141 when it was upgraded now reports rows
summing to ¥0.908176 against a ¥9.3223 total — and 9.322278 − 8.414102 is that
same ¥8.4141, i.e. the rows cover only what was spent after the upgrade.

Mark a split as covering the whole history (`cost_split_complete`) and replay
unless that mark is present, rather than unless the split is absent:

- the replay path writes the mark together with the split it cached;
- the run-end and manual-compaction writes carry it forward, so a load does not
  replay again;
- a session built in this process starts from zero counters, so its split covers
  its whole history by construction and it carries the mark from its first write.

Against the local session database this turns every session into a replay (no
stored mark exists yet — the previous build never wrote one) and the session
above goes from ¥0.9082 to ¥9.6968 of rows for its ¥9.3223 total.

## Still not matching, and why

Ten of those sixty sessions remain off by more than 50%, in the other direction:
their **total** is the undercount. They ran on `azure/gpt-6-astra` while that
model had no prices configured, so every one of those requests was charged at an
estimate of zero and the session's accumulated total never included them.
Replaying their journal now prices the same requests at today's rates for that
model (70 / 350 / 7 / 90 per 1M), which is the first time those requests have
been priced at all. That gap is not something this change can close: the total is
what was accumulated at the time, and re-deriving it would rewrite history rather
than report it. Worth a separate decision.

## Tests

- A session whose stored split covers only its tail (a partial `input` of 3.0
  next to a journal that prices to 101) is replayed and marked complete;
  ignoring the mark fails it with the stale 3.0.
- A session whose split is marked complete keeps it, even when its journal would
  replay to something else.
- A session with no split at all still replays (regression guard for #732).

`cargo fmt -p future-agent --check`, `cargo clippy -p future-agent --all-targets -- -D warnings`, `cargo test -p future-agent` (1795 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.

1 participant