Files
ai/controllers/model_pricing.go
zeekay 12c52220fe video: OpenRouter's video models are served at /v1/videos/generations, kept in S3 and billed at what each clip cost
The resale family reads OpenRouter's video list beside its text list, so
Veo, Kling, Seedance, Wan, Hailuo, Runway, Grok Imagine and the rest are
SKUs with outputs video and a video object stating the durations,
resolutions, aspect ratios, frame images and audio each takes. A create is
held to that object, reserved at the dearest rate that applies to what it
asks for (the paid lane's quote too), and posted to the vendor; the job is
a stored row a watcher drives to the end: the file is copied into the
host's object store, its duration and frame size read from the MP4, and
the clip billed once at the vendor's stated cost times the markup. A
failed clip bills nothing. GET /v1/videos/{id} answers the job, or streams
it as events; /content redirects to a signed URL. A process that starts
resumes every job left unbilled.

A listed media model with no token price (Lyria's songs) bills each call
at the cost it states and is never served as free.
2026-10-17 03:55:09 -07:00

704 lines
34 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"
"strconv"
"strings"
"sync"
"github.com/hanzoai/ai/log"
"github.com/hanzoai/ai/object"
"github.com/hanzoai/decimal"
)
// 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)
// Variable: each call bills at the cost its answer states (zenModel.variable), and
// the rates above are only the ceiling a hold reserves, never a price.
Variable bool
// Card is the exact price list of a SKU we resell (zenModel.Card); nil otherwise.
Card *rateCard
}
// 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 pricing block of a /v1/models row (modelInfo.Pricing),
// emitted ONLY when ai holds real pricing for the model — never synthesized from
// defaults.
//
// Every key names its unit. Prompt and Completion are OpenRouter's keys in
// OpenRouter's unit, US dollars per ONE token as a decimal string, which is what a
// reader of an OpenAI-compatible catalog takes `pricing` to mean. InputPerMillion
// and OutputPerMillion are the same two rates per million tokens, the unit every
// price table here and hanzoai/pricing keep. No per-million figure sits under a
// bare `input`/`output`: Vercel-style catalogs use those keys per token, so a
// reader would price the model a million times over.
//
// Variable marks a router billed at what serves each call: the rates are then the
// most a token can bill, the ceiling its hold reserves, and a call bills its
// serving model's price at our margin, never more than these.
type modelPricingInfo struct {
Prompt string `json:"prompt"` // USD per input token, decimal string
Completion string `json:"completion"` // USD per output token, decimal string
InputPerMillion float64 `json:"input_per_million"` // USD per 1M input tokens
OutputPerMillion float64 `json:"output_per_million"` // USD per 1M output tokens
Variable bool `json:"variable,omitempty"` // the rates are a ceiling; each call bills what served it
// Rates is every rate the model's vendor states, in the vendor's names and units
// (USD per token for prompt, completion, input_cache_read, input_cache_write,
// internal_reasoning, image, audio; per request for request; per search for
// web_search), as billed: the vendor's rate plus the vendor's fee, exact. Present for a
// model we resell.
Rates map[string]string `json:"rates,omitempty"`
// Overrides are the vendor's conditional rates as billed: each states its
// condition (min_prompt_tokens, or utc_start, utc_end and utc_days for hours of
// the day) beside the rates that apply under it.
Overrides []map[string]any `json:"overrides,omitempty"`
}
// 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. A variable SKU lists the ceiling its hold reserves, marked variable, so a
// billed model never reads as unpriced. Only a model no source names has its pricing
// block dropped, and so does a rate that is not a number (a YAML `.inf`).
func pricingInfo(p modelPrice, ok bool) *modelPricingInfo {
if !ok || !finite(p.InputPerMillion) || !finite(p.OutputPerMillion) {
return nil
}
info := &modelPricingInfo{
Prompt: perToken(p.InputPerMillion),
Completion: perToken(p.OutputPerMillion),
InputPerMillion: p.InputPerMillion,
OutputPerMillion: p.OutputPerMillion,
Variable: p.Variable,
}
// A resold SKU's rates are stated exactly, never through the float above.
if c := p.Card; c != nil {
if r, ok := c.Rates["prompt"]; ok {
info.Prompt = r
}
if r, ok := c.Rates["completion"]; ok {
info.Completion = r
}
info.Rates, info.Overrides = c.Rates, c.Overrides
}
// The cache prices a call is billed at (cacheReadRate, cacheWriteRate), stated
// wherever the vendor's card does not state them: a model with no cache rate of its
// own bills a read at a tenth of input and a write at cacheWriteMultiple of it, and
// a caller pricing a cached conversation from this catalog reads the same numbers.
// Not a variable SKU: each of its calls bills what served it, cache included.
if p.InputPerMillion > 0 && !p.Variable {
rates := make(map[string]string, len(info.Rates)+2)
for k, v := range info.Rates {
rates[k] = v
}
read, write := cacheRates(p)
if _, ok := rates["input_cache_read"]; !ok {
rates["input_cache_read"] = read
}
if _, ok := rates["input_cache_write"]; !ok {
rates["input_cache_write"] = write
}
info.Rates = rates
}
return info
}
// cacheRates is what a cache read and a cache write of this price bill, per token and
// exactly: the model's own rate when it states one, else the ledger's defaults
// (cacheReadRate, cacheWriteRate) applied in decimal, so $0.80/M reads at
// "0.00000008" and never a float's 8.000000000000002e-08.
func cacheRates(p modelPrice) (read, write string) {
in := decimal.MustParse(perToken(p.InputPerMillion))
read, write = perToken(p.CacheReadPerMillion), perToken(p.CacheWritePerMillion)
if p.CacheReadPerMillion == 0 {
read = in.Mul(decimal.MustParse("0.1")).String()
}
if p.CacheWritePerMillion == 0 {
write = in.Mul(decimal.MustParse(strconv.FormatFloat(cacheWriteMultiple, 'f', -1, 64))).String()
}
return read, write
}
// perToken is a per-million rate as US dollars per token, exactly: the rate is read
// at its shortest round-tripping decimal and its point moved six places, so $0.80/M
// is "0.0000008" and never the float quotient 7.999999999999999e-07.
func perToken(perMillion float64) string {
d := decimal.MustParse(strconv.FormatFloat(perMillion, 'f', -1, 64))
return decimal.NewFromBig(d.Coef(), d.Scale()+6).String()
}
func finite(f float64) bool { return !math.IsNaN(f) && !math.IsInf(f, 0) }
// 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},
// ── Hanzo Decision (POST /v1/decisions) ─────
// Kai is half of Jev's $0.042 per million input tokens, and Jev — reached
// through OpenRouter under its vendor ids — is billed at that list price on the
// input tokens its answer reports. Output is free for both.
"kai": {InputPerMillion: 0.021},
"typesafe/jev-1.13": {InputPerMillion: 0.042},
"~typesafe/jev-latest": {InputPerMillion: 0.042},
// Zen answering a decision is a chat completion the decision service buys from
// Hanzo's router, so its output is priced: both legs at the published Zen chat
// row (zen5's), the two ids being served by one chain.
"zenlm/zen6": {InputPerMillion: 2.28, OutputPerMillion: 7.26},
"zenlm/zen6-flash": {InputPerMillion: 2.28, OutputPerMillion: 7.26},
}
// 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 SECOND heard, synthesis per CHARACTER spoken ─
//
// Two tables, not one, because the two directions are sold in different units:
// transcription by the length of the audio, synthesis by the length of the text.
// Collapsing them would force one of the two into a unit its market does not quote.
//
// THE RATES ARE THE PUBLISHED ONES: hanzo.ai/pricing lists Speech-to-Text at $0.006
// per minute and Text-to-Speech at $15 per 1M characters, and these tables are that
// list in nano-USD (1 USD = 1e9) — 100,000 per second and 15,000 per character. Nano
// because a dictated sentence is a few seconds, and a per-call cent would bill it at
// twelve times the published rate; the debit is exact (usageBilledUSD), so the
// quoted rate and the booked one are the same number. Sell prices, not COGS: speech
// runs on our own hardware, so the margin is the whole price (providerCostNano).
//
// Keyed by every id a caller can send that reaches the speech service — the SKU we
// list and the upstream ids that stay callable — so no spelling of the same model is
// cheaper than another. A name absent here meters its quantity and is flagged
// Unpriced by recordUnpriced, never billed at an invented rate.
var sttNanoPerSecond = map[string]int64{
"zen-scribe": 100_000,
"zen-scribe-mini": 100_000,
"parakeet": 100_000,
"whisper": 100_000,
"whisper-small": 100_000,
}
var ttsNanoPerChar = map[string]int64{
"zen-voice-mini": 15_000,
"kokoro": 15_000,
}
// sttCostNano prices a transcription from the seconds of audio it heard, rounding
// UP to the nano. No rate costs nothing (and the row is flagged Unpriced) rather
// than falling back to an invented floor: audio here is OUR service, so an absent
// rate is a decision not made, not a gap to paper over.
func sttCostNano(model string, seconds float64) int64 {
rate := sttNanoPerSecond[strings.ToLower(model)]
if seconds <= 0 || rate <= 0 {
return 0
}
return int64(math.Ceil(seconds * float64(rate)))
}
// ttsCostNano prices a synthesis from the characters it spoke. Same absent-rate
// rule as sttCostNano.
func ttsCostNano(model string, chars int) int64 {
rate := ttsNanoPerChar[strings.ToLower(model)]
if chars <= 0 || rate <= 0 {
return 0
}
return int64(chars) * rate
}
// 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)
return seconds > 0 && sttNanoPerSecond[m] > 0 || chars > 0 && ttsNanoPerChar[m] > 0
}
// 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
}
// audioCostNano 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 audioCostNano(record *usageRecord) int64 {
return sttCostNano(record.Model, record.AudioSeconds) +
ttsCostNano(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)
}
// decisionCostNano is the billed price of a decision in nano-USD: its input and
// output tokens at the model's price, as every model is priced. Kai's and Jev's
// output is priced at zero, so their output tokens move nothing; an LLM model's
// completion, reasoning included, bills at its output price. A count is what the
// service reports, relayed from whatever answered it, so each is bounded: none below
// zero, output at the most a completion is asked for, input short of overflow.
func decisionCostNano(model string, inputTokens, outputTokens int) int64 {
return tokenCostNano(model, min(max(inputTokens, 0), decisionInputTokens),
min(max(outputTokens, 0), decisionOutputTokens), 0, 0)
}
// 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.
// A route to the decision service is priced by its own row, never by a family that
// claims the id by prefix or alias: zenlm/zen6 starts with "zen", and the Zen chat
// SKU it names lists at $0.
if !decisionRoute(model) {
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 second heard / per character spoken, not per token, so
// the token table cannot answer for it. The flag follows the audio rate table:
// an id with no rate is honestly Unpriced, and setting one prices the traffic
// without touching a single emit site.
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)
// A variable SKU bills what each call costs, whatever its rates read.
return ok && !p.Variable && 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
}
// Cache writes cost more than the input they store, by how long it is kept:
// Anthropic prices a five-minute write at 1.25x input and a one-hour write at 2x.
const (
cacheWriteMultiple = 1.25
cacheWriteHourMultiple = 2.0
)
// cacheWriteRate is the per-million price of a five-minute cache write: the model's
// own rate when it states one, else cacheWriteMultiple times its input rate. The
// one rule for the price path (modelCostBreakdown) and the nano ledger (tokenNanoAt).
func cacheWriteRate(inputPerM, writePerM float64) float64 {
if writePerM != 0 {
return writePerM
}
return inputPerM * cacheWriteMultiple
}
// modelCostBreakdown computes the per-component dollar cost of a token-billed call.
// promptTokens is every input token, cache reads and writes among them; Input prices
// the part read fresh.
// Cache-read defaults to 10% of input price (Anthropic parity) and cache-write to
// cacheWriteRate 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
}
cacheWriteRate := cacheWriteRate(price.InputPerMillion, price.CacheWritePerMillion)
b := costBreakdown{
Input: float64(fresh(promptTokens, cacheReadTokens, cacheWriteTokens)) * 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 and cache-write tokens at
// cacheWriteRate (matching Anthropic).
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
}