Files
magpie/internal/usage/kimi_code_cost_test.go
T
hlyx1 2b067434d7 provider: a Kimi Code membership's k3.1-pro and k3.1-pro-highspeed are priced at the Kimi API's (#4635)
hlyx1's fix (#4635, branch hlyx1/magpie kimi-code-k31-pro-price), asked
to be expedited by matrix0487 on Discord. Kimi Code's plans serve
k3.1-pro and k3.1-pro-highspeed, which models.dev's kimi-code-plan-*
catalogs list at the membership's $0. Neither id was in kimiCodeAPI, so
a membership's call on either was unpriced on Usage. They now map to
kimi-k3.1-pro and kimi-k3.1-pro-highspeed, like k3 to kimi-k3, and the
Requests list names that model (PricedNameFor).

Checked: Kimi Code's own /models answer (internal/catalog/testdata/
kimi-code-cn-models-2026-10-31.json) lists k3.1-pro "K3.1 Pro" and
k3.1-pro-highspeed "K3.1 Pro Highspeed". Kimi's pricing page
(platform.kimi.ai/docs/pricing/chat, read 2026-11-01) and models.dev's
live moonshotai entry both give kimi-k3.1-pro $2 in / $10 out / $0.20
cached / $2 5m write, and kimi-k3.1-pro-highspeed $4 / $20 / $0.20 / $4.
The fixture's new entries match models.dev's api.json in id, name, cost
and canonical_model_id.

The alias stays Kimi Code's (kimiCodePlans): the test's relay catalog
now lists a k3.1-pro of its own at $0.50/$1, which keeps that price, and
PricedNameFor(relay, k3.1-pro) stays k3.1-pro.

Docs: docs/reference.md, docs/subsystems/providers-accounts.md.

Run on mote (Go 1.26.8), at this change rebased on fb3942ee6 (the
pushed head adds only 784b66366, app.js and a GUI test): gofmt -l
clean; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows
go vet ./... ok; go test -count=1 -timeout 45m for internal/provider,
usage, gateway, agent, cli, routes, sessions, usageapi: all ok.
TestKimiCodeListPrice and TestKimiCodeCost red with models.go at
origin/main (k3.1-pro {0 0 0 0} false; kimi-code-cn/k3.1-pro
priced=false cost=0, want true 3.264), green with it. Taking the
kimiCodeMember() check out by hand turns PricedNameFor(relay, k3.1-pro)
into kimi-k3.1-pro and the test red.
Full go test ./... at base da0005df8: every package ok but internal/gui,
red on TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestMainWindowCorners/macOS_26_SDK (#3949), both open on main and not
touched here.
Not tried: a real Kimi Code membership's k3.1-pro call through the GUI.
2026-11-01 11:19:26 +08:00

80 lines
2.6 KiB
Go

package usage
import (
"math"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// #1370 (maicent): the Usage page counted a Kimi Code membership's k3 at
// ≈¥0.000, its plan's models.dev catalog listing every model at $0. A k3
// call is now counted at Kimi's API price for kimi-k3, a k3.1-pro one at
// kimi-k3.1-pro's, kimi-for-coding (K2.8 Preview, which the API doesn't
// sell) is unpriced rather than free, and a Moonshot pay-as-you-go call is
// what it was.
func TestKimiCodeCost(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
b, err := os.ReadFile(filepath.Join("..", "provider", "testdata", "kimi_code_catalog.json"))
if err != nil {
t.Fatal(err)
}
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
if err := os.WriteFile(catalog.CachePath(), b, 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
for _, id := range []string{"kimi-code-cn", "moonshot"} {
p, err := provider.FromPreset(id)
if err != nil {
t.Fatal(err)
}
p.Key = "sk-test"
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
// the reporter's k3 row: 240K in, 78.4K out, 10.0M cached
in, out, cached := 240_000, 78_400, 10_000_000
k3 := (float64(in)*3 + float64(out)*15 + float64(cached)*0.3) / 1e6
k31 := (float64(in)*2 + float64(out)*10 + float64(cached)*0.2) / 1e6
now := time.Now()
calls := []struct {
prov, model string
priced bool
want float64
}{
{"kimi-code-cn", "k3", true, k3},
{"kimi-code-cn", "k3.1-pro", true, k31},
{"kimi-code-cn", "kimi-for-coding", false, 0},
{"moonshot", "kimi-k3", true, k3},
}
for i, c := range calls {
Append(Record{Time: now.Add(-time.Duration(len(calls)-i) * time.Minute), Agent: "claude-code", Provider: c.prov, Model: c.model,
Input: in, Output: out, CacheRead: cached, Status: 200})
}
rows, sum, _ := Ledger(Month, Filter{})
if len(rows) != len(calls) {
t.Fatalf("rows %d", len(rows))
}
for i, c := range calls {
r := rows[len(calls)-1-i]
if r.Model != c.model || r.Priced != c.priced || math.Abs(r.Cost-c.want) > 1e-9 {
t.Errorf("%s/%s: priced=%v cost=%v, want %v %v", c.prov, c.model, r.Priced, r.Cost, c.priced, c.want)
}
}
s := Summarize(Month)
if s.Unpriced != 1 || math.Abs(s.Cost-(2*k3+k31)) > 1e-9 || math.Abs(sum.Cost-(2*k3+k31)) > 1e-9 {
t.Fatalf("summary cost %v unpriced %d, ledger %v; want %v and 1", s.Cost, s.Unpriced, sum.Cost, 2*k3+k31)
}
}