Documentation
¶
Overview ¶
Package pricing computes a dollar cost for a run's token usage using, in order of preference:
- The Copilot SDK's per-request cost (UsageStats.ModelMetrics.RequestCost), when any model reports a non-zero cost.
- 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.
- 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 ¶
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.
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.
const SourceEstimate = "estimate"
SourceEstimate indicates the cost was computed from the flat-rate fallback.
const SourceMixed = "mixed"
SourceMixed is used by aggregators when underlying runs report different sources. It is not returned by Compute.
const SourceSDK = "sdk"
SourceSDK indicates the cost was reported directly by the Copilot SDK.
const SourceTable = "table"
SourceTable indicates the cost was priced from the in-repo rate table.
Variables ¶
This section is empty.
Functions ¶
func CombineSources ¶
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.
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 ¶
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"