mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-12-03 12:32:02 +00:00
feat(pricing): automatic gap-fill from models.dev and OpenRouter (#457)
Keep model pricing automatic instead of hand-coding new models. The bundler now layers three sources in priority order: LiteLLM (broad list prices), hand-curated MANUAL_ENTRIES overrides, then a separate last-resort fallback file gap-filled from models.dev first-party makers (official direct prices) and OpenRouter (resale backstop). New models such as MiniMax-M3 ($0.6/$2.4) now price correctly with no per-model code. The fallback is written to its own pricing-fallback.json and consulted only case-insensitively as the final step in getModelCosts, so a reseller variant name can never shadow a canonical or aliased match. Fixes surfaced while building and verifying this: - Alias precedence: LiteLLM ships snowflake/claude-4-opus ($5), which the bundler strips to a bare claude-4-opus key that shadowed the curated alias to claude-opus-4 ($15 official). An explicit alias for a bare name now wins over a coincidental stripped reseller key; the prefixed gateway price is still returned for the fully-qualified id. - Zero-stub guard: LiteLLM [0,0] price stubs (e.g. GigaChat-2-Max) are excluded from the case-insensitive index so a case-mismatched query stays null and keeps firing the unknown-model warning instead of silently reporting $0. - Negative-sentinel guard: OpenRouter returns -1 for variable/BYOK-priced models. The bundler now rejects any non-positive rate pair (and strips the sentinel from cache fields) so a negative per-token cost can never ship and subtract from spend totals. Bundler hardening: bareKey strips @pin and date suffixes to match the runtime canonical form, seen-set dedupes on both full and bare key shapes, and it logs MANUAL_ENTRIES now covered upstream plus models.dev allowlist drift. Extracted buildCosts so the cache-cost heuristics live in one place. Added a data-hygiene test that fails CI if a rebundle reintroduces negative, free, or unreachable fallback entries.
This commit is contained in:
parent
c36f3afa76
commit
a385f65dee
7 changed files with 312 additions and 28 deletions
File diff suppressed because one or more lines are too long
1
src/data/pricing-fallback.json
Normal file
1
src/data/pricing-fallback.json
Normal file
File diff suppressed because one or more lines are too long
119
src/models.ts
119
src/models.ts
|
|
@ -2,6 +2,7 @@ import { readFile, writeFile, mkdir } from 'fs/promises'
|
|||
import { join } from 'path'
|
||||
import { homedir } from 'os'
|
||||
import snapshotData from './data/litellm-snapshot.json'
|
||||
import fallbackData from './data/pricing-fallback.json'
|
||||
import { fetchWithTimeout } from './fetch-utils.js'
|
||||
|
||||
export type ModelCosts = {
|
||||
|
|
@ -31,18 +32,36 @@ const CACHE_TTL_MS = 24 * 60 * 60 * 1000
|
|||
const WEB_SEARCH_COST = 0.01
|
||||
const ONE_HOUR_CACHE_WRITE_MULTIPLIER_FROM_FIVE_MINUTE_RATE = 1.6
|
||||
|
||||
// Assemble a ModelCosts, applying the cache-cost heuristics (write = 1.25x
|
||||
// input, read = 0.1x input) when a source omits them. Shared by the bundled
|
||||
// tuple path (tupleToCosts) and the live LiteLLM path (parseLiteLLMEntry) so the
|
||||
// multipliers live in exactly one place.
|
||||
function buildCosts(
|
||||
input: number,
|
||||
output: number,
|
||||
cacheWrite: number | null | undefined,
|
||||
cacheRead: number | null | undefined,
|
||||
fast: number | null | undefined,
|
||||
): ModelCosts {
|
||||
return {
|
||||
inputCostPerToken: input,
|
||||
outputCostPerToken: output,
|
||||
cacheWriteCostPerToken: cacheWrite ?? input * 1.25,
|
||||
cacheReadCostPerToken: cacheRead ?? input * 0.1,
|
||||
webSearchCostPerRequest: WEB_SEARCH_COST,
|
||||
fastMultiplier: fast ?? 1,
|
||||
}
|
||||
}
|
||||
|
||||
function tupleToCosts(raw: SnapshotEntry): ModelCosts {
|
||||
const [input, output, cacheWrite, cacheRead, fast] = raw
|
||||
return buildCosts(input, output, cacheWrite, cacheRead, fast)
|
||||
}
|
||||
|
||||
function loadSnapshot(): Map<string, ModelCosts> {
|
||||
const map = new Map<string, ModelCosts>()
|
||||
for (const [name, raw] of Object.entries(snapshotData as unknown as Record<string, SnapshotEntry>)) {
|
||||
const [input, output, cacheWrite, cacheRead, fast] = raw
|
||||
map.set(name, {
|
||||
inputCostPerToken: input,
|
||||
outputCostPerToken: output,
|
||||
cacheWriteCostPerToken: cacheWrite ?? input * 1.25,
|
||||
cacheReadCostPerToken: cacheRead ?? input * 0.1,
|
||||
webSearchCostPerRequest: WEB_SEARCH_COST,
|
||||
fastMultiplier: fast ?? 1,
|
||||
})
|
||||
map.set(name, tupleToCosts(raw))
|
||||
}
|
||||
// TEMP (2026-06-09): Fable 5 / Mythos 5 launch pricing, $10/M input and $50/M output,
|
||||
// until LiteLLM indexes them. Added as snapshot fallbacks so a real LiteLLM entry, once
|
||||
|
|
@ -61,8 +80,21 @@ function loadSnapshot(): Map<string, ModelCosts> {
|
|||
return map
|
||||
}
|
||||
|
||||
// Gap-fill pricing from models.dev / OpenRouter, keyed lowercase. Consulted ONLY
|
||||
// as the last-resort fallback in getModelCosts (never for exact/canonical/prefix
|
||||
// matches), so a reseller variant name can't shadow a real canonical entry.
|
||||
const fallbackCosts: Map<string, ModelCosts> = (() => {
|
||||
const map = new Map<string, ModelCosts>()
|
||||
for (const [name, raw] of Object.entries(fallbackData as unknown as Record<string, SnapshotEntry>)) {
|
||||
const lk = name.toLowerCase()
|
||||
if (!map.has(lk)) map.set(lk, tupleToCosts(raw))
|
||||
}
|
||||
return map
|
||||
})()
|
||||
|
||||
let pricingCache: Map<string, ModelCosts> = loadSnapshot()
|
||||
let sortedPricingKeys: string[] | null = null
|
||||
let lowercasePricingIndex: Map<string, ModelCosts> | null = null
|
||||
|
||||
function getSortedPricingKeys(): string[] {
|
||||
if (sortedPricingKeys === null) {
|
||||
|
|
@ -71,6 +103,33 @@ function getSortedPricingKeys(): string[] {
|
|||
return sortedPricingKeys
|
||||
}
|
||||
|
||||
// Case-insensitive index, built lazily. Lets a session model like `MiniMax-M3`
|
||||
// resolve to a gap-filled OpenRouter key like `minimax-m3` (lowercase slug).
|
||||
// First key wins on a lowercase collision so it stays deterministic.
|
||||
//
|
||||
// Zero-priced entries are excluded: LiteLLM ships `[0,0]` stubs (e.g.
|
||||
// `GigaChat-2-Max`) for models it lists but has no price for. Indexing those
|
||||
// would let a case-mismatched query (`gigachat-2-max`) resolve to a silent $0
|
||||
// instead of returning null, which suppresses the unknown-model warning and
|
||||
// hides real spend. A case-EXACT query still finds the stub via the normal
|
||||
// pipeline; only the fuzzy case-insensitive path skips them.
|
||||
function getLowercasePricingIndex(): Map<string, ModelCosts> {
|
||||
if (lowercasePricingIndex === null) {
|
||||
lowercasePricingIndex = new Map()
|
||||
const priced = (c: ModelCosts) => c.inputCostPerToken > 0 || c.outputCostPerToken > 0
|
||||
// The live pricing data wins on any lowercase collision; the gap-fill only
|
||||
// fills names that resolve to nothing through the normal pipeline.
|
||||
for (const [key, costs] of pricingCache) {
|
||||
const lk = key.toLowerCase()
|
||||
if (priced(costs) && !lowercasePricingIndex.has(lk)) lowercasePricingIndex.set(lk, costs)
|
||||
}
|
||||
for (const [lk, costs] of fallbackCosts) {
|
||||
if (priced(costs) && !lowercasePricingIndex.has(lk)) lowercasePricingIndex.set(lk, costs)
|
||||
}
|
||||
}
|
||||
return lowercasePricingIndex
|
||||
}
|
||||
|
||||
function getCacheDir(): string {
|
||||
if (process.env['CODEBURN_CACHE_DIR']) return process.env['CODEBURN_CACHE_DIR']
|
||||
return join(homedir(), '.cache', 'codeburn')
|
||||
|
|
@ -96,16 +155,13 @@ function parseLiteLLMEntry(entry: LiteLLMEntry): ModelCosts | null {
|
|||
const inputCost = safePerTokenRate(entry.input_cost_per_token)
|
||||
const outputCost = safePerTokenRate(entry.output_cost_per_token)
|
||||
if (inputCost === null || outputCost === null) return null
|
||||
const cacheWrite = safePerTokenRate(entry.cache_creation_input_token_cost) ?? inputCost * 1.25
|
||||
const cacheRead = safePerTokenRate(entry.cache_read_input_token_cost) ?? inputCost * 0.1
|
||||
return {
|
||||
inputCostPerToken: inputCost,
|
||||
outputCostPerToken: outputCost,
|
||||
cacheWriteCostPerToken: cacheWrite,
|
||||
cacheReadCostPerToken: cacheRead,
|
||||
webSearchCostPerRequest: WEB_SEARCH_COST,
|
||||
fastMultiplier: entry.provider_specific_entry?.fast ?? 1,
|
||||
}
|
||||
return buildCosts(
|
||||
inputCost,
|
||||
outputCost,
|
||||
safePerTokenRate(entry.cache_creation_input_token_cost),
|
||||
safePerTokenRate(entry.cache_read_input_token_cost),
|
||||
entry.provider_specific_entry?.fast,
|
||||
)
|
||||
}
|
||||
|
||||
async function fetchAndCachePricing(): Promise<Map<string, ModelCosts>> {
|
||||
|
|
@ -161,12 +217,14 @@ export async function loadPricing(): Promise<void> {
|
|||
if (cached) {
|
||||
pricingCache = mergeSnapshotFallbacks(cached)
|
||||
sortedPricingKeys = null
|
||||
lowercasePricingIndex = null
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
pricingCache = mergeSnapshotFallbacks(await fetchAndCachePricing())
|
||||
sortedPricingKeys = null
|
||||
lowercasePricingIndex = null
|
||||
} catch {
|
||||
// snapshot already loaded at init; nothing more to do
|
||||
}
|
||||
|
|
@ -365,9 +423,20 @@ function getCanonicalName(model: string): string {
|
|||
export function getModelCosts(model: string): ModelCosts | null {
|
||||
// Try with provider prefix preserved (azure/gpt-5.4, openrouter/anthropic/claude-opus-4.6)
|
||||
const withPrefix = model.replace(/@.*$/, '').replace(/-\d{8}$/, '')
|
||||
const canonicalName = getCanonicalName(model)
|
||||
const canonical = resolveAlias(canonicalName)
|
||||
|
||||
// An explicit alias for a bare (un-prefixed) model name is authoritative: it
|
||||
// must win over a coincidental stripped reseller key of the same name. LiteLLM
|
||||
// ships `snowflake/claude-4-opus` ($5), which the bundler strips to a bare
|
||||
// `claude-4-opus` key; without this, that would shadow the curated alias
|
||||
// `claude-4-opus -> claude-opus-4` ($15 official Anthropic price).
|
||||
if (canonical !== canonicalName && withPrefix === canonicalName && pricingCache.has(canonical)) {
|
||||
return pricingCache.get(canonical)!
|
||||
}
|
||||
|
||||
if (pricingCache.has(withPrefix)) return pricingCache.get(withPrefix)!
|
||||
|
||||
const canonical = resolveAlias(getCanonicalName(model))
|
||||
if (pricingCache.has(canonical)) return pricingCache.get(canonical)!
|
||||
|
||||
// Iterate keys longest-first so a model id like `gpt-5-mini` matches the
|
||||
|
|
@ -379,6 +448,16 @@ export function getModelCosts(model: string): ModelCosts | null {
|
|||
}
|
||||
}
|
||||
|
||||
// Case-insensitive fallback: gap-filled keys from OpenRouter are lowercase
|
||||
// slugs (e.g. `minimax-m3`), but sessions report `MiniMax-M3`. Only consulted
|
||||
// after the exact/canonical/prefix attempts, so it never changes a match that
|
||||
// already resolved above.
|
||||
const lowerIndex = getLowercasePricingIndex()
|
||||
const byCanonical = lowerIndex.get(canonical.toLowerCase())
|
||||
if (byCanonical) return byCanonical
|
||||
const byPrefix = lowerIndex.get(withPrefix.toLowerCase())
|
||||
if (byPrefix) return byPrefix
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue