pricing

package
v1.4.6 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package pricing provides config-driven LLM cost estimation from a YAML pricing table.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WarnUnknownModelOnce

func WarnUnknownModelOnce(providerID, model string)

WarnUnknownModelOnce logs a warning the first time an unknown model is used for cost estimation.

Types

type ModelPricing

type ModelPricing struct {
	InputPer1M  float64 `yaml:"input_per_1m"`
	OutputPer1M float64 `yaml:"output_per_1m"`
}

ModelPricing holds per-1M-token USD prices for a single model.

type PricingTable

type PricingTable struct {
	Version   string                     `yaml:"version"`
	Providers map[string]ProviderPricing `yaml:"providers"`
}

PricingTable is the root structure of pricing/models.yaml.

func Load

func Load(path string) (*PricingTable, error)

Load parses the YAML file at path, resolves inherit references (single depth, no chains), and validates that no prices are negative. Returns an error if the file is missing or malformed.

func LoadOrDefault

func LoadOrDefault(path string) *PricingTable

LoadOrDefault calls Load and on error falls back to the embedded default pricing table (so cost estimation still works when pricing/models.yaml is missing). Logs at info when using the embedded default. Never panics.

func (*PricingTable) Estimate

func (t *PricingTable) Estimate(providerID, model string, inputTokens, outputTokens int) (cost float64, known bool)

Estimate looks up provider and model, computes cost in USD, and returns (cost, true) if found. Returns (0.0, false) if provider or model is not in the table. Safe for concurrent use. If the provider exists with an empty models map (e.g. ollama), returns (0.0, true) for any model (free). Model lookup tries exact key first, then a base name (e.g. gpt-4o-2024-08-06 -> gpt-4o) so API-returned model IDs still match pricing table keys.

func (*PricingTable) ModelCount

func (t *PricingTable) ModelCount(providerID string) int

ModelCount returns the number of models configured for a provider (for PricingAvailable / CLI).

type ProviderPricing

type ProviderPricing struct {
	Models  map[string]ModelPricing `yaml:"models"`
	Inherit string                  `yaml:"inherit,omitempty"`
}

ProviderPricing holds model pricing for a provider, with optional inherit from another provider.

Jump to

Keyboard shortcuts

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