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"`
}
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.