Files
magpie/internal/usage/peak_cost_test.go
T
yetone 643b525469 usage: a DeepSeek call at peak hours costs double its off-peak list price
treeMan on Discord: DeepSeek bills off-peak at half the peak rate, and the
Usage page didn't follow it. The catalog's list prices for these models
are already the off-peak ones: models.dev's DeepSeek, Command Code and
OpenCode Go entries give the off-peak rate as the price and a factor-2
multiplier for peak hours, and api.json leaves the multiplier out
(deepseek-flash: $0.15/$0.60/$0.003, DeepSeek's page says $0.30/$1.20/
$0.006 at peak). So every peak call was counted at half its cost. Now a
call made from 01:00 to 04:00 or 06:00 to 10:00 UTC, Monday to Friday,
costs twice the list price. The call's own timestamp decides.

Which providers (provider.PeakOf, by base URL, or by preset/id only for a
provider with no URL, such as a plugin's or a removed one):
- DeepSeek's API (api.deepseek.com), every model, except on Chinese
  public holidays (catalog.PeakDeepSeek);
- Command Code (api.commandcode.ai, the commandcode preset and the
  commandcode-plan plugin): deepseek-v4.1-flash, -v4.1-flash-fast,
  -v4-flash, -v4-flash-vision-exp and -v4-pro, every weekday, since its
  docs name no holiday. deepseek-v4-flash-fast is one price at every hour;
- OpenCode Go (opencode.ai/zen/go): deepseek-v4.1-flash and
  deepseek-v4-pro, every weekday.
Unchanged: OpenCode Zen (a flat $0.30/$1.20), ZenMux, Cline Pass,
OrcaRouter and every other relay, a price the user set (ModelPrices
returns before a Peak is set), a "same as" price and every other model.
The provider's price rate still applies.

Holidays: catalog.cnHolidays has 2026 from the State Council's notice
(2025-11-04). TestCNHolidaysCoverThisYear fails on 2027-01-01 unless that
year's notice, due in November, is added. A year missing from the table
counts no holidays.

Where the hour is applied (catalog.Price.In): Record.CostAt (Usage's
totals and groups), the packed gateway blocks (priceBlock), OTLP's
exported cost and budget's estimate (now). The Sessions page sums per
model without per-call times and stays at the list price. The model
editor's price box says when a list price counts double (four languages;
app.js kept under its budget by shortening, not by a raise).

Run at the tree pushed (parent 32f5c3369), on mote:
- gofmt -l clean
- go vet on the six packages
- go test of catalog, usage and otlp whole, and provider and gui -run
  Peak|PriceIn|CNHolidays|Price|TestCoreScriptBudget, with TZ=UTC and
  with TZ=Asia/Shanghai: all ok
At 76b18f68d (the same change before the last two rebases), on mote:
- go vet ./internal/..., GOOS=linux -tags nogui and GOOS=windows vet of
  ./..., and the darwin, linux nogui and windows builds: ok
- catalog, provider, usage and otlp whole, in both zones: ok
- internal/gui: TestCoreScriptBudget was red (app.js 108 bytes over),
  fixed by shortening the hint. Ten AppKit window tests were killed at
  their 30s bar at load ~130 (TestDockJoinLeadsCommandTab,
  TestMainWindowCorners and others); this change doesn't touch them.
GUI:
- model-price-edit (now checks both peak hints and none on a plain list
  price), model-price-tiers, provider-price-rate and i18n-keys: Chromium
  ok on mote.
- WebKit on mote couldn't launch at all (browserType.launch timed out at
  180s, 69 WebKit processes from other runs), so the three price files ran
  in WebKit on Linux (wsl-run): 8 of 8 ok.
- The whole GUI suite (both engines) at ff23724f3 was still running at
  load 110 to 150 when this was pushed, 119 red of ~7200. Every file
  checked so far is a timeout under load or in #4304 / #3476.
  model-chip-steady is red the same way on origin/main 0911a65e7.

