Documentation
¶
Overview ¶
Package ledger holds the cost-accounting readout types and pure aggregation logic carved off the agent Session god-object. The Session holds a *llm.Ledger (the metered gateway's shared record); this package defines the display types (BackendSpend, PurposeSpend) and the functions that turn ledger snapshots into sorted, display-ready slices. The functions take a Reader interface (satisfied by *llm.Ledger) so the ledger package depends only on llm, not on runtime.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CacheStats ¶
CacheStats returns cumulative prompt-cache token counts (read ≈ 10% cost; write = tokens written to the cache). Shown under /debug.
func CacheUtilization ¶ added in v0.56.2
CacheUtilization returns the session-wide cache hit percentage (0-100): CacheRead / (In + CacheRead + CacheWrite).
Types ¶
type BackendSpend ¶
type BackendSpend struct {
Backend string
Calls int
In, Out, CacheRead, CacheWrite int
USD float64
InUSD, OutUSD, CacheReadUSD, CacheWriteUSD float64
AvgLatencyMS int64
Reasons map[string]int
}
BackendSpend is one serving backend's slice of the session ledger (for /cost): who actually ran the calls, how fast, and what it cost at the served model's rate card (every backend is token-billed — the cheap lane + frontier APIs).
func SpendByBackend ¶
func SpendByBackend(r Reader) []BackendSpend
SpendByBackend returns per-backend usage, busiest first. One entry ("anthropic") in a classic session; two once the hybrid router is live.
func (BackendSpend) CacheUtilization ¶ added in v0.56.2
func (b BackendSpend) CacheUtilization() float64
CacheUtilization returns the percentage (0-100) of TotalInput served from cache.
func (BackendSpend) TotalInput ¶ added in v0.56.2
func (b BackendSpend) TotalInput() int
TotalInput returns total input tokens for this backend (In + CacheRead + CacheWrite).
type PersistedCacheSummary ¶ added in v0.56.2
type PersistedCacheSummary struct {
Sessions int
Total llm.Stat
ByProvider map[string]llm.BackendStat
}
PersistedCacheSummary aggregates token and cache utilization across persisted agent_session_finished events from state.db.
func SummarizePersistedCache ¶ added in v0.56.2
func SummarizePersistedCache(evs []store.Event) PersistedCacheSummary
SummarizePersistedCache computes cumulative and per-provider cache utilization from persisted agent_session_finished events (state.db). Legacy events without by_backend attribute their totals to "unknown" (or modelVendor if provided in a preceding agent_session_started event for the same session_id).
func (PersistedCacheSummary) ProviderRows ¶ added in v0.56.2
func (p PersistedCacheSummary) ProviderRows() []BackendSpend
ProviderRows returns ByProvider sorted by total input descending, then name.
type PurposeSpend ¶
type PurposeSpend struct {
Purpose string
Calls int
In, Out, CacheRead, CacheWrite int
USD float64
}
PurposeSpend is one purpose's slice of the session ledger (for /cost --by-purpose).
func SpendByPurpose ¶
func SpendByPurpose(r Reader) []PurposeSpend
SpendByPurpose returns per-purpose usage, most expensive first — so /cost can show where the money actually went (e.g. explore scouts vs the main loop vs synthesis).
func (PurposeSpend) CacheUtilization ¶ added in v0.56.2
func (p PurposeSpend) CacheUtilization() float64
CacheUtilization returns the percentage (0-100) of TotalInput served from cache.
func (PurposeSpend) TotalInput ¶ added in v0.56.2
func (p PurposeSpend) TotalInput() int
TotalInput returns total input tokens for this purpose (In + CacheRead + CacheWrite).
type Reader ¶
type Reader interface {
Total() llm.Stat
ByBackend() map[string]llm.BackendStat
ByPurpose() map[llm.Purpose]llm.Stat
}
Reader is the subset of *llm.Ledger the aggregation functions need. *llm.Ledger satisfies it; tests can substitute a fake. The ledger package depends on llm for Stat/BackendStat/Purpose, but never on runtime — so the import direction stays runtime → ledger → llm (a clean leaf below the hub).