diff --git a/internal/accounting/accounting.go b/internal/accounting/accounting.go index 5ce8dbe..6ef3431 100644 --- a/internal/accounting/accounting.go +++ b/internal/accounting/accounting.go @@ -362,3 +362,10 @@ func keyCounter(keyID string) string { } return store.LimitOwner{Scope: store.ScopeKey, ID: keyID}.CounterKey() } + +// AnythingPriced reports whether a budget can ever be spent against. +// +// On the accountant rather than making callers reach for the price table: what +// they are asking is whether the budgets they are about to display mean +// anything, and the accountant is what would enforce them. +func (a *Accountant) AnythingPriced() bool { return a.pricing.AnythingPriced() } diff --git a/internal/accounting/accounting_test.go b/internal/accounting/accounting_test.go index bc6bc01..248b712 100644 --- a/internal/accounting/accounting_test.go +++ b/internal/accounting/accounting_test.go @@ -609,3 +609,24 @@ func TestStatusReportsTheFullestPoolNotTheSmallestNumber(t *testing.T) { t.Error("a member should see the fullest pool that bounds them") } } + +func TestAnEmptyPriceTableMeansNoBudgetCanEverBind(t *testing.T) { + // The ordinary case for self-hosted models, and the reason a budget on a + // team can read as a limit while binding on nothing. + if NewPricing(nil, Price{}).AnythingPriced() { + t.Error("nothing priced should say so") + } + if NewPricing(map[string]Price{"m": {}}, Price{}).AnythingPriced() { + t.Error("a model priced at zero is not priced") + } + if !NewPricing(map[string]Price{"m": {PromptPer1k: 1}}, Price{}).AnythingPriced() { + t.Error("one priced model is enough for a budget to move") + } + if !NewPricing(nil, Price{CompletionPer1k: 1}).AnythingPriced() { + t.Error("a fallback price prices everything") + } + var nilPricing *Pricing + if nilPricing.AnythingPriced() { + t.Error("no price table at all is not priced") + } +} diff --git a/internal/accounting/cost.go b/internal/accounting/cost.go index 34b00af..770eab2 100644 --- a/internal/accounting/cost.go +++ b/internal/accounting/cost.go @@ -68,3 +68,30 @@ func (p *Pricing) For(model string) Price { } return p.fallback } + +// AnythingPriced reports whether any model costs anything. +// +// A budget is spend per period, and spend is computed from a price table. With +// no prices every request costs nothing, so a budget written on a team is a +// number that can never move — displayed as a limit, reported as zero percent +// used, and binding on nothing. +// +// Self-hosted models have no per-token price, so an empty table is the ordinary +// case rather than a misconfiguration. What is not ordinary is showing somebody +// a budget without saying so. +func (p *Pricing) AnythingPriced() bool { + if p == nil { + return false + } + p.mu.RLock() + defer p.mu.RUnlock() + if !p.fallback.Zero() { + return true + } + for _, price := range p.byModel { + if !price.Zero() { + return true + } + } + return false +} diff --git a/internal/router/handlers.go b/internal/router/handlers.go index 9e0383b..267e1ec 100644 --- a/internal/router/handlers.go +++ b/internal/router/handlers.go @@ -160,7 +160,7 @@ func (r *Router) Handler() http.Handler { pages.RegisterAdminUsers(mux, r.store) // Quotas are written on the same tree the accounts page builds, so // they are mounted together. - pages.RegisterAdminQuotas(mux, r.store) + pages.RegisterAdminQuotas(mux, r.store, r.acct) } // Independent of the cluster view: the mirror is a separate machine diff --git a/internal/web/account.go b/internal/web/account.go index 7e017cc..643c5bf 100644 --- a/internal/web/account.go +++ b/internal/web/account.go @@ -57,6 +57,8 @@ type AccountPages struct { hasMirror bool // hasQuotas is set when /admin/quotas is registered. hasQuotas bool + // priced says whether a budget can ever be spent against. + priced Priced // registry browses a model registry, or is nil where there is no way out. registry Registry // catalog puts a mirrored model into service; fleet says whether anything diff --git a/internal/web/admin_quotas.go b/internal/web/admin_quotas.go index 9c5d4f5..5d21fbd 100644 --- a/internal/web/admin_quotas.go +++ b/internal/web/admin_quotas.go @@ -12,6 +12,10 @@ import ( "gitea.mynodes.xyz/ilker/llmgw/internal/store" ) +// Priced says whether anything a request could name costs money. The +// accountant implements it. +type Priced interface{ AnythingPriced() bool } + // QuotaAdmin is the store half of the quota page. type QuotaAdmin interface { AllOrgs(ctx context.Context) ([]store.Org, error) @@ -65,6 +69,10 @@ type quotaRow struct { type adminQuotasData struct { Rows []quotaRow + // BudgetsBind is false when no model has a price, which makes every budget + // on this page a number that cannot move. Shown, because a limit that binds + // on nothing and reads as "0% used" is worse than no limit at all. + BudgetsBind bool // Editing is the "scope:id" whose form is open, so the template can keep // the link on every other row. Editing string @@ -89,8 +97,9 @@ type adminQuotasData struct { // could see or change without a shell on the control plane — which in practice // means quotas nobody sets. Every change is logged with who made it and what it // was before. -func (p *AccountPages) RegisterAdminQuotas(mux *http.ServeMux, s QuotaAdmin) { +func (p *AccountPages) RegisterAdminQuotas(mux *http.ServeMux, s QuotaAdmin, priced Priced) { p.hasQuotas = true + p.priced = priced mux.HandleFunc("GET /admin/quotas", p.adminOnly( func(w http.ResponseWriter, r *http.Request, _ store.User) { @@ -193,6 +202,7 @@ func (p *AccountPages) renderQuotas(w http.ResponseWriter, r *http.Request, Now: time.Now().UTC().Format(time.RFC3339), } + d.BudgetsBind = p.priced != nil && p.priced.AnythingPriced() d.Editing = r.URL.Query().Get("edit") limits, err := s.AllLimits(r.Context()) diff --git a/internal/web/admin_quotas_test.go b/internal/web/admin_quotas_test.go index 92b2b99..44cde12 100644 --- a/internal/web/admin_quotas_test.go +++ b/internal/web/admin_quotas_test.go @@ -59,12 +59,22 @@ func quotasPage(t *testing.T, s *fakeQuotas, admin bool) *http.ServeMux { a := fakeAccounts{user: store.User{ID: "u1", Email: "a@example.com", IsAdmin: admin}} p := NewAccountPages(a, nil, nil, "v0", slog.New(slog.NewTextHandler(io.Discard, nil))) mux := http.NewServeMux() - p.RegisterAdminQuotas(mux, s) + p.RegisterAdminQuotas(mux, s, pricedYes{}) return mux } func i64(v int64) *int64 { return &v } +// pricedYes stands for a cluster where models cost something, so the warning +// about budgets binding on nothing stays out of the way of the other tests. +type pricedYes struct{} + +func (pricedYes) AnythingPriced() bool { return true } + +type pricedNo struct{} + +func (pricedNo) AnythingPriced() bool { return false } + // oneTeam is an org with a team and a project under it. func oneTeam() *fakeQuotas { return &fakeQuotas{ @@ -300,3 +310,33 @@ func TestSavingClosesTheFormSoTheResultIsVisible(t *testing.T) { t.Errorf("the page should confirm what was written; got %s", body) } } + +func TestABudgetThatCannotBindSaysSo(t *testing.T) { + a := fakeAccounts{user: store.User{ID: "u1", Email: "a@example.com", IsAdmin: true}} + p := NewAccountPages(a, nil, nil, "v0", slog.New(slog.NewTextHandler(io.Discard, nil))) + mux := http.NewServeMux() + p.RegisterAdminQuotas(mux, oneTeam(), pricedNo{}) + + // Whitespace normalised: the sentence wraps in the template and a test + // that pinned the wrap would break on a reflow rather than on a change of + // meaning. + body := strings.Join(strings.Fields(mirrorGet(t, mux, "/admin/quotas").Body.String()), " ") + + // A budget is spend per period and spend comes from a price table. With no + // prices the number reads as a limit, reports zero percent used, and binds + // on nothing. + if !strings.Contains(body, "budget below stays at zero") { + t.Errorf("the page should say budgets cannot move; got %s", body) + } + // Said precisely: the other two limits are unaffected and still work. + if !strings.Contains(body, "Rate and token limits are unaffected") { + t.Error("the warning should not read as though no quota works") + } +} + +func TestAPricedClusterIsNotWarned(t *testing.T) { + body := mirrorGet(t, quotasPage(t, oneTeam(), true), "/admin/quotas").Body.String() + if strings.Contains(body, "stays at zero") { + t.Error("a cluster with prices should see no warning") + } +} diff --git a/internal/web/templates/admin_quotas.html b/internal/web/templates/admin_quotas.html index 500ae66..5f25620 100644 --- a/internal/web/templates/admin_quotas.html +++ b/internal/web/templates/admin_quotas.html @@ -10,6 +10,17 @@ your keys +{{if not .BudgetsBind}} +{{/* A budget is spend per period and spend comes from a price table. With no + prices every request costs nothing, so these numbers read as limits and + bind on nothing. Self-hosted models having no per-token price is ordinary; + showing somebody a budget without saying so is not. */}} +
No model has a price, so nothing is charged and every
+budget below stays at zero however much is used. Rate and token limits are
+unaffected — they count requests and tokens, not money. Set prices in the
+pricing block of the server configuration to make budgets bind.
{{.Error}}
{{end}} {{if .Message}}{{.Message}}
{{end}}