Broken by hand, each red:
- CostAt without .In(r.Time): TestCostAtTheCallsHour (0.753 at peak,
  want 1.506)
- priceBlock without .In(p.Time): its block half
- Peak.At ignoring holidays: TestPeakHours on 2026-10-05
- priceOf never setting Peak: TestPriceOfPeak
- OpenCode Go's model list dropped: TestPeakOf (kimi-k3 "weekdays")

Not tried: a real call to DeepSeek, Command Code or OpenCode Go at peak
and off-peak to compare against their bills; a full go test ./...; the
holiday rule's handling of the swapped working weekends (DeepSeek says
Monday to Friday, so a worked Saturday stays off-peak here); ZenMux,
whose own docs give no schedule.
2026-10-30 15:22:55 +08:00

78 lines
2.7 KiB
Go

package usage
import (
"math"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// A call to a DeepSeek model billed by the hour is costed at the hour it
// was made, not the hour the page is read: double its off-peak list price
// at peak (Thursday 02:00 UTC), the list price off-peak (11:00) and on a
// weekend. A call to another model at those same times costs what it did,
// and so does one at a price the user set (no Peak). Both the record's
// cost (Totals, Groups) and a packed gateway block's are.
func TestCostAtTheCallsHour(t *testing.T) {
flash := catalog.Price{Input: 0.15, Output: 0.6, CacheRead: 0.003, Peak: catalog.PeakDeepSeek}
other := catalog.Price{Input: 2, Output: 8, CacheRead: 0.2}
peak := time.Date(2026, 10, 29, 2, 0, 0, 0, time.UTC)
off := time.Date(2026, 10, 29, 11, 0, 0, 0, time.UTC)
weekend := time.Date(2026, 10, 31, 2, 0, 0, 0, time.UTC)
near := func(a, b float64) bool { return math.Abs(a-b) < 1e-12 }
rec := func(at time.Time) Record {
return Record{Time: at, Input: 1_000_000, Output: 1_000_000, CacheRead: 1_000_000}
}
base := 0.15 + 0.6 + 0.003
for _, c := range []struct {
at time.Time
want float64
}{{peak, 2 * base}, {off, base}, {weekend, base}, {time.Time{}, base}} {
if got := rec(c.at).CostAt(flash); !near(got, c.want) {
t.Errorf("deepseek at %v: %v, want %v", c.at, got, c.want)
}
if got := rec(c.at).CostAt(other); !near(got, 10.2) {
t.Errorf("other at %v: %v, want 10.2", c.at, got)
}
set := flash
set.Peak = ""
if got := rec(c.at).CostAt(set); !near(got, base) {
t.Errorf("set price at %v: %v, want %v", c.at, got, base)
}
}
// a gateway block's rows, priced in one pass
var raw rowChunk
times := []time.Time{peak, off, weekend}
for i := 0; i < logBlockRows; i++ {
at := times[i%3]
raw.add(Row{Record: Record{Time: at, Provider: "deepseek", Model: "deepseek-flash", Input: 1_000_000, Output: 1_000_000}}, "", int64(i), false)
raw.add(Row{Record: Record{Time: at, Provider: "openai", Model: "gpt-6-astra", Input: 1_000_000, Output: 1_000_000}}, "", int64(i), false)
}
b := newBlockPricer(provider.Renames{}, nil, func(r Record) *catalog.Price {
if r.Provider == "deepseek" {
return &flash
}
return &other
})
c, _ := b.priceBlock(raw.pack())
if c == nil {
t.Fatal("no block")
}
for i := range c.Rows {
r := c.row(i)
want := 10.0
if r.Input == 1_000_000 && c.Strings[c.Shapes[c.Rows[i].Shape][1]] == "deepseek" {
want = 0.75
if r.Time.Equal(peak) {
want = 1.5
}
}
if !near(r.Cost, want) {
t.Fatalf("row %d (%s at %v): %v, want %v", i, c.Strings[c.Shapes[c.Rows[i].Shape][1]], r.Time, r.Cost, want)
}
}
}