pricing

package
v0.38.4 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package pricing computes a dollar cost for a run's token usage using, in order of preference:

  1. The Copilot SDK's per-request cost (UsageStats.ModelMetrics.RequestCost), when any model reports a non-zero cost.
  2. A model rate table that prices input, output, cache-read, and cache-write tokens per million for known model families, when every model in the usage map can be resolved.
  3. A flat fallback estimate of $0.00025 per token (the historical behavior), when neither SDK data nor the rate table can price the usage.

The Compute function returns the source so the dashboard can disclose how the number was derived.

Index

Constants

View Source
const FlatRatePerToken = 0.00025

FlatRatePerToken is the per-token rate used by the flat estimate fallback. This matches the historical behavior that the rate table replaces.

View Source
const RateTableEffectiveDate = "2025-01-01"

RateTableEffectiveDate is the date the embedded rate table reflects. It is surfaced in dashboard tooltips so users can judge staleness.

View Source
const SourceEstimate = "estimate"

SourceEstimate indicates the cost was computed from the flat-rate fallback.

View Source
const SourceMixed = "mixed"

SourceMixed is used by aggregators when underlying runs report different sources. It is not returned by Compute.

View Source
const SourceSDK = "sdk"

SourceSDK indicates the cost was reported directly by the Copilot SDK.

View Source
const SourceTable = "table"

SourceTable indicates the cost was priced from the in-repo rate table.

Variables

This section is empty.

Functions

func CombineSources

func CombineSources(sources []string) string

CombineSources collapses per-run cost sources into a single label suitable for an aggregate (e.g. SummaryResponse). Empty / unknown sources are ignored; if no sources are provided it returns "". If all sources agree it returns that source. Otherwise it returns SourceMixed.

func Compute

func Compute(usage *models.UsageStats) (float64, string)

Compute returns the dollar cost for a run's usage and the source of the number. The source is one of SourceSDK, SourceTable, or SourceEstimate.

A nil or zero-token usage returns (0, SourceEstimate).

Types

type Rates

type Rates struct {
	InputPer1M      float64
	OutputPer1M     float64
	CacheReadPer1M  float64
	CacheWritePer1M float64
	EffectiveDate   string
}

Rates describes per-million-token prices for a model family in USD.

Cache pricing follows Anthropic's published tiers (read = cheaper than input, write = more expensive than input). For providers without cache billing (e.g. OpenAI does not separately price cache writes), the relevant fields are left at zero.

func LookupRates

func LookupRates(modelID string) (Rates, bool)

LookupRates returns rates for a model ID. It first tries an exact normalized match, then walks the longest prefix down to the shortest looking for a known family. The second return value is false if no match is found.

Examples:

  • "claude-opus-4.6" -> "claude-opus-4"
  • "claude-opus-4-20250514" -> "claude-opus-4"
  • "claude-sonnet-4.6-fast" -> "claude-sonnet-4"
  • "gpt-5-codex-max" -> "gpt-5-codex"
  • "gpt-5.1" -> "gpt-5"

func (Rates) Cost

func (r Rates) Cost(u models.ModelUsage) float64

Cost prices a single model's usage at these rates.

Jump to

Keyboard shortcuts

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