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)
- 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"`
}
Entry is one (day, provider, model) bucket of token counts.
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.
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 Add() deltas 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). Purge/Clear set overwrite, skipping the merge: those are a deliberate replace of the whole ledger. The write itself is atomic (temp + rename) so a crash mid-write can't truncate the file either.
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.