Skip to content

[PR] fix: price compression savings at the session's billed average, not the last model's list price - #7238

Open
ranxianglei wants to merge 2 commits into
masterfrom
2026-11-19_fix-savings-last-model-price
Open

ranxianglei wants to merge 2 commits into
masterfrom
2026-11-19_fix-savings-last-model-price

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

是什么问题

Web 用量统计面板的「压缩节省」卡片用最后一个请求所用模型的实时列表价(metadata.lastModel)去给整段历史 fold 账本定价,而「花费」卡片是按每个请求各自盖戳的价格逐请求累加的(跨模型精确)。多模型会话里,只要有一次请求切到更便宜的模型,就会把整段历史按新价重算,两张卡片因此互相矛盾:例如花费 $170.16(正确)对节省 $6.35(被便宜的 deepseek-flash cacheRead 0.003/Mtok 重定价),而按该会话实际计费均价应约 $370。

根因

  • 花费:billInputUsage/billOutputUsage(src/billing.ts:199/212)按每请求盖戳价累加进 session.stats.costUsd(stampBillingPrice 最新优先,src/billing.ts:157)——跨模型正确。
  • 节省:livePriceForSummary(src/web/sessions-data.ts:664)只从 metadata.lastModel 解析价格;lastModel 在每个入站请求都被写(src/server/handle.ts:3083);savedUsdProfileForSummary 在有 live 价时原样返回 {w:live.cacheWrite, r:live.cacheRead, q:live.output};savedUsdFromFolds(src/billing.ts:229)用该 profile 给整段历史 fold 定价。

修法

当会话有持久化花费账本(stats.costUsd)时,把 live 价格 profile 的价位校准到会话实际计费均价(cu.cacheRead/cachedTokens、cu.output/outputTokens),再给历史 fold 定价;保留 live 模型的 w:r:q 形状(只重锚价位)。单模型会话计费均价≈列表价,行为不变;无账本会话回退到原 live 价(与之前一致)。

  • 不做持久化格式变更(不引入 per-fold 盖戳价),改动收敛在派生视图层。
  • 两张卡片从此同源(都基于每请求计费账本)。

测试

新增两条回归测试(tests/billing-web.test.ts):

  1. 多模型会话:lastModel=便宜模型 → 断言节省按会话计费均价 {w:3.75,r:0.3,q:15} 定价(savedUsd=0.0647),而非便宜模型列表价(修复前为 −0.0019);花费账本 total=26.75 不变。
  2. 单模型会话:计费均价==列表价 → 校准为 no-op(rates 与 savedUsd 与修复前一致)。

已验证:无修复时多模型测试失败(savedUsd −0.0019),加修复后 21/21 全绿;typecheck + build 通过。

Closes #7237

…he last model's list price

