Documentation
¶
Overview ¶
Package usage is a small persistent token-usage ledger: prompt/completion tokens spent, bucketed by day, provider, and model. It lets /usage show a history beyond the current session.
Index ¶
- Variables
- func CostUSD(model string, prompt, completion int, overrides map[string]Price) float64
- type Entry
- type Price
- type Store
- func (s *Store) Add(date, provider, model string, prompt, completion int, dur time.Duration)
- func (s *Store) ByDay() []Total
- func (s *Store) ByModel() []Total
- func (s *Store) Clear()
- func (s *Store) CostSince(cutoff string, overrides map[string]Price) float64
- func (s *Store) Purge(cutoff string) int
- func (s *Store) Save() error
- func (s *Store) Total() Total
- func (s *Store) TotalSince(cutoff string) Total
- type Total
Constants ¶
This section is empty.
Variables ¶
var DefaultPrices = map[string]Price{
"gpt-4o-mini": {0.15, 0.60},
"gpt-4o": {2.50, 10.00},
"chatgpt-4o-latest": {5.00, 15.00},
"claude-3-5-haiku": {0.80, 4.00},
"claude-3-5-sonnet": {3.00, 15.00},
"claude-3-7-sonnet": {3.00, 15.00},
"claude-3-opus": {15.00, 75.00},
"sonnet": {3.00, 15.00},
"haiku": {0.80, 4.00},
"opus": {15.00, 75.00},
"grok-2": {2.00, 10.00},
"grok-4": {3.00, 15.00},
"llama-3.3-70b": {0.59, 0.79},
"llama-3.1-8b": {0.05, 0.08},
}
DefaultPrices are rough list prices (USD / 1M tokens) for common models, matched by the longest substring of the model id. They drift over time and are approximate — /usage labels the figure "est." and a `prices` config map overrides any entry. A ":free" model (OpenRouter) is always $0.
Functions ¶
Types ¶
type Entry ¶
type Entry struct {
Date string `json:"date"` // YYYY-MM-DD
Provider string `json:"provider"`
Model string `json:"model"`
Prompt int `json:"prompt"`
Completion int `json:"completion"`
DurationMS int64 `json:"duration_ms,omitempty"`
}
Entry is one (day, provider, model) bucket of token counts. DurationMS is wall-clock time spent generating them (the run that produced Completion), summed across every call folded into this bucket — a rough per-bucket generation-speed denominator, not pure model-only time (a run can include tool-call round-trips). Omitted (0) for entries recorded before this field existed, or if a caller doesn't have a duration to report.
type Price ¶ added in v0.5.2
type Price struct{ In, Out float64 }
Price is a model's cost in USD per MILLION tokens, split by direction.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the ledger, persisted to a JSON file (in-memory if path is ""). Safe for concurrent use (sub-agents record from parallel tool calls).
func Open ¶
Open loads the ledger, or starts empty if the file is absent. A blank path yields an in-memory store that never persists.
func (*Store) Add ¶
Add folds tokens into the (date, provider, model) bucket. A no-op for a non-positive delta so a turn that reported no usage doesn't create a row. dur is the wall-clock time the run that produced these tokens took (0 if unknown) — see Entry.DurationMS.
func (*Store) Clear ¶ added in v0.5.0
func (s *Store) Clear()
Clear drops the entire ledger. The caller persists with Save.
func (*Store) CostSince ¶ added in v0.5.2
CostSince sums the estimated cost of all entries on or after cutoff, pricing each by its own model. cutoff "" totals everything.
func (*Store) Purge ¶ added in v0.5.0
Purge drops entries older than cutoff (Date < cutoff) and returns how many were removed. The caller persists with Save.
func (*Store) Save ¶
Save writes the ledger (no-op for an in-memory store). Before writing, it re-reads the file and replays this Store's own pending changes onto that fresh copy instead of blindly overwriting it with entries — otherwise two separate ipsupport-code processes sharing the same global usage store could have one's update silently lost to the other's last write (the mutex only protects against races WITHIN one process). With nothing pending there is nothing of this Store's own to persist, so Save skips the write entirely instead of falling through to an unconditional write of s.entries: that write would have no fresh read backing it, so a stale in-memory snapshot (e.g. after an idle flush, or a no-op Purge, with no Add in between) could silently clobber a fresher file a different process wrote since this one last read it. A file lock around the whole read-merge-write cycle keeps two such processes from interleaving (one's read landing before the other's write, so each only ever merges its own delta) the same way the in-process mutex keeps two goroutines from interleaving. The write itself is atomic (temp + rename) so a crash mid-write can't truncate the file either. pending is only cleared once that write actually succeeds — if it fails, they're left intact so the next Save replays them instead of silently losing them (a concurrent Add's own Save wouldn't otherwise know to replay a delta it never recorded).
func (*Store) TotalSince ¶ added in v0.5.0
TotalSince sums all entries on or after cutoff (an ISO YYYY-MM-DD date; ISO dates compare correctly as strings). cutoff "" sums everything.
type Total ¶
type Total struct {
Key string // a date, or "provider/model"
Prompt int
Completion int
DurationMS int64
}
Total is an aggregated row for display.
func (Total) TokensPerSec ¶ added in v0.42.91
TokensPerSec is the completion-token generation rate over DurationMS, or 0 if no duration was recorded for this bucket (e.g. entries from before Entry.DurationMS existed).