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
- func Dollars(micro int64) string
- func InputPrice(c adapter.ModelCost) (int64, bool)
- func Path() (string, error)
- func Range(lo, hi Spend) string
- func ReadRatio(model string) int64
- func Tokens(n int64) string
- type Cache
- func (c *Cache) Input(agent string, n int64) Spend
- func (c *Cache) Learn(agent string, r adapter.Result, at time.Time)
- func (c *Cache) Price(model string) (Price, bool)
- func (c *Cache) RatioRange() Ratios
- func (c *Cache) RecordRatio(recovered, sources, proposed, proposeRaw int64, at time.Time)
- func (c *Cache) Reference(agent string) (string, Price, bool)
- func (c *Cache) Save() error
- func (c *Cache) Weighted(agent string, r adapter.Result) Spend
- type Count
- type Default
- type Meter
- type Price
- type Ratio
- type Ratios
- type Spend
Constants ¶
const ( DefaultRecoverRatio = 12_500 DefaultProposeRatio = 2_050 )
The defaults init estimates with before two runs are 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.
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 ¶
Dollars is micro, in millionths of a dollar, shown as an estimate: whole dollars from $10, cents below.
func InputPrice ¶
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 ¶
Path is where the machine keeps its spend cache: File in the settings directory configdir gives.
func Range ¶
Range reads like "1.9M to 5.6M tokens (about $7.60 to $23)", or without the cost when either end is Unpriced.
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 ¶
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 ¶
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 ¶
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) RatioRange ¶
RatioRange is the range init estimates with: the smallest and the largest of each recorded ratio once two runs are recorded; else the recovery default × 0.5 to × 1.5 and propose's default factor.
func (*Cache) RecordRatio ¶
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 ¶
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 ¶
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 ¶
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 Default ¶
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) Name ¶
Name is the adapter's name; empty for a meter with no adapter, as when the agent could not be resolved.
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 ¶
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.