cccc icon indicating copy to clipboard operation
cccc copied to clipboard

Record provider token usage per settled turn

Open chriscoveries opened this issue 3 weeks ago • 2 comments

Need

An operator who runs several agents in one group cannot answer "what did this task cost?" from cccc today. The ledger records messages, deliveries and actor state, but no provider token counters. The counters exist (Pi RPC message_end usage, Claude transcript usage blocks); they are dropped when the turn settles. Any cost, budget or per-task report has to re-scrape runtime transcripts outside cccc. External transcript scraping must also establish which delivery started each turn; where that binding is unavailable, task attribution is unknown.

Requirement

  • For an owned, settled Pi RPC or Claude transcript turn with an available matching usage envelope, append a turn.usage ledger event carrying observed input, output, cache-read and cache-write counters, provider model, observed start/end times and settlement status. Unsupported runtimes, unobserved settlements and missing values remain unknown; a missing event is not zero usage.
  • No pricing, no budgets, no dispatch policy changes in cccc. Consumers price the counters themselves.
  • Attribution to a task only when it is exact (one delivery, one task reference, bound to that turn); otherwise the turn stays unattributed. No "latest delivery" guessing.
  • Unknown stays unknown: a missing counter, a crashed turn or an unsupported runtime is not a zero.
  • Use a stable turn-derived event ID and pending/committed recovery to make settlement retries safe. Consumers deduplicate by event ID and report incomplete or unreadable ledger ranges; no exactly-once billing guarantee is implied.

Evidence (real data)

In a Linux x86_64 staging install, an earlier revision recorded 85 Pi RPC turn.usage events across two groups over two calendar dates. The sample contains 85 distinct event IDs, 81 completed and 4 failed events, and 71 nonempty task-reference fields. These historical records do not include delivery IDs, so they do not establish exact task attribution. The failed events contain recorded counters, including zeros whose completeness is unknown. The records contain counter and identifier metadata, not prompt or tool content. This sample is not a live test of the proposed revision.

Question

Would you accept this as one ledger event kind (turn.usage) written at settlement, readable through the existing ledger queries? We have a PR prepared and will adapt it to the shape you prefer.

chriscoveries avatar Nov 06 '26 19:11 chriscoveries

Thanks for writing this up, and for keeping it to observed counters: no pricing, no budgets, and unknown stays unknown.

We don’t plan to add this rightnow. CCCC tries to keep what members can do consistent across runtimes. We’re keen on a feature most runtimes can support, but we’re careful with one that only a few can have. Usage for daemon-owned Pi and Claude turns would leave every other way of running a member with nothing, which looks like something broke, and a report couldn’t give a Group’s total. We made the same call on showing a member’s model and reasoning effort.

The numbers also need more work than the proposal suggests. A turn can contain several model calls: a captured Pi tool turn has two message_end events, each with its own usage. In one long Claude Code 2.1.317 session we checked, 81,518 lines carried usage for 38,758 message IDs; lines for the same message repeat the same block, so a plain sum of the counters came to about 2.1 times the per-message total, and subagent transcripts are separate. Once in the ledger, a total that looks complete but isn’t stays there.

Pi’s RPC events and Claude transcripts can contain these counters, so your report can keep reading them directly. If the missing piece is a reliable link between a CCCC delivery and a provider turn, please open a new issue for that, with what reads it and what it does next. A link like that would need to work across runtimes, and it’s a much smaller request.

We’d look at this again if token usage became available across most runtimes. Thanks for the careful evidence.

ChesterRa avatar Nov 07 '26 01:11 ChesterRa

Closing as you suggested: the smaller request, a persisted link between a delivery and the provider turn it started, is filed as #205.

chriscoveries avatar Nov 11 '26 03:11 chriscoveries