Documentation
¶
Overview ¶
Package genaiprices calculates LLM inference API pricing from an embedded catalog of provider and model prices. It is a Go port of the Python and JavaScript genai-prices packages and shares their bundled data (prices/data.json).
Index ¶
- Constants
- Variables
- type ArrayMatch
- type ConditionalPrice
- type Constraint
- type ExtractPath
- type ExtractedUsage
- type MatchLogic
- type ModelInfo
- type ModelPrice
- type Option
- type PathStep
- type Price
- type PriceCalculation
- type Provider
- type Tier
- type TieredPrices
- type Usage
- type UsageExtractor
- type UsageExtractorMapping
Constants ¶
const DataSource = "pydantic/genai-prices"
DataSource identifies the upstream project the embedded price catalog (data.json) is synced from. The exact upstream data version last synced is tracked in upstream-watch/requirements.txt. See SYNCING.md.
const Name = "genai-prices"
Name identifies this library in telemetry that records cost-estimate provenance.
const Version = "0.0.2"
Version is the honeycombio/genai-prices release version. It tracks THIS library's releases, which are deliberately not 1:1 with upstream pydantic/genai-prices data syncs: we can ship engine changes without a data bump, or sync data without a code release. Bump it when cutting a release.
Variables ¶
var ( ErrProviderNotFound = errors.New("genaiprices: provider not found") ErrModelNotFound = errors.New("genaiprices: model not found") )
Sentinel errors returned by CalcPrice / FindProvider, matchable with errors.Is.
Functions ¶
This section is empty.
Types ¶
type ArrayMatch ¶
type ArrayMatch struct {
Type string `json:"type"`
Field string `json:"field"`
Match MatchLogic `json:"match"`
}
ArrayMatch finds the first item in an array whose Field matches Match.
type ConditionalPrice ¶
type ConditionalPrice struct {
Constraint *Constraint `json:"constraint,omitempty"`
Prices ModelPrice `json:"prices"`
}
ConditionalPrice pairs a set of prices with an optional constraint defining when those prices apply.
type Constraint ¶
type Constraint struct {
// Kind is either "start_date" or "time_of_date".
Kind string
// StartDate is set when Kind == "start_date".
StartDate time.Time
// StartTime / EndTime are "HH:MM:SS" UTC strings when Kind == "time_of_date".
StartTime string
EndTime string
}
Constraint defines when a ConditionalPrice is active. The source data distinguishes the two kinds by which fields are present: a start_date marks a date constraint; start_time + end_time mark a daily time-of-day window.
func (*Constraint) UnmarshalJSON ¶
func (c *Constraint) UnmarshalJSON(data []byte) error
type ExtractPath ¶
type ExtractPath struct {
Steps []PathStep
}
ExtractPath is a path into a decoded JSON response: a sequence of object keys (strings) and ArrayMatch steps. The source encodes it as either a single string or an array of strings/ArrayMatch objects.
func (*ExtractPath) UnmarshalJSON ¶
func (e *ExtractPath) UnmarshalJSON(data []byte) error
type ExtractedUsage ¶
ExtractedUsage is the result of ExtractUsage.
func ExtractUsage ¶
func ExtractUsage(provider *Provider, responseData any, opts ...Option) (*ExtractedUsage, error)
ExtractUsage extracts the model name and token usage from a decoded API response (a map[string]any / []any tree as produced by json.Unmarshal). Pass WithAPIFlavor to select a non-default extractor. The returned ExtractedUsage includes the matched ModelInfo when the model name resolves within the bundled catalog.
type MatchLogic ¶
type MatchLogic struct {
// contains filtered or unexported fields
}
MatchLogic is the recursive boolean logic used to match a string (a model or provider identifier). Exactly one clause kind is set per node.
func Contains ¶
func Contains(s string) MatchLogic
Contains matches text containing s (case-insensitive).
func EndsWith ¶
func EndsWith(s string) MatchLogic
EndsWith matches text with suffix s (case-insensitive).
func Regex ¶
func Regex(s string) MatchLogic
Regex matches text against the regular expression s (case-sensitive).
func StartsWith ¶
func StartsWith(s string) MatchLogic
StartsWith matches text with prefix s (case-insensitive).
func (*MatchLogic) IsMatch ¶
func (m *MatchLogic) IsMatch(text string) bool
IsMatch reports whether text satisfies this match logic. All comparisons are case-insensitive except regex.
func (*MatchLogic) UnmarshalJSON ¶
func (m *MatchLogic) UnmarshalJSON(data []byte) error
type ModelInfo ¶
type ModelInfo struct {
ID string `json:"id"`
Match MatchLogic `json:"match"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
ContextWindow *int `json:"context_window,omitempty"`
PriceComments string `json:"price_comments,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
// Prices is always normalized to a slice of conditional prices. A bare
// ModelPrice object in the source data becomes a single entry with a nil
// constraint. See getActiveModelPrice for how an active price is selected.
Prices []ConditionalPrice `json:"prices"`
}
ModelInfo is a single model offered by a provider.
func (*ModelInfo) UnmarshalJSON ¶
UnmarshalJSON normalizes the polymorphic `prices` field (either a single ModelPrice object or an array of ConditionalPrice) into a slice.
type ModelPrice ¶
type ModelPrice struct {
InputMTok *Price `json:"input_mtok,omitempty"`
CacheWriteMTok *Price `json:"cache_write_mtok,omitempty"`
CacheReadMTok *Price `json:"cache_read_mtok,omitempty"`
OutputMTok *Price `json:"output_mtok,omitempty"`
InputAudioMTok *Price `json:"input_audio_mtok,omitempty"`
CacheAudioReadMTok *Price `json:"cache_audio_read_mtok,omitempty"`
OutputAudioMTok *Price `json:"output_audio_mtok,omitempty"`
RequestsKCount *float64 `json:"requests_kcount,omitempty"`
}
ModelPrice is the set of per-token (per million) prices for a model. A nil pointer field means that bucket is not priced, which the engine relies on.
type Option ¶
type Option func(*resolveOptions)
Option configures CalcPrice, FindProvider and ExtractUsage.
func WithAPIFlavor ¶
WithAPIFlavor selects the extractor flavor for ExtractUsage (default "default").
func WithProvider ¶
WithProvider uses the given provider (and only it) instead of the bundled catalog, allowing custom or not-yet-published models.
func WithProviderAPIURL ¶
WithProviderAPIURL selects the provider whose api_pattern matches url.
func WithProviderID ¶
WithProviderID selects the provider by its identifier (e.g. "openai"). The special id "litellm" enables "provider/model" prefix handling on the model reference.
func WithTimestamp ¶
WithTimestamp sets the request time used to select conditional/time-of-day prices. Defaults to time.Now().
type PathStep ¶
type PathStep struct {
Key string
Array *ArrayMatch
}
PathStep is one step of an ExtractPath: either an object key or an ArrayMatch.
func (*PathStep) UnmarshalJSON ¶
type Price ¶
type Price struct {
// Flat is the price per million tokens when Tiered is nil.
Flat float64
// Tiered, when non-nil, defines threshold (cliff) pricing.
Tiered *TieredPrices
}
Price is a per-million-token price that is either a flat rate or tiered.
func (*Price) UnmarshalJSON ¶
type PriceCalculation ¶
type PriceCalculation struct {
InputPrice float64
OutputPrice float64
TotalPrice float64
Provider *Provider
Model *ModelInfo
ModelPrice ModelPrice
}
PriceCalculation is the result of CalcPrice.
func CalcPrice ¶
func CalcPrice(usage Usage, modelRef string, opts ...Option) (*PriceCalculation, error)
CalcPrice calculates the price for usage of modelRef. Provide WithProviderID or WithProviderAPIURL when known for the most reliable matching; otherwise the model reference is matched against each provider's model_match logic.
It returns ErrProviderNotFound or ErrModelNotFound (matchable with errors.Is) when no match exists.
type Provider ¶
type Provider struct {
ID string `json:"id"`
Name string `json:"name"`
APIPattern string `json:"api_pattern"`
PricingURLs []string `json:"pricing_urls,omitempty"`
Description string `json:"description,omitempty"`
PriceComments string `json:"price_comments,omitempty"`
ModelMatch *MatchLogic `json:"model_match,omitempty"`
ProviderMatch *MatchLogic `json:"provider_match,omitempty"`
Extractors []UsageExtractor `json:"extractors,omitempty"`
FallbackModelProviders []string `json:"fallback_model_providers,omitempty"`
Models []ModelInfo `json:"models"`
}
Provider is an LLM inference provider together with its models and the logic used to match it and extract usage from its API responses.
func FindProvider ¶
FindProvider resolves a provider from the given options (WithProviderID, WithProviderAPIURL, or WithProvider). It returns ErrProviderNotFound if none match.
type TieredPrices ¶
TieredPrices is threshold-based (cliff) pricing: crossing a tier applies that tier's rate to ALL tokens.
func (*TieredPrices) UnmarshalJSON ¶
func (t *TieredPrices) UnmarshalJSON(data []byte) error
type Usage ¶
type Usage struct {
InputTokens int
CacheWriteTokens int
CacheReadTokens int
OutputTokens int
InputAudioTokens int
CacheAudioReadTokens int
OutputAudioTokens int
}
Usage holds token counts for a single LLM call. All fields are optional; InputTokens should INCLUDE cached tokens.
type UsageExtractor ¶
type UsageExtractor struct {
APIFlavor string `json:"api_flavor"`
Root ExtractPath `json:"root"`
ModelPath ExtractPath `json:"model_path"`
Mappings []UsageExtractorMapping `json:"mappings"`
}
UsageExtractor describes how to pull usage and the model name out of a provider API response for a given API flavor.
type UsageExtractorMapping ¶
type UsageExtractorMapping struct {
Path ExtractPath `json:"path"`
Dest string `json:"dest"`
Required bool `json:"required"`
}
UsageExtractorMapping maps a path in the response to a Usage field.