pricing

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package pricing centralizes per-model token pricing for all LLM providers.

Each provider previously embedded its own pricing table and cost calculator. That worked but made the cost CLI, multi-dimensional budget gate, and router (idea 09) reach into provider-internals to compare models. This package is the single source of truth: defaults shipped per provider, merged with optional config overrides, plus per-provider Calc dispatch that keeps the distinct cache semantics each vendor bills under.

Index

Constants

View Source
const (
	ProviderAnthropic = "anthropic"
	ProviderOpenAI    = "openai"
	ProviderGemini    = "gemini"
)

Provider identifiers used by Calc to pick cache semantics.

Variables

This section is empty.

Functions

func CalcAnthropic

func CalcAnthropic(usage events.Usage, r Rates) float64

CalcAnthropic implements Anthropic's billing model.

Anthropic's reported `input_tokens` excludes both cache-creation and cache-read portions, so PromptTokens is the cache-miss (plain input) count and CachedTokens / CacheWriteTokens are billed separately.

CacheWriteTokens are charged at the 5-minute-TTL rate; per-request TTL selection (idea 05 prompt caching plan) routes 1h writes via the explicit CacheWrite1hPerMillion rate when callers split the counts. Today no caller split is in place, so all writes hit the 5m lane.

func CalcGemini

func CalcGemini(usage events.Usage, r Rates) float64

CalcGemini implements Gemini's billing model. CachedTokens are billed at CachedRatio × InputPerMillion (Google bills cached input ~25% of standard). PromptTokens includes the cached share, so the uncached remainder bills at the plain input rate. ReasoningTokens (thoughtTokenCount) bill at the output rate per Google's policy.

func CalcOpenAI

func CalcOpenAI(usage events.Usage, r Rates) float64

CalcOpenAI implements OpenAI's billing model. OpenAI auto-caches eligible prompt prefixes (>=1024 tokens) and bills the cached portion at half the input rate by default. PromptTokens already includes the cached share, so we split out CachedTokens and bill the remainder at the plain input rate.

Types

type Rates

type Rates struct {
	InputPerMillion        float64
	OutputPerMillion       float64
	CacheReadPerMillion    float64
	CacheWrite5mPerMillion float64
	CacheWrite1hPerMillion float64
	CachedRatio            float64
}

Rates holds per-model token rates expressed in USD per million tokens.

Not every field applies to every provider:

  • Anthropic uses Input/Output + the explicit cache lanes (read, write 5m, write 1h). Zero cache lanes derive from InputPerMillion at provider defaults (read 0.10×, write 5m 1.25×, write 1h 2.0×).
  • OpenAI uses Input/Output + CacheReadPerMillion. Zero CacheRead derives as 0.5× InputPerMillion (the auto-cache discount).
  • Gemini uses Input/Output + CachedRatio. Zero ratio defaults to 0.25. Reasoning tokens are billed at the output rate.

CalcAnthropic / CalcOpenAI / CalcGemini encode each provider's billing rules; callers pick the right one or use Table.Calc.

type Table

type Table struct {
	Provider string
	// contains filtered or unexported fields
}

Table is a per-provider price table. Construct via DefaultsFor and call Merge to layer config overrides on top.

Table is safe for concurrent use. Hot-reload calls Merge while the bus dispatch path reads via Get/Calc/Models from provider plugins and the budget gate, so an internal RWMutex protects rates against the resulting race.

func DefaultsFor

func DefaultsFor(provider string) *Table

DefaultsFor returns the embedded default price table for the given provider. Unknown providers return an empty table — callers should still receive a usable (zero-cost) result rather than a nil pointer.

func NewTable

func NewTable(provider string) *Table

NewTable returns an empty table for the given provider.

func (*Table) Calc

func (t *Table) Calc(model string, usage events.Usage) float64

Calc dispatches to the per-provider calculator for the table's provider. Unknown providers return 0 (no rates table = no cost). Unknown models return 0 by the same logic.

func (*Table) CheapestModel

func (t *Table) CheapestModel(candidates []string) string

CheapestModel returns the model id with the lowest blended input+output rate among the candidates that exist in the table. Used by the budget gate's downgrade-model action and by routers.

blended = InputPerMillion + OutputPerMillion (a coarse proxy that ranks well enough for routing without requiring an actual usage profile).

Returns "" when no candidate is known to the table.

func (*Table) Get

func (t *Table) Get(model string) (Rates, bool)

Get returns the rates for a model. The bool reports whether the model was found in the table (defaults + overrides).

func (*Table) Merge

func (t *Table) Merge(overrides map[string]any)

Merge layers user-supplied overrides on top of the existing entries.

Accepted YAML shape (per-model fields are all optional):

pricing:
  claude-sonnet-4-6-20250514:
    input_per_million: 3.0
    output_per_million: 15.0
    cache_read_per_million: 0.30
    cache_write_5m_per_million: 3.75
    cache_write_1h_per_million: 6.0
  gemini-2.5-pro:
    cached_ratio: 0.25

Unknown keys are ignored. Nil overrides is a no-op.

func (*Table) Models

func (t *Table) Models() []string

Models returns the model IDs known to the table, in unspecified order.

func (*Table) Set

func (t *Table) Set(model string, rates Rates)

Set installs rates for a single model. Used by Merge and by tests.

Jump to

Keyboard shortcuts

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