spend

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 13 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 spend weighs what sessions spend (design 10 §1): each kind of token counts by its price relative to input, and one weighted token is worth the input price of the adapter's default model. Dollars are the measure: the adapter reports what each session cost, and spend backs each model's input price out of it with the published ratios between token kinds, keeping it machine-wide, newest wins. A model with no price yet counts at raw tokens, with no cost shown. Spend reads in weighted tokens first, with the cost estimate beside it: "2.1M tokens (about $28)". A Meter weighs an adapter's sessions as they end, and the cache also keeps the ratio init estimates recovery with, measured from its finished runs.

Index

Constants

View Source
const (
	DefaultRecoverRatio = 12_500
	DefaultProposeRatio = 2_050
)

The defaults init estimates with before a run is recorded, in thousandths, measured in weighted tokens on nebari's run 9 (design 10 §3): recovery spent 12.5M weighted tokens for sources estimated at 998k, about 12.5 times, and propose 1.4M weighted for 682,510 raw, about 2.05 times, since a one-turn session writes its whole prompt to a 1-hour cache at twice input. The range around the recovery ratio is ×0.5 to ×1.5.

View Source
const File = "spend.json"

File is the name of the machine's spend cache in the settings directory.

Variables

This section is empty.

Functions

func Dollars

func Dollars(micro int64) string

Dollars is micro, in millionths of a dollar, shown as an estimate: whole dollars from $10, cents below.

func InputPrice

func InputPrice(c adapter.ModelCost) (int64, bool)

InputPrice backs c's model's input price out of what c cost: dollars ÷ its tokens weighed by the ratios, in millionths of a dollar per million input tokens. It reports false when c carries no dollars or no tokens.

func Path

func Path() (string, error)

Path is where the machine keeps its spend cache: File in the settings directory configdir gives.

func Range

func Range(lo, hi Spend) string

Range reads like "1.9M to 5.6M tokens (about $7.60 to $23)", or without the cost when either end is Unpriced.

func ReadRatio

func ReadRatio(model string) int64

ReadRatio is what a cache read of model costs, in thousandths of the model's input price: 50 for Claude Opus 5.5 and Claude Sonnet 5.5, 25 for Claude Fable 5.x and Claude Mythos 5.x, and 100 for any other model.

func Tokens

func Tokens(n int64) string

Tokens is n shortened: "980", "7.4k", "74k", "2.1M".

Types

type Cache

type Cache struct {
	Models   map[string]Price   `json:"models"`
	Defaults map[string]Default `json:"defaults"`
	// Ratios are what finished init runs measured in weighted tokens
	// (RatioRange). The older "init_ratios", measured in raw tokens, is
	// ignored and dropped on save.
	Ratios []Ratio `json:"init_weighted_ratios,omitempty"`
	// contains filtered or unexported fields
}

Cache is what the machine knows of prices: each model's input price and each adapter's default model, each the newest learned, and what init runs measured. It is safe for concurrent use.

func Open

func Open(path string) *Cache

Open reads the cache kept at path; an empty path keeps one in memory only. A file that is missing or cannot be read is taken as empty, since it is a cache, and Save replaces it.

func (*Cache) Input

func (c *Cache) Input(agent string, n int64) Spend

Input is n input tokens of the default model of the adapter named agent, weighed: n weighted tokens, at that model's input price when it has one, else Unpriced.

func (*Cache) Learn

func (c *Cache) Learn(agent string, r adapter.Result, at time.Time)

Learn keeps what r, a session of the adapter named agent that ended at at, says of prices: the input price of each model whose cost it carries, backed out of its dollars, and its model as the adapter's default. An entry newer than at is kept.

func (*Cache) Price

func (c *Cache) Price(model string) (Price, bool)

Price is model's cached input price, if any.

func (*Cache) RatioRange

func (c *Cache) RatioRange() Ratios

RatioRange is the range init estimates with: the smallest and the largest of each recorded ratio once two runs are recorded; with one, its recovery ratio × 0.5 to × 1.5 and its propose factor, since one run measured on this machine is nearer than nebari's; with none, the recovery default × 0.5 to × 1.5 and propose's default factor.

func (*Cache) RecordRatio

func (c *Cache) RecordRatio(recovered, sources, proposed, proposeRaw int64, at time.Time)

