Skip to content

fix(pricing): carry the chat platform fee into local cost estimates (PF-8) - #622

Merged
VickyXAI merged 1 commit into
mainfrom
feat/platform-fee-estimate
Nov 19, 2026
Merged

VickyXAI merged 1 commit into
mainfrom
feat/platform-fee-estimate

Conversation

@VickyXAI

@VickyXAI VickyXAI commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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 as platform_fee_percent on each /v1/models chat row: inside pricing on 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 only pricing.input / pricing.output, so its estimates undershot the 402 quote by the fee.

What changes

  • src/platform-fee.ts (new): platformFeePercentOf(row) reads pricing.platform_fee_percent, falls back to the top-level field, and returns 0 when neither is a finite non-negative number. Only billing_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 as platformFeePercent (cleared on each refresh, so a gateway that stops publishing it drops it), and includes it in the change fingerprint.
  • src/models.ts: optional platformFeePercent on BlockRunModel. The bundled list never sets it, so the fee is never a typed number.
  • src/proxy.ts estimateAmount: 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-attempt maxCostPerRun checks.
  • src/proxy.ts API-key rail usage cost: list cost × the fee that gateway's catalog publishes. api.blockrun.ai publishes platform_fee_percent: 0 today (checked with a free read), so it still logs list price there.

Not changed, on purpose

  • calculateModelCost (router decisions, costEstimate / baselineCost / savings, the x-clawrouter-cost breakdown total, the wallet-rail usage fallback) lives in @blockrun/router-core, not this repo. It already multiplies by a hard-coded SERVER_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, so savings compares a fee-inclusive cost with a fee-free baseline. Fixing those belongs in router-core; I did not fold the fee into the ModelPricing rates here, because that would double-count on top of the hard-coded 5%.
  • Before the first live catalog refresh the bundled model list is used, and it carries no fee field, so the estimate is fee-free (the absent-means-0 rule) until the first refresh lands (startup + every 5 minutes). The 20% buffer covers the difference.
  • Image/video/audio pricing tables are untouched (PF-5).
  • No version bump, no CHANGELOG entry (both happen at release), 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; estimateAmount with 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. (Unrestricted npm test timed out 3-5 unrelated index.* / auth.injection tests 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

  • New Features
    • Added support for gateway-published platform fees on eligible models. Fees are reflected in token-based cost estimates and API-key usage records; fixed and promotional prices remain unchanged.
    • Live model catalog updates now reflect changes to published platform fees.
  • Bug Fixes
    • Fees are ignored for free models and unsupported billing modes, and invalid fee values do not affect costs.

…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).
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The catalog now carries valid platform-fee percentages from live catalog rows. Token-based proxy estimates and logged API-key usage costs apply those fees. Fixed and promotional prices remain unchanged.

Changes: Platform Fee Calculation

Layer / File(s) Summary
Read and project catalog fees
src/models.ts, src/platform-fee.ts, src/model-catalog.ts, src/platform-fee.test.ts
BlockRunModel gains an optional platform-fee field. Fee extraction validates catalog values and uses the nested pricing value before the top-level value. Catalog projection sets positive fees, clears stale fees, and includes the fee in its fingerprint. Tests cover extraction, scaling, and catalog estimates.
Apply fees to proxy costs
src/proxy.ts, src/platform-fee.test.ts
Token-based estimates and API-key usage logging apply the model’s fee. Fixed and promotional prices bypass the fee calculation. Integration tests check logged costs with zero and nonzero published fees.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

  • BlockRunAI/ClawRouter#413: Adds the live catalog refresh path that supplies the pricing rows from which this change reads platform fees.

Suggested reviewers: killerqueen-z

Merge Risk: Minimal · up to 16da6

This change applies published platform fees to token-based cost estimates and API-key usage costs. Fixed and promotional prices stay unchanged, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: local per-token cost estimates now include the chat platform fee published on paid model rows. The title follows the conventional commit format and includes the ticket…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: local per-token cost estimates now include the chat platform fee published on paid model rows. The title follows the conventional commit format and includes the ticket reference PF-8. The raw summary, the objectives, and the title all describe the same change, and the title does not describe anything absent from the changeset.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 585f6e0 and 16da685.

Files selected for processing: 5

  • src/model-catalog.ts
  • src/models.ts
  • src/platform-fee.test.ts
  • src/platform-fee.ts
  • src/proxy.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@VickyXAI
VickyXAI merged commit fa06392 into main Nov 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant