Skip to main content

Module token_ledger

Module token_ledger 

Source
Expand description

Counts what an agent’s calls used against the Kanban task they belong to, as they pass through transcript::append (docs/specs/SPEC_TOKEN_ANALYSIS_PER_KANBAN_TASK_2026_11_22.md sections 4.1 to 4.4). srv counts: no agent reports or adds up its own tokens.

One call is one deduplicated message id. Claude writes a call’s usage again on each of its content-block lines, and only the output grows: the first sight of a call counts it, a later line of it adds what grew. Each counted call is kept (id, where it was counted, what was counted) with the counters in one write, so a replay, a restart or an upgrade adds nothing twice, however far back it goes. The task and its stage are decided when the call is counted, never rebuilt later. Counts, ids, model names and times only: nothing of a line is kept.

Which task a call belongs to, in this order (basis of its row):

  1. held, or shared when the agent held several: the task it held at that time;
  2. named: the agent holds none, and the call issued a Tasks action naming one (only its number is read);
  3. spawned: a worker of a leader that holds one, until the worker takes one of its own;
  4. unattributed: none of these. Kept per agent and day with an empty task, never spread over tasks.

The counters are written in batches, about once a second, by one thread; a crash loses at most the batch not yet written, together with the ids of its calls.

Structs§

FoundCall
A call a provider-specific reader found in the provider’s own file: its durable id and when it was made.
Ledger

Traits§

Directory
What the ledger needs to know of the world besides the task store.

Functions§

note_found
Counts calls a provider-specific reader found (FoundCall), for the agent whose transcript is zone.
note_prompt
Takes in the records srv wrote for a prompt it delivered, for the cause of the call that answers it.
note_records
Counts the calls in records (newline-terminated lines an agent wrote to zone). Called for every transcript write of the agent’s own output; a line that holds no usage costs a substring test.