Show a person their own use and what is left of their budgets

The account page (was "Your keys") shows the last thirty days of the
person's own use - requests, tokens, spend and tokens per day - and, for
each place they can spend, what they used there and a bar for every
budget that applies: their own, a per-member one, or a project's, team's
or organization's shared with others, named as such. Bars turn amber past
four-fifths and red when spent. It sits under the keys now, above the
password form.

Spend is read from the usage table for the pool each limit counts
against, from the start of its period.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-09-28 22:51:13 +02:00
1 parent 6b1f8b0827
commit 16a1e8b4d8
8 files changed
+480 -20

No files matched your search

+138
View File
@@ -0,0 +1,138 @@
package store
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
)
// PlaceUsage is one person's use in one place: a project, or on their own.
type PlaceUsage struct {
// ProjectID is empty for personal use.
ProjectID string
// Path names the project; empty for personal use.
Path string
Requests int64
Tokens int64
CostMicros int64
}
// UsageOfUser is one person's own use since a time: where it went, and how it
// ran over time in slices of width. What a person sees of themselves — every
// place they spent, and nothing anybody else did.
func (s *DB) UsageOfUser(
ctx context.Context, userID string, since time.Time, width time.Duration,
) ([]PlaceUsage, []Bucket, error) {
if userID == "" {
return nil, nil, fmt.Errorf("whose usage?")
}
places, err := queryAll(ctx, s.db, `
SELECT COALESCE(NULLIF(u.project_id, ''), ''),
COALESCE(o.name || ' / ' || t.name || ' / ' || p.name, ''),
COUNT(*), SUM(u.prompt_tokens + u.completion_tokens), SUM(u.cost_micros)
FROM usage u
LEFT JOIN projects p ON p.id = NULLIF(u.project_id, '')
LEFT JOIN teams t ON t.id = p.team_id
LEFT JOIN organizations o ON o.id = t.org_id
WHERE u.user_id = ? AND u.ts >= ?
GROUP BY 1, 2
ORDER BY 4 DESC`, []any{userID, millis(since)},
func(rows *sql.Rows) (PlaceUsage, error) {
var pu PlaceUsage
return pu, rows.Scan(&pu.ProjectID, &pu.Path, &pu.Requests, &pu.Tokens, &pu.CostMicros)
})
if err != nil {
return nil, nil, err
}
if width <= 0 {
width = 24 * time.Hour
}
ms := width.Milliseconds()
got, err := queryAll(ctx, s.db, `
SELECT (ts / ?) * ?, COUNT(*),
SUM(CASE WHEN status >= 400 THEN 1 ELSE 0 END),
SUM(prompt_tokens), SUM(completion_tokens), SUM(cost_micros)
FROM usage WHERE user_id = ? AND ts >= ?
GROUP BY 1 ORDER BY 1`, []any{ms, ms, userID, millis(since)},
func(rows *sql.Rows) (Bucket, error) {
var b Bucket
var start int64
err := rows.Scan(&start, &b.Requests, &b.Errors, &b.Prompt, &b.Completion, &b.CostMicros)
b.Start = fromMillis(start)
return b, err
})
if err != nil {
return nil, nil, err
}
// Gap-filled, for the reason UsageBuckets is: a day with nothing in it is
// a day, not a hole.
at := map[int64]Bucket{}
for _, b := range got {
at[millis(b.Start)] = b
}
var buckets []Bucket
for t := (millis(since) / ms) * ms; t <= millis(time.Now()); t += ms {
b, ok := at[t]
if !ok {
b = Bucket{Start: fromMillis(t)}
}
buckets = append(buckets, b)
}
return places, buckets, nil
}
// SpendOfPool is what has been spent since a time from the pool a limit
// counts against — the figure its budget is compared with.
//
// The pool follows the level: a project, team or organization's budget is
// spent by everyone beneath it; a person's by every key they own and the chat
// page; a per-member limit by one person within one scope, whose ScopeID is
// "scopeID/userID". A key's own limit is not answered here, and gives zero.
func (s *DB) SpendOfPool(ctx context.Context, l Limits, since time.Time) (int64, error) {
under := func(scope Scope) (string, error) {
switch scope {
case ScopeProject:
return `NULLIF(u.project_id, '') = ?`, nil
case ScopeTeam:
return `NULLIF(u.project_id, '') IN (SELECT id FROM projects WHERE team_id = ?)`, nil
case ScopeOrg:
return `NULLIF(u.project_id, '') IN (SELECT p.id FROM projects p
JOIN teams t ON t.id = p.team_id WHERE t.org_id = ?)`, nil
}
return "", fmt.Errorf("no pool for %q", scope)
}
var where string
var args []any
switch l.Scope {
case ScopeUser:
where, args = `u.user_id = ?`, []any{l.ScopeID}
case ScopeOrg, ScopeTeam, ScopeProject:
w, err := under(l.Scope)
if err != nil {
return 0, err
}
where, args = w, []any{l.ScopeID}
case ScopeOrgMember, ScopeTeamMember, ScopeProjectMember:
scopeID, userID, ok := strings.Cut(l.ScopeID, "/")
if !ok {
return 0, fmt.Errorf("a per-member pool is scope/user, not %q", l.ScopeID)
}
level := map[Scope]Scope{ScopeOrgMember: ScopeOrg, ScopeTeamMember: ScopeTeam,
ScopeProjectMember: ScopeProject}[l.Scope]
w, err := under(level)
if err != nil {
return 0, err
}
where, args = `u.user_id = ? AND `+w, []any{userID, scopeID}
default:
return 0, nil
}
var total sql.NullInt64
err := s.db.QueryRowContext(ctx,
`SELECT SUM(u.cost_micros) FROM usage u WHERE `+where+` AND u.ts >= ?`,
append(args, millis(since))...).Scan(&total)
return total.Int64, err
}
+92
View File
@@ -0,0 +1,92 @@
package store
import (
"context"
"path/filepath"
"testing"
"time"
)
func selfUsageFixture(t *testing.T, s *DB) time.Time {
t.Helper()
ctx := context.Background()
must := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
must(s.PutOrg(ctx, Org{ID: "o", Name: "Acme"}))
must(s.PutTeam(ctx, Team{ID: "t", OrgID: "o", Name: "ml"}))
must(s.PutProject(ctx, Project{ID: "p", TeamID: "t", Name: "training"}))
must(s.PutProject(ctx, Project{ID: "q", TeamID: "t", Name: "eval"}))
now := time.Now()
u := func(user, project string, cost int64) Usage {
return Usage{At: now, KeyID: "session:" + user, UserID: user, ProjectID: project,
Model: "m", ServedModel: "m", PromptTokens: 10, CompletionTokens: 5, CostMicros: cost, Status: 200}
}
must(s.WriteUsage(ctx, []Usage{
u("ada", "p", 100), u("ada", "p", 100), u("ada", "", 30), u("bob", "p", 1000), u("bob", "q", 7),
}))
return now
}
func checkSelfUsage(t *testing.T, s *DB) {
now := selfUsageFixture(t, s)
ctx := context.Background()
since := now.Add(-48 * time.Hour)
places, buckets, err := s.UsageOfUser(ctx, "ada", since, 24*time.Hour)
if err != nil {
t.Fatal(err)
}
got := map[string]PlaceUsage{}
for _, pl := range places {
got[pl.ProjectID] = pl
}
// Ada's own, and none of Bob's.
if len(places) != 2 || got["p"].Requests != 2 || got["p"].Path != "Acme / ml / training" ||
got[""].CostMicros != 30 || got["p"].Tokens != 30 {
t.Errorf("places = %+v", places)
}
var total int64
for _, b := range buckets {
total += b.Requests
}
if total != 3 || len(buckets) < 2 {
t.Errorf("%d buckets carrying %d requests", len(buckets), total)
}
for _, c := range []struct {
l Limits
want int64
}{
{Limits{Scope: ScopeUser, ScopeID: "ada"}, 230},
{Limits{Scope: ScopeProject, ScopeID: "p"}, 1200},
{Limits{Scope: ScopeTeam, ScopeID: "t"}, 1207},
{Limits{Scope: ScopeOrg, ScopeID: "o"}, 1207},
{Limits{Scope: ScopeProjectMember, ScopeID: "p/ada"}, 200},
{Limits{Scope: ScopeTeamMember, ScopeID: "t/bob"}, 1007},
} {
n, err := s.SpendOfPool(ctx, c.l, since)
if err != nil {
t.Fatal(err)
}
if n != c.want {
t.Errorf("%s %s spent %d, want %d", c.l.Scope, c.l.ScopeID, n, c.want)
}
}
}
func TestAPersonSeesTheirOwnUse(t *testing.T) {
s, err := OpenSQLite(context.Background(), filepath.Join(t.TempDir(), "u.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = s.Close() })
checkSelfUsage(t, s)
}
func TestPostgresAPersonSeesTheirOwnUse(t *testing.T) {
checkSelfUsage(t, openPG(t))
}
+13 -1
View File
@@ -233,6 +233,13 @@ type limitRow struct{ Name, Value string }
type contextLimits struct {
Label string
Rows []limitRow
// Use is what the person spent here over the window, and Budgets how
// much is left of each budget that applies; both nil without usage.
Use *placeUse
Budgets []budgetBar
project string
chain []store.Limits
}
type profileData struct {
@@ -244,6 +251,9 @@ type profileData struct {
Where map[string]string
Memberships []store.Membership
LimitsBy []contextLimits
// Own is the person's use over the last thirty days; nil when it cannot
// be read.
Own *ownUse
User store.User
Keys []store.Key
Limits []limitRow
@@ -407,8 +417,10 @@ func (p *AccountPages) fillPlaces(ctx context.Context, user store.User, d *profi
if err != nil {
continue
}
d.LimitsBy = append(d.LimitsBy, contextLimits{Label: c.Label, Rows: limitRows(e)})
d.LimitsBy = append(d.LimitsBy, contextLimits{Label: c.Label, Rows: limitRows(e),
project: project, chain: e.Chain})
}
p.fillOwnUse(ctx, user, d)
}
func limitRows(e store.Effective) []limitRow {
+137
View File
@@ -0,0 +1,137 @@
package web
import (
"context"
"fmt"
"html/template"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/accounting"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// SelfUsage is what a person's own page reads of their use. Optional: a
// store without it leaves the page showing limits alone.
type SelfUsage interface {
UsageOfUser(ctx context.Context, userID string, since time.Time, width time.Duration) ([]store.PlaceUsage, []store.Bucket, error)
SpendOfPool(ctx context.Context, l store.Limits, since time.Time) (int64, error)
}
// selfWindow is how far back a person's own page looks.
const selfWindow = 30 * 24 * time.Hour
// ownUse is the person's last thirty days, for the top of their page.
type ownUse struct {
Requests, Tokens int64
Spend string
Chart template.HTML
}
// budgetBar is one budget that applies, and how much of it is gone.
type budgetBar struct {
// Label says whose pool it is: the person's own, or one they share.
Label string
Used, Budget string
Period string
Percent int
// Near is past four-fifths, when it is worth colouring.
Near, Over bool
}
// placeUse is what a person spent in one place over the window.
type placeUse struct {
Requests, Tokens int64
Spend string
}
func (p *AccountPages) fillOwnUse(ctx context.Context, user store.User, d *profileData) {
su, ok := p.keys.(SelfUsage)
if !ok {
return
}
places, buckets, err := su.UsageOfUser(ctx, user.ID, time.Now().Add(-selfWindow), 24*time.Hour)
if err != nil {
p.log.Warn("could not read a person's own usage", "err", err)
return
}
u := &ownUse{}
var cost int64
byPlace := map[string]placeUse{}
for _, pl := range places {
u.Requests += pl.Requests
u.Tokens += pl.Tokens
cost += pl.CostMicros
byPlace[pl.ProjectID] = placeUse{pl.Requests, pl.Tokens, units(pl.CostMicros)}
}
u.Spend = units(cost)
s := Series{Colour: "--accent"}
for _, b := range buckets {
s.Values = append(s.Values, float64(b.Prompt+b.Completion))
s.Labels = append(s.Labels, b.Start.UTC().Format("Jan 2"))
}
if u.Requests > 0 {
u.Chart = BarChart(s, "tokens")
}
d.Own = u
for i := range d.LimitsBy {
c := &d.LimitsBy[i]
if pu, ok := byPlace[c.project]; ok {
c.Use = &pu
}
for _, l := range c.chain {
if l.Budget == nil || l.Scope == store.ScopeKey {
continue
}
start := accounting.PeriodStart(l.BudgetPeriod, time.Now())
spent, err := su.SpendOfPool(ctx, l, start)
if err != nil {
p.log.Warn("could not read a pool's spend", "scope", l.Scope, "err", err)
continue
}
bar := budgetBar{Label: poolLabel(l.Scope), Used: units(spent), Budget: units(*l.Budget),
Period: periodWord(l.BudgetPeriod)}
if *l.Budget > 0 {
bar.Percent = int(min(spent*100 / *l.Budget, 100))
bar.Near = spent*5 >= *l.Budget*4
bar.Over = spent >= *l.Budget
}
c.Budgets = append(c.Budgets, bar)
}
}
}
// poolLabel says whose budget a limit is, which is the thing a person needs
// to read a bar right: their own is theirs to spend, a project's is shared
// with everyone in it.
func poolLabel(s store.Scope) string {
switch s {
case store.ScopeUser:
return "Your budget, across everything you use"
case store.ScopeProjectMember:
return "Your budget in this project"
case store.ScopeTeamMember:
return "Your budget in this team"
case store.ScopeOrgMember:
return "Your budget in this organization"
case store.ScopeProject:
return "The project's budget, shared with its members"
case store.ScopeTeam:
return "The team's budget, shared across its projects"
case store.ScopeOrg:
return "The organization's budget, shared across it"
}
return string(s)
}
func periodWord(p store.Period) string {
switch p {
case store.PeriodDay:
return "today"
case store.PeriodMonth:
return "this month"
case store.PeriodNone:
return "in total"
}
return fmt.Sprintf("this %s", p)
}
+57
View File
@@ -0,0 +1,57 @@
package web
import (
"context"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
func TestAPersonSeesTheirUseAndWhatIsLeftOfTheirBudget(t *testing.T) {
ctx := context.Background()
st, err := store.OpenSQLite(ctx, filepath.Join(t.TempDir(), "s.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
budget := int64(50e6)
st.SetPersonalDefaults(&store.Limits{Budget: &budget, BudgetPeriod: store.PeriodMonth})
// 45 of 50 units this month: past four-fifths, so the bar says so.
if err := st.WriteUsage(ctx, []store.Usage{
{At: time.Now(), KeyID: "session:ada", UserID: "ada", Model: "m", ServedModel: "m",
PromptTokens: 1200, CompletionTokens: 300, CostMicros: 45e6, Status: 200},
{At: time.Now(), KeyID: "session:bob", UserID: "bob", Model: "m", ServedModel: "m",
PromptTokens: 9, CompletionTokens: 9, CostMicros: 1e6, Status: 200},
}); err != nil {
t.Fatal(err)
}
a := fakeAccounts{user: store.User{ID: "ada", Email: "ada@example.com"}}
p := NewAccountPages(a, st, nil, "v0", slog.New(slog.NewTextHandler(io.Discard, nil)))
mux := http.NewServeMux()
p.Register(mux)
req := httptest.NewRequest("GET", "/profile", nil)
req.AddCookie(&http.Cookie{Name: SessionCookie, Value: "good"})
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
body := rec.Body.String()
for _, want := range []string{
"Your use and limits",
// Ada's 1.5k tokens, and none of Bob's.
">1.5k</div>", "tokens · 30 days", "Tokens per day",
"Your budget, across everything you use", "<b>45.00</b> of 50.00 this month",
`class="budget near"`, "width: 90%",
} {
if !strings.Contains(body, want) {
t.Errorf("missing %q", want)
}
}
}
+10
View File
@@ -522,3 +522,13 @@ table.repo-files td:last-child .opt { display: block; margin-bottom: .2rem; }
.bubble.md table { width: auto; max-width: 100%; display: block; overflow-x: auto; font-size: .85rem; }
.bubble.md th, .bubble.md td { padding: .25rem .6rem; border-bottom: 1px solid var(--line); }
.bubble.md blockquote { border-left: 3px solid var(--line); padding-left: .75rem; color: var(--muted); }
/* How much of a budget is gone, on a person's own page. */
.budget { max-width: 34rem; margin: .4rem 0 .6rem; }
.budget-head { display: flex; justify-content: space-between; gap: 1rem; font-size: .85rem; color: var(--muted); }
.budget-head b { color: var(--fg); font-variant-numeric: tabular-nums; }
.budget-bar { height: .5rem; border-radius: 999px; background: var(--card); border: 1px solid var(--line); overflow: hidden; margin-top: .25rem; }
.budget-bar > span { display: block; height: 100%; background: var(--accent); }
.budget.near .budget-bar > span { background: var(--warn); }
.budget.over .budget-bar > span { background: var(--bad); }
.budget.over .budget-head b { color: var(--bad); }
.own-use .tile { min-width: 9rem; }
+32 -18
View File
@@ -47,10 +47,10 @@ Tell whoever you asked for access that you have signed up.</p>
{{define "profile"}}<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Your keys · llmgw</title><link rel="stylesheet" href="{{stylesheet}}">
<title>Account · llmgw</title><link rel="stylesheet" href="{{stylesheet}}">
</head><body><div class="wrap">
<header>
<h1>Your keys</h1>
<h1>Your account</h1>
<p class="sub">{{.User.Email}}{{if ne .User.Name .User.Email}} ({{.User.Name}}){{end}}</p>
{{template "nav" .Nav}}
</header>
@@ -106,6 +106,36 @@ minute. To rotate: create the new key, switch over, wait for "last used" on the
old one to stop moving, then revoke it.</p>
{{else}}<p class="empty">You have no keys yet.</p>{{end}}
<h2>Where you are</h2>
{{if .Memberships}}
<p>{{range $i, $m := .Memberships}}{{if $i}} · {{end}}{{$m.Path}} <span class="opt">{{$m.Scope}}</span>{{end}}</p>
{{else}}<p class="empty">You are not a member of any organization, team or project.</p>{{end}}
<h2>Your use and limits</h2>
{{with .Own}}
<div class="tiles own-use">
<div class="tile"><div class="v">{{compact .Requests}}</div><div class="k">requests · 30 days</div></div>
<div class="tile"><div class="v">{{compact .Tokens}}</div><div class="k">tokens · 30 days</div></div>
<div class="tile"><div class="v">{{.Spend}}</div><div class="k">spend · 30 days</div></div>
</div>
{{with .Chart}}<div class="chart-wrap"><h3 class="chart-title">Tokens per day</h3>{{.}}</div>{{end}}
{{end}}
{{range .LimitsBy}}
<h3 class="limits-for">{{.Label}}{{with .Use}} <span class="opt">— {{compact .Requests}} requests, {{compact .Tokens}} tokens in 30 days</span>{{end}}</h3>
{{range .Budgets}}
<div class="budget{{if .Over}} over{{else if .Near}} near{{end}}">
<div class="budget-head"><span>{{.Label}}</span><span><b>{{.Used}}</b> of {{.Budget}} {{.Period}}</span></div>
<div class="budget-bar"><span style="width: {{.Percent}}%"></span></div>
</div>
{{end}}
{{if .Rows}}
<table class="compact narrow-table"><tr><th>Limit</th><th class="num">Value</th></tr>
{{range .Rows}}<tr><td>{{.Name}}</td><td class="num">{{.Value}}</td></tr>{{end}}</table>
{{else}}<p class="empty">No limits here.</p>{{end}}
{{else}}<p class="empty">Nowhere to spend yet.</p>{{end}}
<p class="note">Everything that applies, from the organization down to you: the
strictest value wins.</p>
<h2>Password</h2>
{{if .PasswordChanged}}<p>Your password was changed. Any other browser you were
signed in on has been signed out.</p>{{end}}
@@ -121,21 +151,5 @@ signed in on has been signed out.</p>{{end}}
<p class="note">Changing it signs out every other browser, which is the point if
you are changing it because something went wrong.</p>
<h2>Where you are</h2>
{{if .Memberships}}
<p>{{range $i, $m := .Memberships}}{{if $i}} · {{end}}{{$m.Path}} <span class="opt">{{$m.Scope}}</span>{{end}}</p>
{{else}}<p class="empty">You are not a member of any organization, team or project.</p>{{end}}
<h2>Your limits</h2>
{{range .LimitsBy}}
<h3 class="limits-for">{{.Label}}</h3>
{{if .Rows}}
<table class="compact narrow-table"><tr><th>Limit</th><th class="num">Value</th></tr>
{{range .Rows}}<tr><td>{{.Name}}</td><td class="num">{{.Value}}</td></tr>{{end}}</table>
{{else}}<p class="empty">No limits here.</p>{{end}}
{{else}}<p class="empty">Nowhere to spend yet.</p>{{end}}
<p class="note">Everything that applies, from the organization down to you: the
strictest value wins.</p>
<footer><span>llmgw {{.Version}}</span><span>{{.Now}}</span></footer>
</div></body></html>{{end}}
+1 -1
View File
@@ -22,7 +22,7 @@
<span class="menu-gap" aria-hidden="true"></span>
{{- end}}
<a href="/chat"{{if eq .Current "chat"}} aria-current="page"{{end}}>Chat</a>
<a href="/profile"{{if eq .Current "keys"}} aria-current="page"{{end}}>Your keys</a>
<a href="/profile"{{if eq .Current "keys"}} aria-current="page"{{end}}>Account</a>
<form method="post" action="/logout" class="inline">
<input type="hidden" name="csrf" value="{{.CSRF}}">
<button type="submit" class="link">Sign out</button>