cost.go RawHistoryFolder
1package core
2
3import "github.com/bnema/zut/packages/provider"
4
5// CostTracker accumulates usage across turns in a session.
6//
7// Total is the cumulative usage shown in the status bar's "$x.xx"
8// readout. LastTurn is the per-turn usage of the most recent
9// completed turn; the TUI uses LastTurn.InputTokens+cache as a
10// proxy for "current context size" so the X%/Ymax gauge tracks the
11// prompt size that just went to the model.
12type CostTracker struct {
13 Total provider.Usage
14 LastTurn provider.Usage
15 hasTotal bool
16}
17
18// Add folds u into the running total, records u as the last-turn
19// snapshot, and returns the new cumulative value.
20func (c *CostTracker) Add(u provider.Usage) provider.Usage {
21 if c.hasTotal {
22 c.Total = c.Total.Add(u)
23 } else {
24 c.Total = u
25 c.hasTotal = true
26 }
27 c.LastTurn = u
28 return c.Total
29}
30
31// Seed replaces the running total with usage restored from a session.
32func (c *CostTracker) Seed(u provider.Usage) {
33 c.Total = u
34 c.hasTotal = u.InputTokens != 0 || u.OutputTokens != 0 || u.CacheReadTokens != 0 || u.CacheWriteTokens != 0 || u.CostUSD != 0
35}