usage

package
v0.33.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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

func CostUSD added in v0.5.2

func CostUSD(model string, prompt, completion int, overrides map[string]Price) float64

CostUSD estimates the dollar cost of prompt+completion tokens for a model.

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.

func PriceFor added in v0.5.2

func PriceFor(model string, overrides map[string]Price) Price

PriceFor returns the price for a model id: overrides first (longest substring match), then DefaultPrices, then zero (unknown / free). A ":free" id is $0.

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

func Open(path string) (*Store, error)

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

func (s *Store) Add(date, provider, model string, prompt, completion int)

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) ByDay

func (s *Store) ByDay() []Total

ByDay returns per-day totals, most recent day first.

func (*Store) ByModel

func (s *Store) ByModel() []Total

ByModel returns per provider/model totals, largest first.

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

func (s *Store) CostSince(cutoff string, overrides map[string]Price) float64

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

func (s *Store) Purge(cutoff string) int

Purge drops entries older than cutoff (Date < cutoff) and returns how many were removed. The caller persists with Save.

func (*Store) Save

func (s *Store) Save() error

Save writes the ledger (no-op for an in-memory store). The lock is held across the write, and the write is atomic (temp + rename), so concurrent Saves from parallel sub-agents can't interleave, lose an update, or truncate the file.

func (*Store) Total added in v0.5.0

func (s *Store) Total() Total

Total is the all-time total.

func (*Store) TotalSince added in v0.5.0

func (s *Store) TotalSince(cutoff string) Total

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
}

Total is an aggregated row for display.

func (Total) Tokens

func (t Total) Tokens() int

Tokens is the combined prompt+completion count.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL