Skip to main content

Module perf

Module perf 

Source
Expand description

Per-phase latency + token/cost instrumentation.

Why: Tracks how long each workflow phase takes, how many tokens it consumed, and the resulting USD cost so runs can be compared build-over-build. What: TokenUsage, PhaseRecord, PerfRecord, PerfTotals, PerfCollector, cost_usd. Test: perf::tests::*. Performance instrumentation — per-phase latency + token/cost capture.

Why: (#47) We need to track how long each workflow phase takes, how many tokens it consumed (prompt / completion / Anthropic cache read / cache creation), and the resulting USD cost so we can compare runs build-over- build and catch regressions or prompt-caching wins. Persisting one JSON file per run under docs/performance/runs/ plus a one-line summary log keeps the data greppable without a database. What: PerfCollector is constructed at workflow start, record_phase appends a PhaseRecord after each phase, and flush writes the final JSON + log line. TokenUsage is the provider-agnostic shape captured from each LLM call (extended with Anthropic-specific cache_read / cache_creation fields which are zero for non-Anthropic models). Test: cost_usd_known_model, collector_records_phases, collector_totals_sum_phases, collector_flush_writes_json_and_log.

Module layout (see #366 split):

  • mod.rs — perf record types + PerfCollector lifecycle
  • pricing.rs — cost_usd + the model pricing table + formatters
  • tests.rs — unit tests

Structs§

PerfCollector
Accumulator used during workflow execution.
PerfRecord
Full performance record persisted to docs/performance/runs/.
PerfTotals
Totals rolled up from every PhaseRecord in a run.
PhaseRecord
One phase’s measured performance.
TokenUsage
Token usage captured from a single LLM round-trip.

Functions§

cost_usd
Compute USD cost for a single LLM call given the model name and token counts. Unknown models fall back to Sonnet-class pricing.