Repository navigation
fix(pricing): carry the chat platform fee into local cost estimates (PF-8) - #622
Conversation
…stimates (PF-8) blockrun.ai and sol.blockrun.ai bill per-token paid chat at the list rates plus a platform fee, published as platform_fee_percent on each /v1/models row (inside pricing on both, and at the row's top level on base). ClawRouter read only pricing.input/output, so the x402 estimate behind the balance preflight, the pre-auth and the per-attempt maxCostPerRun check, and the API-key rail's logged cost, all undershot by the fee. - platform-fee.ts: platformFeePercentOf(row) reads pricing first, then the top level; paid rows only; 0 when absent or malformed (older gateways and enterprise PF-9 send none). withPlatformFee() applies it. - model-catalog.ts: the live projection stores it as platformFeePercent on the model (and in the change fingerprint). The bundled list carries none. - proxy.ts estimateAmount: token cost x fee, then the existing $0.001 transaction fee; flat/promo prices untouched. - proxy.ts API-key rail usage cost: list x fee from that gateway's catalog (api.blockrun.ai publishes 0 today, so this stays list price there).
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Why
blockrun.ai and sol.blockrun.ai bill per-token paid chat at the vendor list rates plus a platform fee (BlockRunAI/blockrun
specs/2026-11-16-chat-platform-fee.md, PF-1..PF-9). The fee is published asplatform_fee_percenton each/v1/modelschat row: insidepricingon both gateways, and also at the row's top level on base (blockrun#1142). PF-8 says ClawRouter's estimator has to read it; it read onlypricing.input/pricing.output, so its estimates undershot the 402 quote by the fee.What changes
src/platform-fee.ts(new):platformFeePercentOf(row)readspricing.platform_fee_percent, falls back to the top-level field, and returns 0 when neither is a finite non-negative number. Onlybilling_mode: "paid"rows; free, flat and media rows always get 0.withPlatformFee(cost, percent)applies× (1 + percent/100).src/model-catalog.ts: the live catalog projection stores the fee on the model asplatformFeePercent(cleared on each refresh, so a gateway that stops publishing it drops it), and includes it in the change fingerprint.src/models.ts: optionalplatformFeePercentonBlockRunModel. The bundled list never sets it, so the fee is never a typed number.src/proxy.tsestimateAmount: per-token cost × fee, then the existing $0.001 transaction fee (unchanged, not multiplied), then the existing 20% buffer and $0.001 floor. Flat and promo prices are final and untouched. This feeds the balance preflight, payment pre-auth and the per-attemptmaxCostPerRunchecks.src/proxy.tsAPI-key rail usage cost: list cost × the fee that gateway's catalog publishes. api.blockrun.ai publishesplatform_fee_percent: 0today (checked with a free read), so it still logs list price there.Not changed, on purpose
calculateModelCost(router decisions,costEstimate/baselineCost/savings, thex-clawrouter-costbreakdown total, the wallet-rail usage fallback) lives in@blockrun/router-core, not this repo. It already multiplies by a hard-codedSERVER_MARGIN_PERCENT = 5, so it does not read the field. It also applies that 5% to flat/promo prices, and its Opus baseline carries no fee, sosavingscompares a fee-inclusive cost with a fee-free baseline. Fixing those belongs in router-core; I did not fold the fee into theModelPricingrates here, because that would double-count on top of the hard-coded 5%.dist/not rebuilt.Tests
src/platform-fee.test.ts(17 tests): absent → 0; nested read; top-level fallback; nested wins (including an explicit nested 0); malformed values fall through; free/flat/media rows always 0;estimateAmountwith no fee / nested / both / top-level only; flat and free rows unaffected; fee dropped when the gateway stops publishing it; API-key rail usage cost against a local gateway publishing 0 and 5.npx vitest run --maxWorkers=3: 133 files passed, 1 skipped; 1444 tests passed, 1 skipped. (Unrestrictednpm testtimed out 3-5 unrelatedindex.*/auth.injectiontests at the 5s limit with load average ~110 on the machine; those files pass in isolation and with fewer workers.)npm run typecheck,npx eslint src/,npx prettier --check .,npm run build(+ dist smoke),node scripts/sync-brand-numbers.mjs --check: all clean.🤖 Generated with Claude Code
Summary by CodeRabbit