plan: a Le Chat plan's month of Mistral Vibe on its key's card (dhanyqaz on Discord)

Le Chat Pro, Student and Team come with a monthly Mistral Vibe budget,
which the plan's Vibe key (chat.mistral.ai/code/extensions?focus=key) is
told at Mistral's billing API, the routes Mistral's own Vibe CLI reads
(mistralai/mistral-vibe c96a6db, vibe/app_server/_billing.py and
_account.py, fixtures in tests/app_server/test_billing.py):
GET https://api.mistral.ai/v1/billing/budget and /v1/billing/subscription,
the key as a Bearer. A key on api.mistral.ai now has a plan source
(planQuotaSourceOf, not sure): vibe_budget becomes one Month window, its
usage_percentage the share used, initial_budget the limit in its currency
(EUR), reset at reset_at, and Aside while pay-as-you-go is on (the
budget's payg_enabled, else the subscription's vibe_payg_enabled), since
what goes past it is billed then rather than stopped. The plan's name is
billing's plan_name titled as Vibe titles it (EDU is Student, INDIVIDUAL
Pro), asked only of a key with a budget; the card stands without it.
A key with no Vibe budget (initial_budget 0 or none, a la Plateforme key)
has no windows and no card, and a refusal gives no card unless one was
read before. Le Chat's own message limits are told by no API.

Run (mote): go test ./internal/provider (whole package, ok), plus the
Balance/Plan/Quota/Usage/Allowance/Key tests of internal/gui and
internal/gateway; GOOS=linux and GOOS=windows go vet ./internal/provider;
gofmt -l clean.
Broken by hand: with the budget's own payg_enabled ignored,
TestMistralVibeBudget goes red.
Not tried: a real Vibe key. None was at hand. Live, both billing routes
answer 401 "Invalid API Key" to a bad key while an unknown /v1/billing
path is 404, so they exist; the bodies are Vibe's own test fixtures
("Producer: dashboard billing_api_public"), not a reply seen.

