cost

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: Apache-2.0 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 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

func ComputeSavings(in, cacheRead, cacheWrite int64) Savings

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 Embedded

func Embedded() (*Table, error)

Embedded returns the table compiled into the binary.

func Load

func Load(overridePath string) (*Table, error)

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 Parse

func Parse(b []byte) (*Table, error)

Parse decodes a pricing table and validates it.

func (*Table) Lookup

func (t *Table) Lookup(model string) (Model, bool)

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

func (t *Table) Price(model string, d event.TokenDelta) (float64, bool)

Price returns the USD cost of a token delta for a model. ok is false when the model is unknown to the table.

Jump to

Keyboard shortcuts

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