Documentation
¶
Overview ¶
Package cost turns token usage into dollars using the versioned pricing table, and computes the cache-savings math ported from Caprock-python (_savings.py).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Model ¶
type Model struct {
ID string `json:"id"`
Display string `json:"display"`
Input float64 `json:"input"`
CacheWrite5m float64 `json:"cache_write_5m"`
CacheWrite1h float64 `json:"cache_write_1h"`
CacheRead float64 `json:"cache_read"`
Output float64 `json:"output"`
ContextWindow int64 `json:"context_window"`
// Until is the last date this row's prices applied, "YYYY-MM-DD"
// inclusive, in UTC. Empty means "still current" — almost every row.
//
// A vendor's introductory price is a real price for the turns that ran
// while it was live, and a different real price afterwards. Sonnet 5
// launched at $2/$10 and reverts to $3/$15 on 2026-08-31; overwriting the
// figure on that date would silently restate every August turn at a price
// nobody was charged, which is rule 6's "no invented numbers" applied to
// our own history. So a superseded price stays in the table with the date
// it stopped applying, and Lookup picks the row that was in force when the
// turn happened.
//
// Rows for one model id are ordered oldest-first in the JSON; the current
// row is the one with no Until.
Until string `json:"until,omitempty"`
}
Model is one row of the pricing table. Prices are USD per million tokens.
type Savings ¶
type Savings struct {
BilledWith float64 `json:"billed_with"`
BilledWithout float64 `json:"billed_without"`
Saved float64 `json:"saved"`
// HitRate = cache_read / (in + cache_read + cache_write); 0 when no input.
HitRate float64 `json:"hit_rate"`
// CutPct = saved / billed_without × 100; 0 when nothing was billed.
CutPct float64 `json:"cut_pct"`
}
Savings is the cache-savings math from Caprock-python, in input-token equivalents.
billed_with = in + 1.25·cache_write + 0.10·cache_read billed_without = in + cache_write + cache_read (all fresh input) saved = billed_without − billed_with
func ComputeSavings ¶
ComputeSavings applies the formula to raw token counts.
type Table ¶
type Table struct {
Version string `json:"version"`
Source string `json:"source"`
FetchedAt string `json:"fetched_at"`
Currency string `json:"currency"`
Unit string `json:"unit"`
Notes []string `json:"notes"`
Models []Model `json:"models"`
// UserOverride is true when the table came from <data_dir>/pricing.json.
UserOverride bool `json:"user_override,omitempty"`
}
Table is the parsed pricing table.
func Load ¶
Load returns the user override at overridePath if it exists and parses, else the embedded table. A broken override is an error (never silently ignored — a wrong price file must not masquerade as the default).
func (*Table) Lookup ¶
Lookup finds the pricing row for a model id observed in a transcript (e.g. "claude-opus-5", "claude-sonnet-4-5-20250929", "us.anthropic.claude-sonnet-4-5-20250929-v1:0"). Matching is by longest id prefix after stripping known provider prefixes. ok is false when nothing matches — the caller must then leave cost nil, never zero.
func (*Table) LookupAt ¶ added in v0.35.0
LookupAt is Lookup for a turn that happened at a particular instant, which is what pricing a historical turn requires.
Prices change, and a change is not retroactive: turns that ran under an introductory price were charged that price forever, and turns after it were not. A table with one row per model can only express "the price now", so restating history is the unavoidable side effect of every price change — August's spend would silently grow on the morning Sonnet 5's introductory rate expired.
So a model may have several rows, oldest first, each carrying the date its price stopped applying (`until`, inclusive, UTC). This returns the first row whose window contains `at`. A zero `at` means "price it as of now", which is what Lookup does and what anything without a timestamp gets.
Matching is still by longest id prefix after normalisation; the date only chooses between rows that already matched.
func (*Table) Price ¶
Price returns the USD cost of a token delta for a model, at today's prices. ok is false when the model is unknown to the table.