Files
ai/controllers/model_pricing.go

628 lines
30 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"math"
"strings"
"sync"
"github.com/hanzoai/ai/log"
"github.com/hanzoai/ai/object"
)
// modelPrice defines per-model economics in dollars per 1M tokens. Input/Output/Cache*
// are the CUSTOMER PRICE (what the org is billed); CostIn/CostOut are the PROVIDER COGS
// (what it costs Hanzo to serve the call). The two are independent facts: a price is
// what we chose to charge, a cost is what an upstream invoices us, and a model may
// state one without the other. Where CostIn/CostOut are unset the cost is not known —
// see costed — and no margin is reported for it.
type modelPrice struct {
InputPerMillion float64 // $ per 1M input tokens (customer price)
OutputPerMillion float64 // $ per 1M output tokens (customer price)
CacheReadPerMillion float64 // $ per 1M cache-read tokens (0 = use InputPerMillion)
CacheWritePerMillion float64 // $ per 1M cache-write tokens (0 = use InputPerMillion)
CostInPerMillion float64 // $ per 1M input tokens (provider COGS; 0 = not known)
CostOutPerMillion float64 // $ per 1M output tokens (provider COGS; 0 = not known)
}
// costed reports whether this model states what it costs us to serve.
//
// Both legs, because half a COGS is not a COGS: a rate on the input leg and nothing on
// the output leg would price the completion — the expensive half — at zero and read as
// enormous margin.
//
// The rates are what an upstream invoices us. Where they are not configured, nothing
// here knows them, and the honest report is that nobody asked the vendor rather than a
// number chosen so the arithmetic completes. Every caller of costInputPerMillion asks
// this first; there is no path that reads a rate without knowing whether it is one.
func (p modelPrice) costed() bool {
return p.CostInPerMillion > 0 && p.CostOutPerMillion > 0
}
// costInputPerMillion is the provider COGS for input tokens. Ask costed first.
func (p modelPrice) costInputPerMillion() float64 { return p.CostInPerMillion }
// costOutputPerMillion is the provider COGS for output tokens. Ask costed first.
func (p modelPrice) costOutputPerMillion() float64 { return p.CostOutPerMillion }
// modelPricingInfo is the public, omitempty pricing block embedded in the
// /v1/models response (modelInfo.Pricing). It is expressed in US dollars per
// one million tokens and is emitted ONLY when ai holds real pricing for the
// model — never synthesized from defaults.
type modelPricingInfo struct {
Input float64 `json:"input"` // USD per 1M input tokens
Output float64 `json:"output"` // USD per 1M output tokens
}
// pricingInfo projects an internal modelPrice into the public pricing block, or
// returns nil (→ dropped by omitempty) when nobody stated a price. ok is the whole
// question: it reports that a real per-model entry was FOUND, and a found price of
// zero is a price — the one a caller most needs to see, since it says the route is
// free. Only a model no source names has its pricing block dropped.
func pricingInfo(p modelPrice, ok bool) *modelPricingInfo {
if !ok {
return nil
}
return &modelPricingInfo{Input: p.InputPerMillion, Output: p.OutputPerMillion}
}
// staticModelPrice looks up real per-model pricing from the static tables only.
// Unlike getModelPrice it neither queries the DB nor falls back to a default
// price: ok is false when ai holds no genuine pricing, letting the /v1/models
// listing OMIT pricing rather than fabricate it.
func staticModelPrice(model string) (modelPrice, bool) {
// Zen family: the discovered retail price is the source of truth (hip-00NN).
if p, ok := familyModelPrice(model); ok {
return p, true
}
m := strings.ToLower(model)
if price, ok := modelPricing[m]; ok {
return price, true
}
if base, ok := aliasPricing[m]; ok {
if price, ok := modelPricing[base]; ok {
return price, true
}
}
return modelPrice{}, false
}
// modelPricing maps upstream model identifiers to their pricing.
// Keyed by user-facing model name (lowercase). Pricing reflects actual
// upstream costs with Hanzo margin applied.
//
// Sources:
// - DO-AI: DigitalOcean GenAI Platform pricing (Feb 2026)
// - Fireworks: Fireworks AI pricing (Feb 2026)
// - OpenAI Direct: OpenAI API pricing (Feb 2026)
var modelPricing = map[string]modelPrice{
// ── DO-AI models (non-premium) ───────────────────────────────────
// OpenAI via DO-AI
"gpt-4o": {InputPerMillion: 2.50, OutputPerMillion: 10.00},
"gpt-4o-mini": {InputPerMillion: 0.15, OutputPerMillion: 0.60},
"gpt-4.1": {InputPerMillion: 2.00, OutputPerMillion: 8.00},
"gpt-5": {InputPerMillion: 5.00, OutputPerMillion: 15.00},
"gpt-5-mini": {InputPerMillion: 1.25, OutputPerMillion: 5.00},
"gpt-5-nano": {InputPerMillion: 0.30, OutputPerMillion: 1.20},
"gpt-5.1-codex-max": {InputPerMillion: 3.00, OutputPerMillion: 12.00},
"gpt-5.2": {InputPerMillion: 5.00, OutputPerMillion: 15.00},
"gpt-5.2-pro": {InputPerMillion: 10.00, OutputPerMillion: 30.00},
"gpt-oss-120b": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"gpt-oss-20b": {InputPerMillion: 0.20, OutputPerMillion: 0.20},
"o1": {InputPerMillion: 15.00, OutputPerMillion: 60.00},
"o3": {InputPerMillion: 10.00, OutputPerMillion: 40.00},
"o3-mini": {InputPerMillion: 1.10, OutputPerMillion: 4.40},
// Anthropic via DO-AI
"claude-3-5-haiku": {InputPerMillion: 0.80, OutputPerMillion: 4.00},
"claude-3-7-sonnet": {InputPerMillion: 3.00, OutputPerMillion: 15.00},
"claude-4-1-opus": {InputPerMillion: 15.00, OutputPerMillion: 75.00},
"claude-haiku-4-5": {InputPerMillion: 1.00, OutputPerMillion: 5.00},
"claude-opus-4": {InputPerMillion: 15.00, OutputPerMillion: 75.00},
"claude-opus-4-5": {InputPerMillion: 15.00, OutputPerMillion: 75.00},
"claude-opus-4-6": {InputPerMillion: 15.00, OutputPerMillion: 75.00},
"claude-sonnet-4": {InputPerMillion: 3.00, OutputPerMillion: 15.00},
"claude-sonnet-4-5": {InputPerMillion: 3.00, OutputPerMillion: 15.00},
// Open source via DO-AI
"deepseek-r1-distill-70b": {InputPerMillion: 0.35, OutputPerMillion: 1.20},
"llama-3.1-8b": {InputPerMillion: 0.10, OutputPerMillion: 0.10},
"llama-3.3-70b": {InputPerMillion: 0.59, OutputPerMillion: 0.79},
"mistral-nemo": {InputPerMillion: 0.15, OutputPerMillion: 0.15},
"qwen3-32b": {InputPerMillion: 0.20, OutputPerMillion: 0.60},
"gemma-4-31b": {InputPerMillion: 0.18, OutputPerMillion: 0.50}, // DO published rate
// DO-AI embeddings (POST /v1/embeddings). Output cost is 0 — embeddings
// bill on input tokens only. Rates are DigitalOcean's published per-1M
// embedding prices (real, not defaulted).
"bge-m3": {InputPerMillion: 0.02},
"e5-large-v2": {InputPerMillion: 0.02},
"gte-large-en-v1.5": {InputPerMillion: 0.09},
"all-mini-lm-l6-v2": {InputPerMillion: 0.009},
"multi-qa-mpnet-base-dot-v1": {InputPerMillion: 0.009},
// DO-AI Inference Router. The router itself adds no cost (public preview);
// the request bills at the underlying foundation model it selects. There is
// no fixed per-router token price upstream, so this is a conservative
// in-pattern estimate matching the gpt-oss-120b class the routers select
// (DEFAULTED — not a published per-router rate).
"router:general": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"router:knowledge-base-document": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"router:software-engineering": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"router:software-engineering-01": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"router:writing": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
// ── Fireworks premium models ────────────────────────────────────
"fireworks/cogito-671b": {InputPerMillion: 3.00, OutputPerMillion: 3.00},
"fireworks/deepseek-r1": {InputPerMillion: 3.00, OutputPerMillion: 8.00},
"fireworks/deepseek-v3": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"fireworks/deepseek-v3p1": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"fireworks/deepseek-v3p2": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"fireworks/glm-4p7": {InputPerMillion: 1.80, OutputPerMillion: 6.60},
"fireworks/glm-5": {InputPerMillion: 3.00, OutputPerMillion: 9.60},
"fireworks/glm-5-thinking": {InputPerMillion: 3.00, OutputPerMillion: 9.60},
"fireworks/gpt-oss-120b": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"fireworks/gpt-oss-20b": {InputPerMillion: 0.20, OutputPerMillion: 0.20},
"fireworks/kimi-k2": {InputPerMillion: 2.00, OutputPerMillion: 8.00},
"fireworks/kimi-k2-thinking": {InputPerMillion: 2.00, OutputPerMillion: 8.00},
"fireworks/kimi-k2p5": {InputPerMillion: 2.50, OutputPerMillion: 10.00},
"fireworks/minimax-m2p5": {InputPerMillion: 1.00, OutputPerMillion: 4.00},
"fireworks/mixtral-8x22b": {InputPerMillion: 0.90, OutputPerMillion: 0.90},
"fireworks/qwen3-4b": {InputPerMillion: 0.30, OutputPerMillion: 0.30},
"fireworks/qwen3-8b": {InputPerMillion: 0.60, OutputPerMillion: 0.60},
"fireworks/qwen3-235b-a22b": {InputPerMillion: 3.60, OutputPerMillion: 3.60},
"fireworks/qwen3-coder-30b-a3b": {InputPerMillion: 1.50, OutputPerMillion: 1.50},
"fireworks/qwen3-coder-480b": {InputPerMillion: 3.60, OutputPerMillion: 3.60},
"fireworks/qwen3-coder-480b-bf16": {InputPerMillion: 4.50, OutputPerMillion: 4.50},
"fireworks/qwen3-next-80b-a3b": {InputPerMillion: 2.70, OutputPerMillion: 2.70},
"fireworks/qwen3-next-80b-a3b-thinking": {InputPerMillion: 2.70, OutputPerMillion: 2.70},
"fireworks/qwen3-vl-30b-a3b": {InputPerMillion: 0.45, OutputPerMillion: 1.80},
"fireworks/qwen3-vl-235b": {InputPerMillion: 1.20, OutputPerMillion: 1.20},
// ── OpenAI Direct premium models ────────────────────────────────
"openai-direct/gpt-4o": {InputPerMillion: 2.50, OutputPerMillion: 10.00},
"openai-direct/gpt-4o-mini": {InputPerMillion: 0.15, OutputPerMillion: 0.60},
"openai-direct/gpt-5": {InputPerMillion: 5.00, OutputPerMillion: 15.00},
"openai-direct/o3": {InputPerMillion: 10.00, OutputPerMillion: 40.00},
"openai-direct/o3-mini": {InputPerMillion: 1.10, OutputPerMillion: 4.40},
}
// imagePricePerImageCents maps an image model (user-facing name OR upstream
// fal/OpenAI id, lowercase) to its price per generated image, in cents. Image
// generation is billed per image, NOT per token — the token-based
// calculateCostCents math yields 0 for a 0-token image call, so image billing
// is this separate, explicit seam. Keyed by both the user-facing zen3-image
// name and the upstream id so a lookup succeeds whichever the caller records.
var imagePricePerImageCents = map[string]int64{
// Upstream ids (the do-ai fal image models) — same price, so billing is
// correct whether the record carries the user-facing or upstream name.
"fal-ai/flux/schnell": 5,
"fal-ai/fast-sdxl": 6,
// Stable Diffusion 3.5 Large — synchronous OpenAI /images shape (not fal
// async). DO published rate is $0.08/image. Keyed by both the user-facing
// name and the identical upstream id so billing is correct either way.
"stable-diffusion-3.5-large": 8,
// OpenAI-family image models, if ever routed directly ($0.08/image).
"gpt-image-1": 8,
"dall-e-3": 8,
"dall-e-2": 2,
}
// imageCostCents returns the total cost in cents for generating n images with
// the given model. It matches the user-facing name or the upstream id, falling
// back to a conservative per-image floor for an unknown image model so an image
// call is never silently free. n <= 0 costs nothing.
func imageCostCents(model string, n int) int64 {
if n <= 0 {
return 0
}
m := strings.ToLower(model)
per, ok := imagePricePerImageCents[m]
if !ok {
// Unknown image model: charge a conservative floor rather than $0.
per = 5
}
return per * int64(n)
}
// videoPricePerVideoCents maps a text-to-video model (user-facing name OR
// upstream do-ai id, lowercase) to its price per generated video, in cents. Like
// images, video is billed per unit, NOT per token (the token-based math yields 0
// for a 0-token video call), so video billing is this separate, explicit seam.
// Keyed by both the user-facing zen3-video name and the upstream id so a lookup
// succeeds whichever the caller records.
//
// PRICING NOTE: DigitalOcean publishes NO per-video rate for wan2-2-t2v-a14b (the
// catalog record carries only token fields, which are meaningless for video). 40
// cents/video is a real, defensible estimate: Wan 2.2 T2V on comparable fal /
// serverless GPU markets runs ~$0.20-0.40 for a short clip; 40¢ sits at the top
// of that band with Hanzo margin, and is intentionally an order of magnitude
// above the image rate (5-8¢) since a t2v inference is minutes of A14B compute.
// FLAGGED as best-estimate — refine when DO publishes a per-video price.
var videoPricePerVideoCents = map[string]int64{
// The do-ai text-to-video upstream, callable by its raw id. The zen video
// family (zen-video) is served by the zen service and priced from its catalog.
"wan2-2-t2v-a14b": 40,
}
// ── Audio ── transcription bills per MINUTE, synthesis per MILLION CHARACTERS ─
//
// Two maps, not one, because the two directions are priced in different units by
// every vendor that sells them: transcription per minute of audio (OpenAI's
// Whisper API, AWS Transcribe, Deepgram) and synthesis per million characters
// (OpenAI tts, AWS Polly, ElevenLabs). Collapsing them would force one of the two
// into a unit its market does not quote.
//
// Rates are deliberately EMPTY. The metering is the part that was broken — the
// quantity never reached the record — and it is fixed independently of what we
// decide to charge. An empty map means no configured rate, which makes
// recordUnpriced flag the row rather than invent a number, so audio traffic is
// visible and honest while priced at nothing.
//
// THE DECISION THAT IS OPEN, and the numbers it needs. sttPricePerMinuteCents is
// CENTS PER MINUTE of audio submitted; ttsPricePerMillionCharsCents is CENTS PER
// 1,000,000 CHARACTERS synthesized. Both are sell prices, not COGS — speech runs
// on our own hardware, so there is no upstream invoice to pass through and the
// number is a margin choice rather than a markup.
//
// What the cost side measures, on four idle cores (distil-small.en, int8):
//
// - STREAMING re-decodes its window as it fills, so it does SEVERAL times the
// decoding a batch call does for the same audio. Measured, audio-seconds
// decoded per audio-second submitted: 6.04 at one concurrent session, 2.64 at
// four, 1.79 at eight — the amplification falls as load rises, because a busy
// pod completes fewer re-decodes of the same window. Batch is 1.0 by
// definition.
// - At the operating point (four concurrent, the largest count whose backlog
// does not grow) that is 0.91 CPU-seconds per audio-second, and 2.83 at one.
//
// So the two are not one product priced by one number, and the direction is worth
// stating plainly: the SAME audio costs multiples more through the streaming
// endpoint, and costs LESS per second the busier the pod is. A single per-minute
// rate would sell live transcription under water while leaving batch overpriced.
//
// The batch side has no clean CPU-second figure yet — the earlier one was measured
// against a contended box and is withdrawn rather than quoted. Setting a rate needs
// it, and it is a short measurement, not a research project.
//
// What the market quotes per audio-minute, for the shape of the band rather than
// as a rate to copy: OpenAI Whisper 0.6 cents, Deepgram ~0.43 cents, AWS
// Transcribe 2.4 cents. Synthesis is quoted per million characters: AWS Polly
// $4 standard / $16 neural, OpenAI tts $15.
//
// Until the choice is made, an empty map is the honest state: seconds and
// characters ARE recorded, the row is flagged Unpriced, and the traffic is
// therefore visible and back-billable. Setting a rate here is a revenue decision,
// not a code change waiting to be finished.
var sttPricePerMinuteCents = map[string]int64{}
var ttsPricePerMillionCharsCents = map[string]int64{}
// sttCostCents prices a transcription from the audio duration it consumed,
// rounding UP to the cent so a sub-cent call is never free once a rate is set.
// No configured rate costs nothing (and the row is flagged Unpriced) rather than
// falling back to an invented floor: unlike an unknown image model, audio here is
// OUR service, so an absent rate is a decision not yet made, not a gap to paper
// over.
func sttCostCents(model string, seconds float64) int64 {
if seconds <= 0 {
return 0
}
perMinute, ok := sttPricePerMinuteCents[strings.ToLower(model)]
if !ok || perMinute <= 0 {
return 0
}
minutes := seconds / 60.0
return int64(math.Ceil(minutes * float64(perMinute)))
}
// ttsCostCents prices a synthesis from the characters it spoke, rounding UP to
// the cent. Same absent-rate rule as sttCostCents.
func ttsCostCents(model string, chars int) int64 {
if chars <= 0 {
return 0
}
perMillion, ok := ttsPricePerMillionCharsCents[strings.ToLower(model)]
if !ok || perMillion <= 0 {
return 0
}
return int64(math.Ceil(float64(chars) * float64(perMillion) / 1_000_000.0))
}
// audioPriced reports whether a configured rate exists for the model in the
// direction the record measured. It is what keeps the Unpriced flag HONEST
// without a second hardcoded `Unpriced: true` at every audio emit site: the flag
// follows the rate table, so setting a rate prices the traffic with no further
// edit at the call sites.
func audioPriced(model string, seconds float64, chars int) bool {
m := strings.ToLower(model)
if seconds > 0 {
if rate, ok := sttPricePerMinuteCents[m]; ok && rate > 0 {
return true
}
}
if chars > 0 {
if rate, ok := ttsPricePerMillionCharsCents[m]; ok && rate > 0 {
return true
}
}
return false
}
// recordIsAudio reports whether the record measured an audio quantity, in either
// direction. The ONE predicate the cost switches and the Unpriced detector share,
// so they can never disagree about what an audio row is.
func recordIsAudio(record *usageRecord) bool {
return record.AudioSeconds > 0 || record.AudioChars > 0
}
// audioCostCents prices an audio record from whichever quantity it carries. A
// record carries at most one direction (a call either transcribes or
// synthesizes), so the two are summed rather than branched.
func audioCostCents(record *usageRecord) int64 {
return sttCostCents(record.Model, record.AudioSeconds) +
ttsCostCents(record.Model, record.AudioChars)
}
// videoDefaultCostCents is the conservative per-video floor for an unknown video
// model, so a video call is never silently free. Set to the known real rate.
const videoDefaultCostCents int64 = 40
// videoCostCents returns the total cost in cents for generating n videos with
// the given model, matching the user-facing name or the upstream id and falling
// back to a conservative per-video floor for an unknown video model. n <= 0
// costs nothing.
func videoCostCents(model string, n int) int64 {
if n <= 0 {
return 0
}
m := strings.ToLower(model)
per, ok := videoPricePerVideoCents[m]
if !ok {
per = videoDefaultCostCents
}
return per * int64(n)
}
// DO-AI alias pricing (same as their base model)
var aliasPricing = map[string]string{
"openai/gpt-4o": "gpt-4o",
"openai/gpt-4o-mini": "gpt-4o-mini",
"openai/gpt-5": "gpt-5",
"openai/o3": "o3",
"openai/o3-mini": "o3-mini",
"anthropic/claude-haiku-4-5-20251001": "claude-haiku-4-5",
"anthropic/claude-opus-4-6": "claude-opus-4-6",
"anthropic/claude-sonnet-4-5-20250929": "claude-sonnet-4-5",
}
// getModelPrice looks up pricing for a user-facing model name.
// Returns a default price if the model isn't in the pricing table.
func getModelPrice(model string) modelPrice {
return getModelPriceForOrg(model, "")
}
func getModelPriceForOrg(model string, orgId string) modelPrice {
p, _ := getModelPriceForOrgOK(model, orgId)
return p
}
// getModelPriceForOrgOK is getModelPriceForOrg plus whether a REAL price was found. ok
// is false ONLY when every source missed (family, DB route, conf, static map/alias) and
// the conservative default ($1/$4 per 1M) was synthesized — the caller then marks the
// usage row Unpriced instead of silently trusting an invented price. The returned price
// is byte-identical to getModelPriceForOrg; nothing about billing changes.
func getModelPriceForOrgOK(model string, orgId string) (modelPrice, bool) {
// Zen family: the discovered retail price is the source of truth (hip-00NN).
//
// What the call COSTS us is a separate fact, and the family wire carries no cost
// field — so a family price arrives priced and uncosted, and the cost is completed
// from where costs are registered. Resolving the two together would make restating
// the price the only way to state a family model's COGS, which is how the cost came
// to BE the price.
if p, ok := familyModelPrice(model); ok {
if !p.costed() {
p.CostInPerMillion, p.CostOutPerMillion = registeredCost(model, orgId)
}
return p, true
}
// Check DB route pricing first (org-specific -> global)
//
// Priced is what lets the answer be zero: a route that states its price is the
// price, and a route that states nothing still reads as unstated at zero, so the
// fall-through to config and the static table is unchanged for every row written
// before the column existed.
dbRoute, err := object.ResolveModelRouteFromDB(strings.ToLower(model), orgId)
if err == nil && dbRoute != nil && (dbRoute.Priced || dbRoute.InputPrice > 0 || dbRoute.OutputPrice > 0) {
return modelPrice{
InputPerMillion: dbRoute.InputPrice,
OutputPerMillion: dbRoute.OutputPrice,
CostInPerMillion: dbRoute.CostInPerMillion,
CostOutPerMillion: dbRoute.CostOutPerMillion,
}, true
}
// YAML config pricing. When a config is loaded it is authoritative: a hit is a real
// price, a miss returns the configured default (a synthesized price ⇒ ok=false).
if cfg := GetModelConfig(); cfg != nil {
return cfg.GetPriceOK(model)
}
// Static fallback
m := strings.ToLower(model)
// Direct lookup
if price, ok := modelPricing[m]; ok {
return price, true
}
// Check aliases
if base, ok := aliasPricing[m]; ok {
if price, ok := modelPricing[base]; ok {
return price, true
}
}
// Default: conservative pricing for unknown models — the ONE synthesized price.
return modelPrice{InputPerMillion: 1.00, OutputPerMillion: 4.00}, false
}
// registeredCost is what an operator has recorded that a model costs us, per 1M tokens,
// and 0,0 when nobody has recorded anything.
//
// The route row is the one place a COGS is registered — object.ModelRoute carries the
// pair, and the admin cockpit reads and writes it there. The configured PRICE tables are
// deliberately not consulted: a price is what we charge, and reaching for it here is the
// substitution this function exists to avoid.
//
// Both legs or neither, matching costed(): a row that names only the input rate would
// leave the completion — the expensive half — at zero and report the call as almost pure
// margin.
func registeredCost(model, orgId string) (float64, float64) {
route, err := object.ResolveModelRouteFromDB(strings.ToLower(model), orgId)
if err != nil || route == nil {
return 0, 0
}
if route.CostInPerMillion > 0 && route.CostOutPerMillion > 0 {
return route.CostInPerMillion, route.CostOutPerMillion
}
return 0, 0
}
// unpricedWarned dedupes the unpriced-model warning to once per model id, so a popular
// unknown model logs a single actionable line instead of one per request.
var unpricedWarned sync.Map
// warnUnpricedOnce logs (once per model) that calls are billing at the conservative
// default because the model has no configured price. The row is also flagged Unpriced;
// this log is the ops signal to add the model to conf/models.yaml or a model route.
func warnUnpricedOnce(model string) {
if _, seen := unpricedWarned.LoadOrStore(strings.ToLower(model), struct{}{}); seen {
return
}
log.Warning("pricing: model %q has no configured price — billing at the conservative default ($1.00/$4.00 per 1M); add it to conf/models.yaml or a model route. Its usage rows are flagged unpriced.", model)
}
// recordUnpriced reports whether a token-billed call fell back to the conservative
// default price because the model is unknown to every pricing source. Image/video calls
// have their own per-unit pricing and are never "unpriced" here. The ONE detector, read
// by recordUsage (warn + row flag) and zapWriteUsage (warehouse column).
func recordUnpriced(record *usageRecord) bool {
// A free route has a price and it is zero. Reading "the table has no rate for
// this SKU" as "we guessed" would file the one honest number in the ledger as
// an invented one.
if record.Free {
return false
}
if record.ImageCount > 0 || record.VideoCount > 0 {
return false
}
// Audio is priced per minute / per million characters, not per token, so the
// token table cannot answer for it. The flag follows the audio rate table:
// while no rate is configured the row is honestly Unpriced, and setting one
// prices the traffic without touching a single emit site. This replaced a
// hardcoded `Unpriced: true` at each audio emitter, which said "no price"
// even after a price existed.
if recordIsAudio(record) {
return !audioPriced(record.Model, record.AudioSeconds, record.AudioChars)
}
_, ok := getModelPriceForOrgOK(record.Model, "")
return !ok
}
// costsNothing reports that a model's price is zero on both sides.
//
// It reads the price the LEDGER bills from, so "free" here and "$0.00" on the
// invoice are one fact rather than two opinions that can drift. It fails closed
// twice over: a model whose price is synthesized rather than found reads as not
// free (ok is false), and so does one this cannot name at all — `auto` among them,
// since what that resolves to is not yet decided.
func costsNothing(model, orgId string) bool {
p, ok := getModelPriceForOrgOK(model, orgId)
return ok && p.InputPerMillion <= 0 && p.OutputPerMillion <= 0
}
// ModelCostsNothing is costsNothing for the router's balance filter, which runs
// BEFORE any controller and therefore before the in-controller gate. Exported for
// the same reason FamilyModelGated is: the filter must be able to ask what a model
// costs without knowing how families and price tables are laid out.
func ModelCostsNothing(model, orgId string) bool { return costsNothing(model, orgId) }
// calculateCostCents computes the cost in cents for a model call.
func calculateCostCents(model string, promptTokens, completionTokens int) int64 {
return calculateCostCentsWithCache(model, promptTokens, completionTokens, 0, 0)
}
// costBreakdown is the per-component dollar cost of a token-billed LLM call. It is
// the ONE place the input/output/cache component split is computed, so the billing
// total (calculateCostCentsWithCache) and the o11y gen_ai span cost-detail
// (_o11y.gen_ai.cost_*) read the same numbers and can never drift.
type costBreakdown struct {
Input float64
Output float64
CacheRead float64
CacheWrite float64
Total float64
}
// modelCostBreakdown computes the per-component dollar cost of a token-billed call.
// Cache-read defaults to 10% of input price (Anthropic parity) and cache-write to the
// input price when the model declares no explicit cache rate. Pure: a pricing lookup
// and arithmetic, no I/O, no globals mutated — safe to call from the span emit path.
func modelCostBreakdown(model string, promptTokens, completionTokens, cacheReadTokens, cacheWriteTokens int) costBreakdown {
price := getModelPrice(model)
// Cache-read price: use explicit CacheReadPerMillion if set, else 10% of input.
cacheReadRate := price.CacheReadPerMillion
if cacheReadRate == 0 && price.InputPerMillion > 0 {
cacheReadRate = price.InputPerMillion * 0.10
}
// Cache-write price: use explicit CacheWritePerMillion if set, else same as input.
cacheWriteRate := price.CacheWritePerMillion
if cacheWriteRate == 0 {
cacheWriteRate = price.InputPerMillion
}
b := costBreakdown{
Input: float64(promptTokens) * price.InputPerMillion / 1_000_000.0,
Output: float64(completionTokens) * price.OutputPerMillion / 1_000_000.0,
CacheRead: float64(cacheReadTokens) * cacheReadRate / 1_000_000.0,
CacheWrite: float64(cacheWriteTokens) * cacheWriteRate / 1_000_000.0,
}
b.Total = b.Input + b.Output + b.CacheRead + b.CacheWrite
return b
}
// calculateCostCentsWithCache computes cost in cents including cache token pricing.
// Cache-read tokens are billed at 10% of input price (matching Anthropic).
// Cache-write tokens are billed at the same rate as input tokens.
func calculateCostCentsWithCache(model string, promptTokens, completionTokens, cacheReadTokens, cacheWriteTokens int) int64 {
b := modelCostBreakdown(model, promptTokens, completionTokens, cacheReadTokens, cacheWriteTokens)
costCents := int64(math.Round(b.Total * 100))
// Minimum 1 cent for any non-zero usage
if costCents <= 0 && (promptTokens > 0 || completionTokens > 0 || cacheReadTokens > 0 || cacheWriteTokens > 0) {
costCents = 1
}
return costCents
}