Repository navigation
A turn's cost counts every call it made, the plane's row follows it, and compacting stops when it no longer helps - #1265
Merged
Conversation
… no longer helps A summary that fails (refused, timed out, its stream crashed) is written as compaction_failed, with the call as stopped, so a replay, the listing, the ledger and the session's summary count it; Session.Summary.fold counts the summariser's call on compacted and a stopped call on llm_error, cancelled and compaction_failed, so its totals agree with the listing. A pod reports what a session has cost as calls are folded, at most once every five seconds, so the plane's row says a subagent's calls while its root waits. compacted carries thinking_resent, thinking_dropped and prefix_mismatch, which Bench.Prefix counts. A compaction that leaves the prompt over the threshold holds compacting until a prompt is under it again, said in a config_note when it starts to, rather than compacting before every call. prompt_bytes.brief is the brief's size under a stable system prompt too, where it is part of the conversation. Refs #1050 Signed-off-by: Martin Johannesson <martin.johannesson@it-minds.dk>
…UI formats Signed-off-by: Martin Johannesson <martin.johannesson@it-minds.dk>
…ix/1050-turn-cost Signed-off-by: Martin Johannesson <martin.johannesson@it-minds.dk>
This was referenced Nov 28, 2026
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.
Fixes #1050 (entries D72, D53, D122; D92 measured, with a proposal below).
What was there
Session.Summary.foldcountedllm_responseonly, so its totals disagreed with the listing, which also counts the summariser's call oncompactedand a stopped call'sstopped.session.statuson lifecycle changes only. A subagent's calls move the cost while the root stands still on itsdelegate, so the plane's row kept the cost from before them until the root moved.compactedhad nothinking_resent/prefix_mismatch, soTroupe.Bench.Prefix.count/1missed a summariser call sent again. With a context window close to the base prompt's size, what a compaction left was still over the threshold and the agent compacted before every call, with nothing to stop it.llm_request.prompt_bytes.briefwas 0 under the stable system prompt.What changed
compaction_failed:reason(threshold,context_overflow,manual, ascompactedhas it),error(a sentence),command_id,model,prompt_bytes, andstoppedinllm_error's words when the agent stopped the call, or when the provider had reported usage before a refusal or a crash. A new event and notllm_errorwith a field, because clients and the plane readllm_erroras the turn ending on a failed call (the desktop's rest state, A2A'sfailed), and this turn goes on. A manual compaction still writes itsllm_errorafter it, as before. The agent's replay,Spend.of,Log.Fold,Session.Usage(the ledger), the index's dormant totals, the live bench andSession.Summarycount itsstopped.Session.Summary.foldnow countscompactedusage andstoppedonllm_error,cancelledandcompaction_failed. The terminal and@troupe/clientcount its usage and show one line for a compaction the harness started ("context not compacted: …; the conversation goes on as it was"). For one a person asked for, thellm_erroralready says it. Schema regenerated; PROTOCOL.md says all of this.llm_response,compactedwith usage, and astoppedonllm_error,cancelledorcompaction_failed). The first one starts a 5 s timer, and when it fires the manager reports status if the fields moved. So a busy session reports its cost at most once every five seconds, and lifecycle reports still carry the cost as before.compactedcarriesthinking_resent,thinking_droppedandprefix_mismatch, asllm_responsedoes, andBench.Prefix.count/1counts them.config_note(keycompaction, withmodelandlimit), the same shape the harness already uses for what the configuration did to the session: "Compaction is paused: the prompt was still N tokens right after compacting, over the T at which compacts, so the agent goes on without compacting until a prompt is under that again." A replay folds the hold back from that note and lets it go at anllm_responseunder the threshold.Log.Foldwitnesses the note. A provider refusing a prompt as too long is still handled as before, and a manual compaction still runs.prompt_bytes.briefunder the stable system prompt is the brief's size once the turn context has put it into the conversation, until a compaction rewrites the conversation; before that it is 0.{:input_tokens, n, step}and{:thinking_resent, mismatch, step}.D92: measured, not changed
Decision 793 is not changed here.
fake-model):llm_request.tools_changedis true once, at call 11, whentodo_write/todo_readjoin the tools. The same turn onclaude-opus-5-5, which binds its thinking: 0 changes, with the todo tools there from call 1.test/fixtures/(nine logs with the field, 33 compared requests): onetools_changed: true, incheckpoints/manual.jsonlat its third call, where a list had been written and the tools joined.So the cost is one full cache write of the prompt prefix per long turn with no list, plus one after a list first exists.
Proposal for the next chunk. Offer
todo_writeandtodo_readfrom the first call on every model, as 867 does for models that bind. Keep 793's point, fewer list calls on small tasks for models likeqwen3-235b, by enforcing it in the tool instead of in the tool set: before ten calls, atodo_writeon a turn with no list answers with a refusal sentence ("a task list is for work longer than this turn has run; carry on without one"), and that call costs no extra round trip. The tools then stay the same for the whole session on every provider, and the prefix stays cacheable. The cost is the two definitions (about 1 kB) on every call, which a cached prefix serves at the read price. This would take a decision that supersedes 793's gate;TaskListCallsTestwould need to showfollow_stepsstill within nine calls.Verified
Each new or changed test fails before the change (run against the lib files set aside) and passes after:
CompactionTest(thinking_resent on compacted; window close to the prompt holds after one compaction and says so in one sentence; lets go once a prompt is under the threshold; a refused summary is written with nostopped),StoppedCallTest(a stopped summary is written withstopped,Session.Summaryagrees withSpend.of, the ledger makes a row, and a restart charges the budget the same),TurnCostTest(brief counted under the stable prompt),SummaryTest(fold counts every call the listing counts).TurnCostStatusTest("a subagent's calls reach the row while the root waits on its delegation" fails before withno matching session.status within 8000ms).TurnCostTestand@troupe/clientturn-cost.test.tscover the new event.Gates (WSL, empty KUBECONFIG):
mix check: coreResult: 2019/2031 passed (13/13 doctests, 3/3 properties, 2003/2015 tests). The 12 failures are the knownrg-shim set (FileToolsTest grep ×6, ReadOutputTest, ReadRootsTest, PathOnlyLookupTest, BenchTest ×2 oncut_output, DoctorTest bench). Also:1606 passed, 9 excluded(the apps before core), gateway437 passed (2 properties, 435 tests), 2 skipped, worker361 passed, daemon32 passed, a2a53 passed.mix credo --strict: no issues.mix format --check-formatted: ok.mix troupe.boundaries: no violations.clients/tuimix check:991/997 passed (2/2 doctests, 989/995 tests). The failures are BenchCLITest ×2 and DoctorCLITest (thergshim) and AgentsMendTest ×3 under load; AgentsMendTest and TurnCostTest run alone:12 passed.clients/gui: typecheck, build, tokens, icons and lines all pass; tests: client527 pass, 0 fail, console409 passed, desktop552 passed, 2 skipped.mix troupe.schema.diff:schema unchanged: 230 documents, aftertroupe.schema.gen(newevents/compaction_failed.json,compacted.jsonandindex.jsonupdated).decision-citations.exs --base origin/development-2026-11-28-2: none adds a decision number to code. Citations in the comment blocks I touched were taken out.doc-size.exs,doc-links.exs: hold.Result: 2029/2041 passed (13/13 doctests, 3/3 properties, 2013/2025 tests), the same 12rg-shim failures; workerResult: 361 passed;schema unchanged: 230 documents.Decisions
Read 788, 793, 769, 815, 863, 867, 774, 1029. Took none: 788's stopped call is now also written for a summary, as 788 meant ("at its timeout in a turn or in a compaction"), and 793 is unchanged. 769's
briefnow counts the brief where a stable prompt puts it; the schema and PROTOCOL.md say so.Left
Session.Checkpointfacts do not count astoppedonllm_errororcancelled(nor the newcompaction_failed) in their spend. That was already true before this change and is out of A turn's cost counts every call it made and its prompt prefix stays cacheable #1050's files. Worth its own issue if checkpoint spend feeds a total.llm_errorafter a stream that had reported usage (not a timeout) is still not counted inthinking. Only the summariser's case is fixed here.