Documentation
¶
Overview ¶
Package pricing holds the pricers that ship with the platform (§4).
A Pricer turns one model call into money. The runtime calls it after every call, before the usage row is stored, so cost lives on the row next to the tokens instead of being worked out by whoever reads it later.
Three cover almost everyone:
Table — a price list, keyed by model. The common case. Receipt — read the cost the provider already computed and sent back. Chain — try several in order; the first that answers wins.
Anything else is a ports.Pricer of your own, or ports.PricerFunc around a plain function.
Index ¶
- Constants
- func Amount(micros int64) float64
- func Chain(pricers ...ports.Pricer) ports.Pricer
- func Fixed(micros int64, currency string) ports.Pricer
- func Format(micros int64, currency string) string
- func Micros(amount float64) int64
- func Receipt(read ReceiptReader) ports.Pricer
- func ReceiptIn(currency string, read ReceiptReader) ports.Pricer
- type ModelPrice
- type ReceiptReader
- type Table
- type ToolPrice
- type Tools
Constants ¶
const USD = "USD"
USD is the currency the shipped pricers use unless told otherwise.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
Chain tries each pricer in order and takes the first answer. Put the accurate one first and the fallback last:
pricing.Chain(
pricing.Receipt(gatewayReceiptMicros), // the real figure, when sent
table, // otherwise the price list
)
A pricer that errors is skipped rather than believed. If none answers, Chain returns the first error it saw, or nil, nil when they simply had nothing to say.
func Fixed ¶
Fixed prices every call the same, whatever the model. Useful for a credit system that charges per call rather than per token, and for tests.
func Format ¶
Format renders micros for a human: "0.2500 USD". Money for display only; never do arithmetic on the string.
func Receipt ¶
func Receipt(read ReceiptReader) ports.Pricer
Receipt prices a call from the number the provider already computed, in USD. This is the most accurate pricer there is: no price list to keep in step with a provider's changes, and discounts and gateway markups are already inside the figure.
Types ¶
type ModelPrice ¶
type ModelPrice struct {
InputPerMillion float64
CacheReadPerMillion float64
CacheWritePerMillion float64
OutputPerMillion float64
// ReasoningPerMillion is usually 0. Most providers already count
// reasoning tokens inside the output count, so charging them again here
// bills the same tokens twice. Set it only when your provider reports
// reasoning tokens SEPARATELY from output.
ReasoningPerMillion float64
}
ModelPrice is what one model costs, per MILLION tokens, in the table's currency. A price of 3.0 for input means $3.00 per million input tokens, which is how every provider publishes them, so a price list can be typed straight off their pricing page.
A cache rate left at zero is taken as not given: those tokens are priced at InputPerMillion, and a warning is logged once per model. Cache tokens are never free, and a price list that forgot them must not bill them at 0.
type ReceiptReader ¶
ReceiptReader pulls a cost out of what the provider attached to the finish: a gateway receipt, a billing header, whatever your provider sends. It returns false when this call carried no receipt, and the next pricer in a Chain gets its turn.
type Table ¶
type Table map[string]ModelPrice
Table is a price list keyed by model (§4): the registry key a run asked for ("claude-sonnet-4@high"), or the wire id the provider reported back ("claude-sonnet-4-20250514"). Both work; a lookup tries them in that order, then the registry key with any "@variant" suffix removed.
A model the table does not know is not priced. Its row is stored with no cost and UsageTotals.Unpriced counts it, so a missing price shows up as a gap in the bill rather than as a silent zero.
func (Table) In ¶
In returns the same table priced in another currency. The numbers are not converted: it only changes the code that goes onto every Cost, for a table whose prices are already in that currency.
type ToolPrice ¶ added in v0.5.0
ToolPrice is what a paid tool service costs, in currency units: per use (a search, a page), per second of sandbox time (bash, code_execution), or both.
type Tools ¶ added in v0.5.0
Tools prices the built-in tools' usage rows (Kind "tool", Model "tool:<name>"), per use, keyed by adapter name ("brave", "jina-search") or by tool name ("web_search"). The adapter wins: the same tool costs what the service behind it charges. Every other row is left to the next pricer, so chain it with the model table:
Pricer: pricing.Chain(modelPrices, pricing.Tools{"brave": {PerUse: 0.005}})
Sandbox tools are priced by the sandbox adapter's name and the seconds their commands ran: pricing.Tools{"e2b": {PerSecond: 0.000028}}.
A tool's own model calls (reading a page with a question) carry the model's key, so the model table prices them.