Repository navigation
fix(cost): accumulate a session's spend split request by request - #729
Merged
Merged
Conversation
`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
enabled auto-merge (squash)
September 20, 2026 01:01
huichen
disabled auto-merge
September 20, 2026 01:03
This was referenced Sep 20, 2026
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
credit_costwhen the provider reports it, otherwise that request's model prices), so it was never wrong.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):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.CostSplitaccumulates[input, output, cache_read, cache_write];Loopcarries it besidecumulative_costand charges it at the same two sites (per LLM call, and the compaction summary request).RpcSessionpersists it in the run-end / manual-compactionsession_infometadata and restores it inswitch_session.get_statereturns 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
200.0vs101.0.get_stateprefers the accumulated split over re-pricing the totals (fails with200.0vs101.0when the fallback is forced).cargo fmt -p future-agent --check,cargo clippy -p future-agent --all-targets -- -D warnings,cargo test -p future-agent(1790 passed).