RecordRatio keeps what a finished init run measured from its weighted spend: recovery's weighted tokens against the sources' estimated tokens, and propose's weighted tokens against its raw ones. A run missing any of them is not recorded.

func (*Cache) Reference

func (c *Cache) Reference(agent string) (string, Price, bool)

Reference is the model one weighted token of the adapter named agent is an input token of, its default model, and that model's input price, if any is cached.

func (*Cache) Save

func (c *Cache) Save() error

Save writes the cache to its file, merged with what the file holds now so that another run's newer entries survive, newest wins. A cache with no path is not written.

func (*Cache) Weighted

func (c *Cache) Weighted(agent string, r adapter.Result) Spend

Weighted is what r, a session of the adapter named agent, spent in weighted tokens: each model's dollars ÷ the input price of the adapter's default model, or, for a model that reported no dollars, its tokens by kind at its cached price. A model with no price, and every model while the default model has none, counts at raw tokens and leaves the Spend Unpriced.

type Count

type Count struct {
	Spend Spend
	Raw   adapter.Tokens
}

Count is what a meter has counted: its sessions' spend, weighed, and their raw tokens.

func (Count) Since

func (c Count) Since(o Count) Count

Since is what c counted beyond o, an earlier count of the same meter.

type Default

type Default struct {
	Model string    `json:"model"`
	At    time.Time `json:"at"`
}

Default is an adapter's default model: the one its sessions ran on, and when one last did.

type Meter

type Meter struct {
	adapter.Adapter
	// Cache holds the prices sessions are weighed by, and learns theirs.
	Cache *Cache
	// Now dates what is learned; nil means time.Now.
	Now func() time.Time
	// Ended, when set, is called after each session ends with all the meter
	// has counted, outside any lock.
	Ended func(total Spend)
	// contains filtered or unexported fields
}

Meter is an adapter whose sessions are weighed as each ends: the prices each session's cost shows are learned into Cache, and its spend is added to what the meter has counted. It never stops a session.

func (*Meter) Count

func (m *Meter) Count() Count

Count is all the meter has counted so far; a nil meter has counted nothing.

func (*Meter) Name

func (m *Meter) Name() string

Name is the adapter's name; empty for a meter with no adapter, as when the agent could not be resolved.

func (*Meter) Spent

func (m *Meter) Spent() Spend

Spent is all the meter has counted so far.

func (*Meter) Start

func (m *Meter) Start(ctx context.Context, s adapter.Spec) (adapter.Session, error)

Start starts s on the meter's adapter, weighing the session when it ends.

func (*Meter) Window

func (m *Meter) Window() int

Window is the window the meter's adapter states, 0 when it states none or there is none.

type Price

type Price struct {
	PerMillion int64     `json:"micro_usd_per_million_input"`
	At         time.Time `json:"at"`
}

Price is a model's input price, in millionths of a dollar per million input tokens, and when it was worked out.

type Ratio

type Ratio struct {
	Recover int64     `json:"recover_milli"`
	Propose int64     `json:"propose_milli"`
	At      time.Time `json:"at"`
}

Ratio is what one finished init run measured, in thousandths: Recover is recovery's weighted tokens ÷ the gathered sources' estimated tokens, and Propose is propose's weighted tokens ÷ its raw tokens.

type Ratios

type Ratios struct {
	RecoverLo, RecoverHi int64
	ProposeLo, ProposeHi int64
	Measured             int
}

Ratios is the range init estimates with: the recovery ratio's and propose's factor's, in thousandths, from Measured runs (0 for the defaults).

type Spend

type Spend struct {
	Tokens   int64 `json:"tokens"`
	MicroUSD int64 `json:"micro_usd"`
	Unpriced bool  `json:"unpriced,omitempty"`
}

Spend is spend weighed: Tokens weighted tokens and MicroUSD their cost estimate in millionths of a dollar. Unpriced says some of it counted at raw tokens, for want of a price, so no cost is shown for it.

func (Spend) Add

func (s Spend) Add(o Spend) Spend

Add is s and o together.

func (Spend) Cost

func (s Spend) Cost() string

Cost is the cost estimate: "about $28", "under $0.01", or "none: no price yet" when Unpriced.

func (Spend) String

func (s Spend) String() string

String reads like "2.1M tokens (about $28)", or "2.1M tokens" when Unpriced.

Jump to

Keyboard shortcuts

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