The web 用量统计 panel's 压缩节省 (savings) card priced the whole
historical fold ledger at metadata.lastModel's live list price, while
the 花费 (spend) card accumulates per-request at each request's stamped
price. In a multi-model session, a single request to a cheaper model
re-priced the entire fold history by orders of magnitude, so the two
cards contradicted each other (e.g. spend $170.16 vs savings $6.35
where the session's actual billed average implied ~$370).

Fix: when the session has a persisted spend ledger (stats.costUsd),
calibrate the live price profile's LEVEL to the session's actual billed
average (cu.cacheRead/cachedTokens, cu.output/outputTokens) before
pricing the folds, keeping the live model's w:r:q SHAPE. A single-model
session's billed average ≈ its list price, so behavior is unchanged
there; ledger-less sessions fall back to the raw live tiers as before.

Closes #7237
@ranxianglei ranxianglei added bug Something isn't working dsh p2 scope/ui labels Nov 19, 2026
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-19_fix-savings-last-model-price (38bf75a)

Latest build — install this exact version

npm install -g billion-context@0.0.10918-pr.7238.dd06891

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
38bf75a billion-context@0.0.10918-pr.7238.dd06891 build
7d64301 billion-context@0.0.10916-pr.7238.0c1e9e3 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr7238.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei ranxianglei mentioned this pull request Nov 19, 2026
@ranxianglei ranxianglei changed the title fix: price compression savings at the session's billed average, not the last model's list price [PR] fix: price compression savings at the session's billed average, not the last model's list price Nov 19, 2026
The parenthetical 'spend/miss mirrors use livePrice — same tiers whenever
both' predates this PR: once a session's spend ledger exists, usdProf is
calibrated to the billed average and can differ from the raw livePrice
tiers. Restate what the two surfaces actually use.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

查重: 不重复。检索 open PR/issue:#2482(savings 展示功能,引入方,不同方向)、#3193(dryRun 预览,不同机制)均与本 PR 无关;无其他在途 PR 修 #7237。

标签: bug, p2, scope/ui(统计面板数值缺陷,面板域;数值错但核心功能可用,未达 p1)

CI(审查时实测): head 7d64301 的 119 项 check run = 12 success / 15 skipped / 3 in_progress,无红。我补了一个 comment-only commit(见下)后 CI 在新 head 重跑中。

结构化八问

  1. 是什么问题: 面板「压缩节省」卡片用最后一个请求所用模型的实时列表价给整段历史 fold 账本定价,而「花费」卡片按每请求盖戳价累加——多模型会话里切一次便宜模型,整段历史就按新价重算,两卡互相矛盾(花费 $170.16 对节省 $6.35,按实际计费均价应约 $370)。
  2. 什么原因导致的: savedUsdProfileForSummary(src/web/sessions-data.ts:694)只从 metadata.lastModel 的实时价表取 profile,而 lastModel 每个入站请求都被覆盖写(src/server/handle.ts:3083);savedUsdFromFolds(src/billing.ts:229)拿这个 profile 给整段历史 fold 定价。花费卡走的是每请求持久化账本 stats.costUsd——两卡数据源不同,多模型会话里「最后模型列表价」≠「会话实际计费价」。
  3. 影响范围与严重程度: 仅 web 面板节省卡(及其 rates/payback 分解)的美元展示;压缩机制、计费账本、wire 均不受影响。触发条件:会话有持久化花费账本 + lastModel 有 live 价 + cachedTokens>0;多模型会话失真最明显(最后模型越便宜,节省被低估越狠,可为负)。严重程度:展示失准(p2),无数据丢失。
  4. 怎么修复的: 派生视图层校准(不动持久化格式):有花费账本时,把 live profile 的价位重锚到会话实际计费均价(r = cu.cacheRead/cachedTokens,q = cu.output/outputTokens),保留 live 模型的 w:r:q 形状(w 按 live 的 write:output 比 × 校准后 q 推导;q 取不到时按 live 的 output:read 比缩放);无账本/取不到均价 → 回退原 live 价。两卡从此同源。
  5. 是否有回归问题: 无。逐项清点既有行为:花费卡未被触碰(测试断言 total=26.75 不变);无账本/无 cachedTokens 会话走回退路径(既有测试 web-money-1 有账本但 cachedTokens=0,仍走 raw live tier,绿);单模型会话计费均价≈列表价,校准 no-op(新测试钉死 rates deepEqual + savedUsd 与修复前一致);payback 分解与 savedUsdGross 同源于 usdProf,屏上 net == gross − spent 的恒等式按构造保持;overview 的 moneyRates 一致性逻辑结构未变。有意行为变更(已披露): 模型列表价在会话请求之后变过的单模型会话,节省卡改跟会话实际计费价(旧行为跟当前列表价)——新行为更准(fold 发生在当时计费价),但面板数字会可见地变。
  6. 是否破坏缓存: 不涉及。纯派生视图,每次 fetch 重算,无缓存/重试/游标/去重键。
  7. 是否改了设计方向: 否。收敛在派生视图层,不引入 per-fold 盖戳价,kernel 未动。
  8. 是否引入了新的配置: 否。无新 env/config/默认值。

测试有效性

  • 多模型测试是真回归测试:我在未修复的 master 代码上实测失败,断言错误为 savedUsd -0.0019(与 PR 描述一致),加修复后通过;断言覆盖花费账本不变、节省按计费均价 {w:3.75,r:0.3,q:15} 定价、便宜模型价不泄漏进 rates(deepEqual)、gross 跟校准后 r。
  • 单模型测试钉 no-op 边界;回退路径由既有测试隐式钉住。
  • 小缺口(不拦):q 回退子分支(output 均价取不到)与 w 回退子分支(live.output=0)无直接测试行。
  • 本地定向跑:tests/billing-web.test.ts 11/11 绿;typecheck 通过。

顺手修

src/web/sessions-data.ts:955 注释 "spend/miss mirrors use livePrice — same tiers whenever both" 在校准后与代码事实不符(usdProf 与 livePrice 可以不同)——已当场修正,commit 38bf75aa8a(comment-only,typecheck 过)。

判定: 可合(等新 head CI 全绿)。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG

1 participant