Files
magpie/internal/catalog/peak.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

96 lines
3.5 KiB
Go

package catalog
import "time"
// Peak names the hours a list price is the off-peak price of: a call made
// in them is billed at PeakFactor times it. DeepSeek bills its models at
// half price off-peak, and models.dev lists that half as the price, with a
// multiplier for the peak hours its api.json leaves out
// (providers/deepseek/models/deepseek-flash.toml: "Base prices below are
// off-peak; the multiplier doubles them at peak"). Empty: the price holds
// at every hour.
type Peak string
const (
// PeakDeepSeek is DeepSeek's own: 01:00 to 04:00 and 06:00 to 10:00
// UTC, Monday to Friday, never on a Chinese public holiday
// (https://api-docs.deepseek.com/quick_start/pricing)
PeakDeepSeek Peak = "deepseek"
// PeakWeekdays is the same hours on every weekday, holidays too: what
// Command Code (https://commandcode.ai/docs/resources/pricing-limits)
// and OpenCode Go (https://opencode.ai/docs/go) bill DeepSeek's models
// at, neither of which names a holiday
PeakWeekdays Peak = "weekdays"
)
// PeakFactor is what a call in the peak hours costs, times the off-peak
// list price: DeepSeek's off-peak rates are half its peak rates.
const PeakFactor = 2
// At is whether a call made at t is in the peak hours. A zero time, a
// call whose time isn't known, is not.
func (k Peak) At(t time.Time) bool {
if k == "" || t.IsZero() {
return false
}
u := t.UTC()
if wd := u.Weekday(); wd == time.Saturday || wd == time.Sunday {
return false
}
if h := u.Hour(); !(h >= 1 && h < 4 || h >= 6 && h < 10) {
return false
}
// the peak hours are 09:00 to 18:00 in China, so a holiday's date is
// the same in UTC
return k != PeakDeepSeek || !cnHoliday(u)
}
// In is the price a call made at t is billed at: PeakFactor times this one
// in its peak hours, else this one. The price it gives holds at every hour.
func (p Price) In(t time.Time) Price {
if !p.Peak.At(t) {
p.Peak = ""
return p
}
p = p.Times(PeakFactor)
p.Peak = ""
return p
}
// cnHolidays are the days off of China's public holidays, as the State
// Council's notice for each year lists them (a weekend day inside a break
// is a day off already, and a weekend day worked in exchange stays a
// weekend day for DeepSeek, whose peak is Monday to Friday). It needs a
// year added each November, when the notice for the next year comes out:
// TestCNHolidaysCoverThisYear fails in a year it doesn't have. A year it
// doesn't have counts no holiday, so its weekdays are all at peak hours'
// price, the most a call could have cost.
var cnHolidays = map[int][]string{
// 国务院办公厅关于2026年部分节假日安排的通知, 2025-11-04
// (https://www.news.cn/politics/20251104/88bcffd88ae249e58c699b8772548e3d/c.html)
2026: {
"01-01", "01-02", "01-03", // 元旦
"02-15", "02-16", "02-17", "02-18", "02-19", "02-20", "02-21", "02-22", "02-23", // 春节
"04-04", "04-05", "04-06", // 清明节
"05-01", "05-02", "05-03", "05-04", "05-05", // 劳动节
"06-19", "06-20", "06-21", // 端午节
"09-25", "09-26", "09-27", // 中秋节
"10-01", "10-02", "10-03", "10-04", "10-05", "10-06", "10-07", // 国庆节
},
}
// cnHoliday is whether t's date, in China, is a day off of a public holiday.
func cnHoliday(t time.Time) bool {
d := t.In(chinaTime).Format("01-02")
for _, h := range cnHolidays[t.In(chinaTime).Year()] {
if h == d {
return true
}
}
return false
}
// chinaTime is China's time, UTC+8 all year: a fixed zone, so it doesn't
// depend on the system's zone database (Windows has none Go can read).
var chinaTime = time.FixedZone("CST", 8*60*60)