{"modulePath":"github.com/memcode-ai/memcode","version":"v0.56.16","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package ledger\n\nPackage 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.\n\n## Functions\n\n```go\nfunc CacheStats(r Reader) (read, write int)\n```\nCacheStats returns cumulative prompt-cache token counts (read ≈ 10% cost; write = tokens written to the cache). Shown under /debug.\n\n```go\nfunc CacheUtilization(r Reader) float64\n```\nCacheUtilization returns the session-wide cache hit percentage (0-100): CacheRead / (In + CacheRead + CacheWrite).\n\n```go\nfunc Spend(r Reader) (in, out, cacheRead, cacheWrite int, usd float64)\n```\nSpend returns the session's token breakdown and estimated cost in USD (priced per response under each call's model; rates are approximate). Powers /cost.\n\n```go\nfunc Tokens(r Reader) (in, out int)\n```\nTokens returns cumulative session token flow: total input the model saw (uncached + cache reads + cache writes) and total output. Footer \"↑in ↓out\".\n\n## Types\n\n```go\ntype BackendSpend struct {\n\tBackend                                    string\n\tCalls                                      int\n\tIn, Out, CacheRead, CacheWrite             int\n\tUSD                                        float64\n\tInUSD, OutUSD, CacheReadUSD, CacheWriteUSD float64\n\tAvgLatencyMS                               int64\n\tReasons                                    map[string]int\n}\n```\nBackendSpend 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).\n\n```go\nfunc SpendByBackend(r Reader) []BackendSpend\n```\nSpendByBackend returns per-backend usage, busiest first. One entry (\"anthropic\") in a classic session; two once the hybrid router is live.\n\n```go\nfunc (b BackendSpend) CacheUtilization() float64\n```\nCacheUtilization returns the percentage (0-100) of TotalInput served from cache.\n\n```go\nfunc (b BackendSpend) TotalInput() int\n```\nTotalInput returns total input tokens for this backend (In + CacheRead + CacheWrite).\n\n```go\ntype PersistedCacheSummary struct {\n\tSessions   int\n\tTotal      llm.Stat\n\tByProvider map[string]llm.BackendStat\n}\n```\nPersistedCacheSummary aggregates token and cache utilization across persisted agent\\_session\\_finished events from state.db.\n\n```go\nfunc SummarizePersistedCache(evs []store.Event) PersistedCacheSummary\n```\nSummarizePersistedCache 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).\n\n```go\nfunc (p PersistedCacheSummary) ProviderRows() []BackendSpend\n```\nProviderRows returns ByProvider sorted by total input descending, then name.\n\n```go\ntype PurposeSpend struct {\n\tPurpose                        string\n\tCalls                          int\n\tIn, Out, CacheRead, CacheWrite int\n\tUSD                            float64\n}\n```\nPurposeSpend is one purpose's slice of the session ledger (for /cost --by-purpose).\n\n```go\nfunc SpendByPurpose(r Reader) []PurposeSpend\n```\nSpendByPurpose 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).\n\n```go\nfunc (p PurposeSpend) CacheUtilization() float64\n```\nCacheUtilization returns the percentage (0-100) of TotalInput served from cache.\n\n```go\nfunc (p PurposeSpend) TotalInput() int\n```\nTotalInput returns total input tokens for this purpose (In + CacheRead + CacheWrite).\n\n```go\ntype Reader interface {\n\tTotal() llm.Stat\n\tByBackend() map[string]llm.BackendStat\n\tByPurpose() map[llm.Purpose]llm.Stat\n}\n```\nReader 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).\n\n","path":"github.com/memcode-ai/memcode/internal/agent/ledger","name":"ledger","synopsis":"Package ledger holds the cost-accounting readout types and pure aggregation logic carved off the agent Session god-object.","isRedistributable":true}