Reported-by: dhanyqaz
This commit is contained in:
yetone
2026-11-06 11:53:33 +08:00
parent dbe875ef30
commit 8216592a84
4 changed files with 235 additions and 2 deletions
File diff suppressed because one or more lines are too long
+106
View File
@@ -0,0 +1,106 @@
package provider
import (
"context"
"encoding/json"
"strings"
"time"
)
// Mistral's Le Chat plans (Pro, Student, Team) come with a month of Mistral
// Vibe, which the plan's Vibe key (chat.mistral.ai/code/extensions?focus=key)
// is told at the billing API on api.mistral.ai, as Mistral's own Vibe CLI
// reads it (mistralai/mistral-vibe, vibe/app_server/_billing.py and
// _account.py): GET /v1/billing/budget for the month's budget, and
// GET /v1/billing/subscription for the plan it is on (dhanyqaz on Discord,
// a Student plan). Le Chat's own message limits are told nowhere.
const (
mistralBudgetURL = "https://api.mistral.ai/v1/billing/budget"
mistralSubscriptionURL = "https://api.mistral.ai/v1/billing/subscription"
)
// mistralPlanTitles are the plans billing names, as Vibe titles them.
var mistralPlanTitles = map[string]string{
"FREE": "Free",
"INDIVIDUAL": "Pro",
"EDU": "Student",
"TEAM": "Team",
}
// mistralBudget: {"vibe_budget":{"usage_percentage":42.5,
// "initial_budget":300.0,"currency":"EUR","reset_at":"2026-11-01T00:00:00Z",
// "payg_enabled":false},"api_budget":{…}}
type mistralBudget struct {
Vibe *struct {
UsagePercentage float64 `json:"usage_percentage"`
InitialBudget float64 `json:"initial_budget"`
Currency string `json:"currency"`
ResetAt string `json:"reset_at"`
PaygEnabled *bool `json:"payg_enabled"`
} `json:"vibe_budget"`
}
// mistralSubscription: {"plan_name":"INDIVIDUAL","payg_enabled":true,
// "vibe_payg_enabled":true,"enterprise":false}
type mistralSubscription struct {
PlanName string `json:"plan_name"`
VibePaygEnabled *bool `json:"vibe_payg_enabled"`
}
// readMistralBudget is a Mistral key's Vibe month: what share of the
// budget is used, in the budget's currency, and when it resets. A key with
// no Vibe budget (a la Plateforme key, or none in its plan) has no
// windows, and no card. With pay-as-you-go on (the budget's own, else
// payg, the subscription's), what goes past the budget is billed rather
// than stopped, so the window doesn't stop the models.
func readMistralBudget(b []byte, payg *bool) ([]QuotaWindow, error) {
var r mistralBudget
if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
v := r.Vibe
if v == nil || v.InitialBudget <= 0 {
return []QuotaWindow{}, nil
}
used := max(0, v.UsagePercentage)
w := QuotaWindow{Name: "Month", Used: min(100, used), Limit: v.InitialBudget,
Amount: v.InitialBudget * used / 100, Unit: strings.ToUpper(strings.TrimSpace(v.Currency))}
if t, err := time.Parse(time.RFC3339, v.ResetAt); err == nil {
w.ResetsAt = &t
w.Span = t.Sub(t.AddDate(0, -1, 0))
}
if v.PaygEnabled != nil {
payg = v.PaygEnabled
}
w.Aside = payg != nil && *payg
return []QuotaWindow{w}, nil
}
func readMistralPlan(b []byte) (string, []QuotaWindow, error) {
ws, err := readMistralBudget(b, nil)
return "", ws, err
}
// mistralKeyWindows is readMistralBudget's window with the plan it is on,
// asked only of a key that has a budget; the card stands without its
// name when billing/subscription can't be read.
func mistralKeyWindows(ctx context.Context, src planQuotaSource, key string) (string, []QuotaWindow, error) {
b, err := planBody(ctx, src, key)
if err != nil {
return "", nil, err
}
ws, err := readMistralBudget(b, nil)
if err != nil || len(ws) == 0 {
return "", ws, err
}
var sub mistralSubscription
if sb, err := planBody(ctx, planQuotaSource{url: mistralSubscriptionURL, bearer: true}, key); err == nil && json.Unmarshal(sb, &sub) == nil {
ws, _ = readMistralBudget(b, sub.VibePaygEnabled)
}
name := strings.ToUpper(strings.TrimSpace(sub.PlanName))
plan := mistralPlanTitles[name]
if plan == "" {
plan = strings.TrimSpace(sub.PlanName)
}
return plan, ws, nil
}
+118
View File
@@ -0,0 +1,118 @@
package provider
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// A Le Chat plan's Vibe key (dhanyqaz on Discord, a Student plan) is told
// its month of Vibe at Mistral's billing API, as Vibe reads it: the share
// used in euros, the reset, and the plan's name; a key with no Vibe budget
// has no card, and one refused says what Mistral said. The bodies are
// Vibe's own fixtures (tests/app_server/test_billing.py, test_account.py).
func TestMistralVibeBudget(t *testing.T) {
type reply struct {
status int
body string
}
var budget, sub reply
var auths []string
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auths = append(auths, r.Host+" "+r.Header.Get("Authorization"))
var rep reply
switch r.URL.Path {
case "/v1/billing/budget":
rep = budget
case "/v1/billing/subscription":
rep = sub
default:
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(rep.status)
w.Write([]byte(rep.body))
}))
t.Cleanup(srv.Close)
addr := srv.Listener.Addr().String()
old := http.DefaultClient.Transport
http.DefaultClient.Transport = &http.Transport{
DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, network, addr)
},
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
t.Cleanup(func() { http.DefaultClient.Transport = old })
p := Provider{ID: "mistral", Chat: "https://api.mistral.ai/v1", Key: "vibe-key"}
src, ok := planQuotaSourceOf(p)
if !ok || src.url != mistralBudgetURL || src.sure {
t.Fatalf("source: %+v %v, want the budget, not sure", src, ok)
}
read := func() (string, []QuotaWindow, error) {
auths = nil
plan, ws, _, err := planKeyWindows(context.Background(), p, src, p.Key)
return plan, ws, err
}
const vibe = `{"vibe_budget":{"usage_percentage":42.5,"initial_budget":300.0,"currency":"EUR","reset_at":"2026-11-01T00:00:00Z","payg_enabled":false},"api_budget":{"usage_percentage":3.0,"initial_budget":50.0,"currency":"EUR","reset_at":"2026-11-01T00:00:00Z"}}`
budget = reply{200, vibe}
sub = reply{200, `{"plan_name":"EDU","payg_enabled":true,"vibe_payg_enabled":true,"enterprise":false}`}
plan, ws, err := read()
if err != nil || plan != "Student" || len(ws) != 1 {
t.Fatalf("student: %q %+v %v", plan, ws, err)
}
w := ws[0]
reset := time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)
if w.Name != "Month" || w.Used != 42.5 || w.Limit != 300 || w.Amount != 127.5 || w.Unit != "EUR" || w.ResetsAt == nil || !w.ResetsAt.Equal(reset) || w.Span != 31*24*time.Hour {
t.Errorf("window: %+v", w)
}
if w.Aside {
t.Error("the budget's own payg_enabled false is the window's, over the subscription's")
}
for _, a := range auths {
if a != "api.mistral.ai Bearer vibe-key" {
t.Errorf("asked %q, want api.mistral.ai with the key as a Bearer", a)
}
}
if len(auths) != 2 {
t.Errorf("asked %d times, want the budget and the subscription", len(auths))
}
// pay-as-you-go the subscription's alone: billed past the budget
budget = reply{200, `{"vibe_budget":{"usage_percentage":120,"initial_budget":300.0,"currency":"EUR","reset_at":"2026-11-01T00:00:00Z"}}`}
sub = reply{200, `{"plan_name":"INDIVIDUAL","vibe_payg_enabled":true}`}
if plan, ws, err := read(); err != nil || plan != "Pro" || len(ws) != 1 || !ws[0].Aside || ws[0].Used != 100 || ws[0].Amount != 360 {
t.Errorf("pro, payg: %q %+v %v", plan, ws, err)
}
// the subscription unread: the window stands, with no plan name
budget, sub = reply{200, vibe}, reply{500, `{"message":"boom"}`}
if plan, ws, err := read(); err != nil || plan != "" || len(ws) != 1 || ws[0].Used != 42.5 {
t.Errorf("no subscription: %q %+v %v", plan, ws, err)
}
// a key with no Vibe budget: no windows, so no card, and nothing more asked
for _, body := range []string{`{"vibe_budget":null,"api_budget":{"usage_percentage":3,"initial_budget":50}}`, `{"vibe_budget":{"usage_percentage":0,"initial_budget":0}}`, `{}`} {
budget = reply{200, body}
if _, ws, err := read(); err != nil || len(ws) != 0 || len(auths) != 1 {
t.Errorf("%s: %+v %v, asked %d", body, ws, err, len(auths))
}
}
// refused: Mistral's words
budget = reply{401, `{"message":"Invalid API Key"}`}
if _, _, err := read(); err == nil || !strings.Contains(err.Error(), "Invalid API Key") {
t.Errorf("refused: %v", err)
}
budget = reply{403, `{"message":"Forbidden"}`}
if _, ws, err := read(); err == nil || len(ws) != 0 {
t.Errorf("a member without the permission: %+v %v, want an error, and no card from it unless one was read", ws, err)
}
}
+10 -1
View File
@@ -2,7 +2,8 @@ package provider
// A plan bought with an API key — Zhipu's GLM Coding Plan (and Z.ai's),
// Kimi Code, OpenCode Go, a Command Code plan, MiniMax's Coding (Token)
// Plan, Ollama Cloud's included allowance, StepFun's Step Plan, Volcengine Ark's Coding and Agent Plans and
// Plan, Ollama Cloud's included allowance, a Le Chat plan's month of Mistral
// Vibe, StepFun's Step Plan, Volcengine Ark's Coding and Agent Plans and
// Tencent Cloud's Token Plan 企业版 — has windows of allowance like a
// subscription's, which the vendor tells to the key (StepFun only to a
// sign-in, stepfun_plan.go; Volcengine and Tencent Cloud only to the
@@ -68,6 +69,10 @@ func planQuotaSourceOf(p Provider) (planQuotaSource, bool) {
// Ollama Cloud's included allowance (#1888); a key with none
// gets no card
return planQuotaSource{ollamaBalanceURL, true, readOllamaPlan, false}, true
case "api.mistral.ai":
// a Le Chat plan's Vibe key is told its month of Vibe
// (mistral_plan.go); a key with no Vibe budget gets no card
return planQuotaSource{mistralBudgetURL, true, readMistralPlan, false}, true
}
}
return planQuotaSource{}, false
@@ -700,6 +705,10 @@ func planKeyWindows(ctx context.Context, p Provider, src planQuotaSource, key st
plan, ws, err = cmdKeyWindows(ctx, src, key)
return plan, ws, false, err
}
if src.url == mistralBudgetURL {
plan, ws, err = mistralKeyWindows(ctx, src, key)
return plan, ws, false, err
}
plan, ws, err = planWindows(ctx, src, key)
// a key refused is refused as a team's too
if zhipu := strings.HasSuffix(src.url, "/api/monitor/usage/quota/limit"); zhipu && (err != nil || len(ws) == 0) && !zhipuKeyRefusedErr(err) {