Fix model pricing by selecting best provider and deriving cache prices - #12
Merged
Merged
Conversation
models.dev 的同一个裸模型 ID 挂在 176 个 provider 下,原解析直接 pricingMap.set(modelId, cost) 一路覆盖,最后遍历到谁用谁:2652 个有价模型里 1606 个落到了没有 cache_read/cache_write 字段的二级经销商条目上,缓存 token 全部按 0 单价计费。一条 cache_read=54773 的请求只收 $0.000385。 - model-catalog: 新增 buildCatalogMapsFromModelsDev(),同模型多 provider 改为 按「官方 provider 优先 → 有真实价格优先 → 缓存字段完整优先」择优,并从基础 价格一致的条目回填缺失的缓存单价;对 cost/limit 做数值校验 - catalog-db: onConflictDoUpdate 的 set 引用的是已存在行的列(等于把旧值写回 自己),冲突时整行不更新,坏价格会被永久缓存;改为引用 excluded.* - pricing: 上游缺缓存单价时按官方比例从 input 推导兜底(读 0.1x / 写 1.25x / 1h 写 2x),并用 ephemeral_5m/1h 把缓存写入按 TTL 分档计费;CostBreakdown 暴露实际计费单价与 TTL 分档 token 数 - console-store: 用量聚合与时间序列补上 ephemeral_5m/1h 字段 - console: Models 页新增缓存读/写价格列,成本公式改用实际计费单价并拆分 5m/1h Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011a12zJk3jbzU6orYUtiD4X
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a critical issue where model pricing data was being incorrectly overwritten by secondary resellers, resulting in missing cache pricing information. The fix implements intelligent provider selection, cache price backfilling, and fallback pricing derivation across the entire pricing pipeline.
Root Cause
The
models.devAPI exposes the same model ID (e.g.,claude-opus-4-6) across 170+ providers. The previous implementation simply overwrote pricing with each provider encountered, meaning the final pricing came from whichever provider was last in the iteration order. This frequently resulted in secondary resellers' incomplete pricing (missingcache_read/cache_writefields) overwriting official provider data, causing cache operations to be billed at $0.Key Changes
Model Catalog (src/model-catalog.ts)
buildCatalogMapsFromModelsDev()function: Replaces simple overwriting with intelligent provider selectioncache_read/cache_writefrom other providers with identical base pricingPricing Calculation (src/pricing.ts)
resolveEffectiveCachePricing()function: Derives cache prices when upstream data is incompletecache_pricing_derivedflag for transparencycache_creation_input_tokensinto 5m and 1h buckets using newephemeral_5m_input_tokens/ephemeral_1h_input_tokensfieldsCostBreakdown: Addscache_write_5m_tokens,cache_write_1h_tokens, and actual billing prices (cache_read_price,cache_write_5m_price,cache_write_1h_price)Database (src/catalog-db.ts)
onConflictDoUpdateto referenceexcluded.*(new values) instead of existing columns, ensuring refresh updates actually persistConsole Frontend
ephemeral_5m/1hfields for consistent cost reportingImpact
For a typical high-cache-hit request with
claude-opus-4-6:Affected ~1,606 of 2,652 models that were falling back to incomplete secondary reseller pricing.
Testing
Added comprehensive test suite (
test/model-catalog-pricing.test.ts) covering:https://claude.ai/code/session_011a12zJk3jbzU6orYUtiD4X