Skip to main content

Module token_usage

Module token_usage 

Source
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Β§

CacheCreation πŸ”’
FileUsageSummary
One session file’s deduped usage, keyed model -> day -> counts. mtime + size drive the incremental cache: an unchanged file is reused rather than re-parsed. folded_project is 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.
RecordMessage πŸ”’
RecordUsage πŸ”’
TokenCounts
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.
WindowAcc πŸ”’
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-DD calendar day in tz for an rfc3339 timestamp, or None when 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 counts at model’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 (see FileUsageSummary::project_resolved).
fold_project_in πŸ”’
Testable core of fold_project. projects_prefix is the encoded <home>/Projects/ string to strip; an empty prefix disables the repo-resolution rule (no known home). projects_root is the real directory the candidate repo names are stat’d against.
home_dir πŸ”’
make_row πŸ”’
parse_date πŸ”’
Parse a YYYY-MM-DD day key into a Date.
parse_file
Parse one session file into its per-file usage summary, bucketing each record by its calendar day in tz. None when 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 the Projects/ prefix). Picks the longest leading run of --joined tokens that is an existing directory under projects_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 /usage overlay drew. Windows are calendar periods relative to now, 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 via pricing (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.