Summary
LLM Usage Accounting covers how token consumption and cost are recorded and reconciled for agent runs. The central difficulty is that a missing usage record is not the same as a zero usage record: runs that are killed (timeout/SIGTERM) never write their terminal usage line, so naive summarisers report a confident zero for precisely the most expensive runs. Accurate accounting also depends on correct handling of the vendor SDK's usage semantics (deduplication, cache pricing, error-result usage, and caller-side accumulation).
Key Points
- Absence ≠ zero. A usage record written once per process at terminal emit is absent for runs killed by timeout/SIGTERM. The file then holds tool-call lines only, and a summariser reports a confident zero for the most expensive runs. Absence must be named distinctly from zero.
- Exporting an env flag alone is insufficient (e.g.
YOYO_AUDIT=1does not close the gap). - Append-only sinks report cumulative totals unless there is a per-run watermark or a truncate mechanism.
- Sub-agent tokens are not counted at all (
yologdev/yoagent#173), so any total is a floor, not a measurement. - The largest unmeasured consumer is the social phase (~42 runs/week). It produces no commits to main, so its usual evidence of having run is absent and its spend is invisible in the same way a no-op run is.
Vendor SDK prerequisites (Anthropic Agent SDK cost-tracking docs)
- Parallel tool calls produce multiple assistant messages sharing one message id with identical usage — count each unique id once or totals inflate.
- Cache creation vs cache read tokens are priced differently and must be tracked separately from
input_tokens. - Both success and error result messages carry usage /
total_cost_usd. A conversation that fails midway still consumed tokens, so read cost from the result message regardless of subtype.