pricing

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package pricing embeds pricing.json and computes the USD cost of a message.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type History

type History struct {
	// contains filtered or unexported fields
}

History is the time-indexed set of pricing tables, sorted ascending by Version.

func HistoryForTest

func HistoryForTest(entries []Table) (History, error)

HistoryForTest builds a History from a caller-provided slice.

Test-only helper. Exported because Go does not let _test.go in one package be imported by tests in another. Do not call from production code — use Load() instead, which reads the embedded history/*.json.

func Load

func Load() (History, error)

Load reads every embedded history/*.json file, validates each, and returns a History sorted by Version ascending. Duplicate versions are a build error.

func (History) CostFor

func (h History) CostFor(m parse.Message) (cost float64, version string, unknown bool)

CostFor resolves the applicable Table for m via fall-forward (see resolveTable) and returns cost, the stamped version string, and whether the model was missing from every snapshot at-or-after the date-resolved one. The stamped version is the snapshot whose rates produced the cost — the fall-forward source when the date-resolved snapshot predates the model.

func (History) Latest

func (h History) Latest() Table

Latest returns the highest-version entry.

func (History) TableAt

func (h History) TableAt(ts time.Time) Table

TableAt resolves the historical Table that applies to ts. Picks the largest entry with Version <= ts.UTC() date string. Falls back to earliest entry if ts predates all entries.

func (History) VersionFor added in v0.4.0

func (h History) VersionFor(ts time.Time, model string) string

VersionFor returns the pricing_version CostFor would stamp for a message with this timestamp and model, applying the same fall-forward resolution, without needing token counts. Used by staleness checks (PricingVersionStats).

func (History) Versions

func (h History) Versions() []string

Versions returns sorted version strings of every embedded entry.

type ModelRate

type ModelRate struct {
	InputPerMtok        float64 `json:"input_per_mtok"`
	OutputPerMtok       float64 `json:"output_per_mtok"`
	CacheReadPerMtok    float64 `json:"cache_read_per_mtok"`
	CacheWrite5mPerMtok float64 `json:"cache_write_5m_per_mtok"`
	CacheWrite1hPerMtok float64 `json:"cache_write_1h_per_mtok"`
}

ModelRate holds the per-million-token billing rates for one model.

type Table

type Table struct {
	Version  string               `json:"version"`
	Currency string               `json:"currency"`
	Models   map[string]ModelRate `json:"models"`
}

Table is a versioned snapshot of per-model billing rates decoded from a pricing JSON file.

func (Table) CostFor

func (t Table) CostFor(m parse.Message) (float64, bool)

CostFor returns USD estimate for the message and whether the model rate was missing from this Table.

Jump to

Keyboard shortcuts

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