magpie icon indicating copy to clipboard operation
magpie copied to clipboard

[Bug] 价格变更会重算历史用量:WorkBuddy Step-5-Preview 限免结束后,免费期调用被按现价计费

Open MYCF711 opened this issue 1 month ago • 3 comments

版本

magpie 0.1.1978(Windows)

现象

WorkBuddy 的 Step-5-Preview 在 2026-10-12 00:00 ~ 2026-10-26 00:00(Asia/Shanghai) 是「独家限免」,WorkBuddy 插件价格表里对应促销为 discountedCredits: "0.00x"。限免结束后恢复收费(当前模型列表显示 0.33x)。

Usage 页 30 天账本里,限免期内(10/17 ~ 10/25)的 310 次 step-5-preview 调用被计成 $33.05:

  • input:21,827,158
  • output:150,230
  • cache_read:108,159,360
  • provider:session-unknown
  • model:step-5-preview
  • source:log

这 310 次全部落在限免期内,成本应该是 $0。验证:执行

magpie model price '*/step-5-preview' 0,0,0,0

后这 310 条立刻变成 cost: 0;执行 magpie model price '*/step-5-preview' --reset 后又全部回到 $33.05。说明 Usage 是按当前价格重算历史,而不是按调用发生时的价格。

影响

任何“限时免费 → 恢复收费”或“模型价格调整 / models.dev 目录更新”都会把历史调用按新价重算:

  • 免费期跑得越多,恢复收费当天 Usage 费用越“爆炸”,但并没有新增调用。
  • 历史账单无法和当时的真实扣费对上;session-unknown 会话日志尤其明显,因为 provider 未知,只能走 models.dev 的 maker price,完全不知道 WorkBuddy 的限免。
  • 这不是 #3898 的“免费模型显示 $0”那一面,而是 #3898 里提到的 “changed tariff re-prices history” 真的发生了。

根因定位

  • internal/usage/ledger.go:552-564:NewPricer 注释明确写 “make it again on the next read so a changed tariff re-prices history”;Pricer() 调用 provider.EffectivePriceIn(s, r.Provider, r.Model),没有把 r.Time 传进去。
  • internal/provider/models.go:1831-1879:ListPrice() 先查当前 models.dev catalog,再 pluginFree。所以即使 provider 是 WorkBuddy,只要 models.dev 给 step-5-preview 一个价,pluginFree 也不会生效;对 session-unknown 更是直接走 MakerPrice。
  • WorkBuddy 插件的促销有 schedule.validFrom/validUntil(10/12~10/26,discountedCredits: "0.00x"),但 Usage 的会话日志记录只有模型名,没有 provider/促销快照,事后无法重建当时价格。

期望

  1. 历史调用的成本应按调用时刻的有效价格计算,或直接在写入 Usage 记录时把当时价格/成本快照存下来;至少要有价格版本与 validFrom/validUntil,Pricer 用 r.Time 查对应版本。
  2. session-unknown 的历史行不能在 models.dev 新增/修改价格后被全量重定价。
  3. 价格变更(免费→收费、涨价、目录更新)只影响生效时间之后的调用。

复现

  1. WorkBuddy 限免期内产生 Step-5-Preview 调用(本机 10/17 ~ 10/25 共 310 次)。
  2. 限免结束后查看 Usage 30 天:这些调用显示 $33.05。
  3. magpie model price '*/step-5-preview' 0,0,0,0 → 全部 $0;magpie model price '*/step-5-preview' --reset → 又变回 $33.05。

补充:本机同一账本 30 天 DSH 费用因此从约 $269.72(¥1,807 @ 6.681)降到 $238.34(约 ¥1,592),差额正好是 Step-5 的 $33.05(其余是实时新增调用)。

MYCF711 avatar Oct 29 '26 03:10 MYCF711

补充一个更根本的修法(用户建议):把模型倍率做成带时间的历史,按调用时刻查倍率,而不是靠手动改价。

要点:

  • WorkBuddy 的 rate 是积分倍率,不是官方 API 价的倍率;免费就是 rate = 0,所以 rate * 0 天然为 0,不需要再改价格。
  • 每次登录 / 模型目录刷新 / 每日签到时,把 model -> rate 连同 validFrom / validUntil 快照下来(插件已经有 modelPromotions 的 schedule.validFrom/validUntil,可以直接用)。
  • Usage 计价时用 record.Time 查对应区间,而不是用当前 rate。这样 Step-5-Preview 在 10/12–10/26 是 0,10/26 后是 0.33,历史自动正确。
  • 网关调用优先用 record.credits(WorkBuddy 实际扣减的积分)× 积分单价,这比用倍率反推更准;只有 session-unknown 的会话日志没有 credits 时,才用 rate × 每 1x 的积分基准 × 积分单价 估算。
  • 注意不能直接 官方价 × rate:WorkBuddy 的 0.11x 直接乘官方价会把 DeepSeek Flash 算成约 $0.066/M 而不是约 $0.56/M,低一个数量级。倍率要乘的是积分基准/积分单价,不是 USD 牌价。

