Skip to main content

Module token_spend

Module token_spend 

Source
Expand description

Token spend: what each harness session used, read from the harness’s own records (Claude Code’s message.usage on each response, Codex’s cumulative token_count totals) and priced at the model’s list rates (crate::pricing::built_in). A model with no list price is unrecorded, never zero.

session_spend answers one loaded session per UTC day and model (harness.v1.sessions.usage), from a moment on when asked. machine_spend answers every Claude Code and Codex session on this machine with use in a window (harness.v1.sessions.spend, supercode sessions usage): a subagent’s use counts on its parent session. It reads only transcripts modified inside the window, and of each only its tail back to the window’s start.

Structs§

CodexUse
Codex’s cumulative token_count totals, read in order: each change is one turn’s use. Codex counts cached prompt tokens inside input_tokens; a use separates them, as Claude’s usage does. The model is the latest turn_context’s.
Spend
Use summed, and what it cost at list rates where its models are priced.
UseRecord
One response’s (Claude Code) or one turn’s (Codex) recorded use.

Functions§

claude_use
The use one Claude Code transcript line records, with its response’s message id: Claude writes a response’s usage on each of its lines, so a reader keeps one per id (the last). A synthetic message records none. cache_creation_input_tokens is split by TTL where Claude records the split (usage.cache_creation); a write the split does not account for is at the default 5-minute TTL.
machine_spend
Every Claude Code and Codex session on this machine with recorded use from since_ms to now, a subagent’s use on its parent. Sorted by sort: cost (the default) puts priced sessions first, most cost first, then the rest by tokens; tokens sorts all by tokens. harnesses limits the reading (empty: both); limit keeps the first rows.
parse_since
A window’s start: a duration back from now_ms (90s, 10m, 1h, 2d) or an RFC 3339 UTC moment.
session_spend
One loaded session’s use per UTC day and model, each record counted from since_ms on (all of it without one).