pricing

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 7 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 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

View Source
const USD = "USD"

USD is the currency the shipped pricers use unless told otherwise.

Variables

This section is empty.

Functions

func Amount

func Amount(micros int64) float64

Amount converts micros back to a currency amount: Amount(250_000) is 0.25.

func Chain

func Chain(pricers ...ports.Pricer) ports.Pricer

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

func Fixed(micros int64, currency string) ports.Pricer

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

func Format(micros int64, currency string) string

Format renders micros for a human: "0.2500 USD". Money for display only; never do arithmetic on the string.

func Micros

func Micros(amount float64) int64

Micros converts a currency amount to micros: Micros(0.25) is 250_000.

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.

func ReceiptIn

func ReceiptIn(currency string, read ReceiptReader) ports.Pricer

ReceiptIn is Receipt in another currency.

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

type ReceiptReader func(meta map[string]any) (micros int64, ok bool)

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

func (t Table) In(currency string) ports.Pricer

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.

func (Table) Lookup

func (t Table) Lookup(model, modelID string) (ModelPrice, bool)

Lookup finds a model's price, trying the registry key, the wire id, then the registry key without its "@variant" suffix.

func (Table) Price

func (t Table) Price(ctx context.Context, u ports.NewUsage) (*ports.Cost, error)

Price implements ports.Pricer in the table's default currency, USD.

Jump to

Keyboard shortcuts

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