prices

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 14 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 prices parses the daily model price catalogue, normalizes model ids, and indexes prices by source and maker.

Index

Constants

View Source
const DefaultInterval = 24 * time.Hour

DefaultInterval is the default refresh cadence when Worker.Interval is unset.

Variables

View Source
var Makers = []string{
	"anthropic", "openai", "google", "xai", "deepseek", "mistral", "moonshotai", "zhipuai",
	"alibaba", "meta", "minimax", "cohere",
}

Makers lists maker catalogue sources in search order. Builtin front-door types map to catalogue sources first.

View Source
var TypeSources = map[string]string{
	"openai":      "openai",
	"anthropic":   "anthropic",
	"gemini":      "google",
	"vertex":      "google-vertex",
	"xai":         "xai",
	"deepseek":    "deepseek",
	"mistral":     "mistral",
	"groq":        "groq",
	"cerebras":    "cerebras",
	"openrouter":  "openrouter",
	"fireworks":   "fireworks-ai",
	"cohere":      "cohere",
	"bedrock":     "amazon-bedrock",
	"azure":       "azure",
	"nebius":      "nebius",
	"huggingface": "huggingface",
}

TypeSources maps front-door built-in provider types to catalogue source ids.

Functions

func Fetch

func Fetch(ctx context.Context, client *http.Client, rawURL string) ([]store.CatalogEntry, error)

Fetch downloads and parses the price catalogue from rawURL with a 60 s timeout and a 32 MiB body cap, refusing empty results.

func Normalize

func Normalize(model string) string

Normalize turns a provider/model identifier into its canonical matching form: lower-case, leading '~' dropped, portion after the last '/' kept, ':suffix' dropped, trailing '-latest', '-YYYY-MM-DD' or '-YYYYMMDD' dropped, and '.'/'_' replaced with '-'.

func Parse

func Parse(r io.Reader) ([]store.CatalogEntry, error)

Parse decodes the catalogue JSON, skipping models without cost.input/output and ignoring tiers, context_over_200k, reasoning, and audio fields.

Types

type Catalog

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

Catalog is an immutable in-memory price index. Callers swap it via atomic.Pointer.

func NewCatalog

func NewCatalog(entries []store.CatalogEntry) *Catalog

NewCatalog builds an immutable price index from entries. Missing cache_read or cache_write rates default to input.

func (*Catalog) Find

func (c *Catalog) Find(model string) (p Price, ref string, ok bool)

Find searches Makers in order for an exact model ID match first (preserving dated snapshots), then for the normalized id, then retries with a trailing -NNNN snapshot suffix dropped. ref is "<source>/<model>" using the matched catalogue entry's raw id.

func (*Catalog) Lookup

func (c *Catalog) Lookup(source, model string) (Price, bool)

Lookup looks up model under source, trying the exact entry first and then the normalized id.

type Price

type Price struct {
	Input, Output, CacheRead, CacheWrite float64
}

Price holds token rates in USD per 1M tokens.

type Store

type Store interface {
	Catalog(ctx context.Context) ([]store.CatalogEntry, time.Time, error)
	ReplaceCatalog(ctx context.Context, entries []store.CatalogEntry, fetchedAt time.Time) error
}

Store reads and replaces the persisted price catalogue. *store.Store implements it.

type Worker

type Worker struct {
	URL      string
	Interval time.Duration
	Store    Store
	Client   *http.Client
	Log      *slog.Logger
	// contains filtered or unexported fields
}

Worker keeps an in-memory *Catalog backed by the store and refreshes it periodically from URL. An empty URL disables fetching and serves the stored copy only.

func (*Worker) Current

func (w *Worker) Current() *Catalog

Current returns the active price catalogue. It never returns nil.

func (*Worker) Load

func (w *Worker) Load(ctx context.Context) error

Load builds the index from Store.Catalog and fetches once if the stored copy is empty or older than Interval. A failed fetch logs a warning and keeps the stored copy.

func (*Worker) Run

func (w *Worker) Run(ctx context.Context)

Run fetches the catalogue every Interval until ctx is cancelled. When URL is empty, Run returns immediately.

Jump to

Keyboard shortcuts

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