Architecture
llm-usage-metrics is organized around a small report-command architecture:
- report definitions describe command identity, shared option profile, and examples
- builders parse and normalize local usage data
- feature aggregators reshape that data into report-specific totals or series
- renderers emit terminal, JSON, Markdown, or share artifacts
- a shared report runtime handles output-format resolution, diagnostics, and stdout/stderr behavior
Report definitions and runtime
Section titled “Report definitions and runtime”src/cli/report-definitionsCanonical metadata fordaily,weekly,monthly,efficiency,optimize, andtrendssrc/cli/report-runtimeShared prepare/run lifecycle for format validation, share handling, terminal overflow warnings, and stdout emission
This same metadata is reused for:
- Commander command registration
- generated CLI docs
- root help examples
That keeps command/docs drift low without duplicating option descriptions outside Commander help.
Report flows
Section titled “Report flows”buildUsageData(...) parses source events, applies pricing, aggregates by period/source/model, then renderUsageReport(...) formats the report.
Efficiency
Section titled “Efficiency”buildEfficiencyData(...) reuses usage parsing, attributes events to a repository root, joins them with local Git outcomes, then renderEfficiencyReport(...) formats the result.
Optimize
Section titled “Optimize”buildOptimizeData(...) reuses usage parsing and pricing, aggregates baseline token totals, computes candidate-model counterfactual costs, then renderOptimizeReport(...) formats the result.
Trends
Section titled “Trends”buildTrendsData(...) reuses usage parsing, loads pricing only for cost mode, aggregates to daily totals, projects those totals into daily trend series, then renderTrendsReport(...) formats terminal charts or JSON.
Aggregation and rendering details
Section titled “Aggregation and rendering details”aggregateUsage(...)supports anincludeModelBreakdownprofile Usage keeps per-model breakdowns; efficiency, optimize, and trends skip them.render/unicode-table.tsis driven by explicit row metadata (periodKey,rowKind) This keeps sorting and separators deterministic without coupling the generic table renderer to usage-row domain types.
Module boundaries
Section titled “Module boundaries”src/cli: command creation, report builders, diagnostics emissionsrc/cli/report-definitions: command metadata and shared option profilessrc/cli/report-runtime: shared report lifecyclesrc/sources: adapter discovery and parsingsrc/domain: canonical normalized contractssrc/pricing: LiteLLM pricing loader/cache/cost enginesrc/aggregate: period/source usage aggregationsrc/efficiency: repo attribution and Git outcome joinssrc/optimize: counterfactual pricing aggregationsrc/trends: daily trend-series aggregationsrc/render: output formatters
Invariants
Section titled “Invariants”- deterministic ordering for periods, sources, candidates, and models
- diagnostics on
stderr, report body onstdout - JSON/Markdown stay data-only on
stdout - provider identifiers are normalized at the domain boundary
- incomplete pricing remains explicit in output (
~, warnings, or incomplete flags)