Expand description
Token/cost accounting for the usage subject.
pricing is the LiteLLM-sourced per-model USD table used to turn
JSONL token counts into a notional cost. This root holds the
project-folding that maps a ~/.claude/projects/<slug> directory
name back to the repo it belongs to.
Re-exportsΒ§
use std::collections::BTreeMap;πuse std::collections::HashSet;πuse std::io::BufRead;πuse std::path::Path;πuse std::path::PathBuf;πuse std::time::SystemTime;πuse forge_primitives::token_usage::UsageReport;πuse forge_primitives::token_usage::UsageRow;πuse forge_primitives::token_usage::WindowUsage;πuse serde::Deserialize;πuse serde::Deserialize;πuse serde::Serialize;πuse serde::Serialize;πuse time::format_description::well_known::Rfc3339;πuse time::Date;πuse time::Duration;πuse time::Month;πuse time::OffsetDateTime;πuse time_tz::OffsetDateTimeExt;πuse time_tz::Tz;πuse self::pricing::PricingTable;π
ModulesΒ§
- pricing
- LiteLLM-sourced per-model pricing: the parser plus the proxy-aware fetch that populates the runtime cache.
StructsΒ§
- Cache
Creation π - File
Usage Summary - One session fileβs deduped usage, keyed
model -> day -> counts.mtime+sizedrive the incremental cache: an unchanged file is reused rather than re-parsed.folded_projectis the repo the file belongs to, folded from its~/.claude/projects/<slug>dir name. - Record π
- Minimal view of a transcript record: only the fields usage accounting reads. Unknown fields (content, tool blocks, β¦) are ignored by serde.
- Record
Message π - Record
Usage π - Token
Counts - The five-way token split accumulated for one
(model, day)bucket. Cache-write is kept split by TTL tier so pricing can apply the 1h / 5m rates independently. - Window
Acc π - Per-window accumulator keyed
project -> model -> counts. Keeping the model split under each project lets the by-project rows sum cost across a projectβs differently-priced models.
ConstantsΒ§
- WORKTREE_
MARKER π - Encoded form of
/.claude/worktrees/in a project slug: Claude Code maps both/and.to-, so a worktree path folds to<parent>--claude-worktrees-<name>.
FunctionsΒ§
- basename_
fallback π - The trailing
--separated component of a slug, used when no richer rule applies. - calendar_
day π - The
YYYY-MM-DDcalendar day intzfor an rfc3339 timestamp, orNonewhen it doesnβt parse. DST-correct: the offset is resolved at the instant, so a late-evening UTC time lands on the next local day. - cost_of π
- Notional cost of
countsatmodelβs price; 0 for an unpriced or<synthetic>model. - encoded_
projects_ πprefix - The slug prefix for paths under
<projects_root>: the root path with/and.mapped to-, plus the trailing separatorβs-. - fold_
project π - Fold a
~/.claude/projects/<slug>directory name to the display project it belongs to, plus whether that label is final (seeFileUsageSummary::project_resolved). - fold_
project_ πin - Testable core of
fold_project.projects_prefixis the encoded<home>/Projects/string to strip; an empty prefix disables the repo-resolution rule (no known home).projects_rootis the real directory the candidate repo names are statβd against. - home_
dir π - make_
row π - parse_
date π - Parse a
YYYY-MM-DDday key into aDate. - parse_
file - Parse one session file into its per-file usage summary, bucketing
each record by its calendar day in
tz.Nonewhen the file canβt be statβd or opened; a malformed line is skipped. - resolve_
project_ πname - Resolve the repo name from a slug remainder (the encoded
<name>/<subpath...>after theProjects/prefix). Picks the longest leading run of--joined tokens that is an existing directory underprojects_root; when nothing resolves (the repo is not checked out) the whole run is the best-effort label. - roll_up
- Roll per-file summaries up into the four windows the
/usageoverlay drew. Windows are calendar periods relative tonow, in the same local timezone the day buckets already use: today, the current week (from Monday), the current month (from the 1st), and all-time. Each window carries both groupings and a total row, priced viapricing(an unpriced model contributes 0 cost). - sort_
by_ πcost_ desc - usage_
files - Every real session file under
projects_dir/<slug>/. Syncthing conflict copies (*.sync-conflict-*.jsonl) are skipped: they are stale duplicates of a real session and would double-count usage that per-file message-id dedup canβt catch across files.