如果这个方向可行,核心需要的是价格/倍率的有效时间,而不是更多手动 magpie model price 覆盖;后者只能救单个模型,且会和后续真实调价冲突。

MYCF711 avatar Oct 29 '26 03:10 MYCF711

Taking your decision — price at the time the call was recorded — this is the smallest shape I can see that honours it, with the WorkBuddy case as the test.

What is wrong today. Pricing reads the model's current rate. When a promo window closes (Step-5-Preview free through 10-26, 0.33 after), every call made during the free window is re-priced at the new rate on the next usage read. The history is not wrong at write time — it is wrong every time it is read.

The pieces already exist

  • every usage row carries t (the call's own timestamp) — nothing new to record there
  • a plugin's magpie.modelPromotions already carries schedule.validFrom / schedule.validUntil — the window is declared, just not kept
  • WorkBuddy's rate is a credit multiplier, not a price multiplier: free is rate = 0, so rate × 0 = 0 is already correct once the right rate is looked up

Change

type ratePoint struct {
    Model string
    Rate  float64   // credit multiplier, not USD
    From  time.Time
    To    time.Time // zero = open
}

// rateAt is the rate in force at t — the call's own time, never now.
func rateAt(points []ratePoint, model string, t time.Time) (ratePoint, bool) {
    for _, p := range points {
        if p.Model != model { continue }
        if t.Before(p.From) { continue }
        if !p.To.IsZero() && t.After(p.To) { continue }
        return p, true
    }
    return ratePoint{}, false
}

Points are appended on every catalog refresh (login, model list, daily check-in) from the plugin's modelPromotions schedule; the previous open point gets To = validFrom of the new one. Pricing then asks rateAt(history, record.Model, record.T), and the upside is that no manual magpie model price override is needed — those can only rescue one model and fight the next real price change.

Two things to keep right

  • record.credits (what WorkBuddy actually charged) beats any reconstruction — use it where present, and the rate path only for session-unknown rows that have no credits
  • do not multiply the USD list price by rate: WorkBuddy's 0.11x on the official price puts DeepSeek Flash at ~$0.066/M instead of ~$0.56/M, an order of magnitude low. rate multiplies the credit base, not the dollar list.

If you want the concrete rows: this machine has calls from 10-12 to 10-26 on that model now showing the post-window rate; I can paste a before/after table once the lookup exists. No PR from me (CreatePullRequest is refused for non-collaborators) — this is the patch in prose.

MYCF711 avatar Nov 04 '26 12:11 MYCF711

I went through this against the code. Decision: no time-segmented rate history in this round, for the reasons below. What I'd take instead is at the end.

What the 310 rows actually are. They are session-unknown rows: read from a session log, with no provider. Usage gives them the API-equivalent value at the model maker's list price. models.dev lists step-5-preview at $1/M input and $2.7/M output. 21.8M input, 0.15M output and 108M cache reads at that price come to the $33.05 you see. That value never depended on WorkBuddy's window: the same rows read $33.05 inside the free window as well. Usage didn't re-price them when the promotion ended; WorkBuddy's promotion never reached them at all.

Why a rate history wouldn't fix these rows.

  • The rows have no provider, so applying WorkBuddy's 10-12 to 10-26 window by model name would also make another vendor's step-5-preview calls free in that window. That puts a vendor's rule on calls it didn't serve (we've been bitten by this before: a rule scoped to one vendor reached every model).
  • rate × the USD list price is wrong, as you say. record.credits × a credit unit price needs that unit price, which magpie doesn't have: no plugin reports one, and Usage values calls in USD as an API equivalent, not as what the subscription charged.
  • The WorkBuddy plugin hands magpie a running promotion's rate/rateWas only, not its validFrom/validUntil, so nothing could be recorded yet even for gateway calls.

What stays true from the report. A models.dev catalog change re-prices history on the next read. That is by design today (NewPricer), and freezing a row's price when it is written is a real request, but it is a larger change to the ledger. The "free models show $0" side is #3898.

What I'd take, scoped to the vendor:

  1. The plugin passes the promotion's validFrom/validUntil with rate.
  2. magpie records the window per provider.
  3. Usage applies it only to rows whose provider is that plugin's: gateway calls, never session-unknown.

If you want to drive it, that is plugins-side first (WorkBuddy's priceOf), then the host and the ledger. For your own history today, magpie model price '*/step-5-preview' 0,0,0,0 is the workaround you found, with the catch you named.

Leaving this open.

yetone avatar Nov 04 '26 14:11 yetone