diff --git a/admin.go b/admin.go index 8fb8520..b595e75 100644 --- a/admin.go +++ b/admin.go @@ -960,6 +960,7 @@ type adminUsageEntryView struct { MCPServers []string `json:"mcpServers,omitempty"` Agents []string `json:"agents,omitempty"` RequestsPerMinute int64 `json:"requestsPerMinute"` + TokensPerMinute int64 `json:"tokensPerMinute"` RequestsPerDay int64 `json:"requestsPerDay"` TokensInPerDay int64 `json:"tokensInPerDay"` TokensOutPerDay int64 `json:"tokensOutPerDay"` @@ -1003,6 +1004,7 @@ func usageEntryView(su scopeUsage, limits *LimitsConfig) adminUsageEntryView { ID: su.id, Limits: limits, RequestsPerMinute: su.requestsPerMinute, + TokensPerMinute: su.tokensPerMinute, RequestsPerDay: su.requestsPerDay, TokensInPerDay: su.tokensInPerDay, TokensOutPerDay: su.tokensOutPerDay, @@ -1063,7 +1065,8 @@ func usageEntryView(su scopeUsage, limits *LimitsConfig) adminUsageEntryView { // chunk, keeping this endpoint's per-round-trip key count at the same // order of magnitude this constant has always targeted, rather than // letting it grow every time a future per-scope counter is added here. -const adminUsageChunkScopes = 145 +// 123 scopes * 13 keys/scope = 1,599 keys, within usageModelsChunkKeys. +const adminUsageChunkScopes = 123 // chunkedCurrentUsage calls limiter.currentUsage in batches of at most // adminUsageChunkScopes scopes at a time, concatenating the results in diff --git a/admin_test.go b/admin_test.go index eaf649a..1846935 100644 --- a/admin_test.go +++ b/admin_test.go @@ -3288,8 +3288,8 @@ func TestAdminUsage_LastSeen(t *testing.T) { // without a test catching it. func TestAdminUsage_ChunkScopesLoweredForLastSeenKey(t *testing.T) { t.Parallel() - if adminUsageChunkScopes != 145 { - t.Errorf("adminUsageChunkScopes = %d, want 145", adminUsageChunkScopes) + if adminUsageChunkScopes != 123 { + t.Errorf("adminUsageChunkScopes = %d, want 123", adminUsageChunkScopes) } if adminUsageChunkScopes*usageKeysPerScope > 1600 { t.Errorf("adminUsageChunkScopes*usageKeysPerScope = %d, want <= 1600", adminUsageChunkScopes*usageKeysPerScope) diff --git a/admin_usage_tpm_test.go b/admin_usage_tpm_test.go new file mode 100644 index 0000000..5405bd3 --- /dev/null +++ b/admin_usage_tpm_test.go @@ -0,0 +1,37 @@ +package traefikllmgateway + +import ( + "encoding/json" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func tpmUsage(t *testing.T, limits *LimitsConfig) adminUsageEntryView { + t.Helper() + h, gw := exemptGateway(t, nil, limits, nil) + require.Equal(t, http.StatusOK, memberLimitsChat(t, h, "sk-a").Code) + return findUsageEntry(t, gw.buildAdminUsage().Users, "a") +} + +func TestAdminUsage_TokensPerMinuteLive(t *testing.T) { + e := tpmUsage(t, &LimitsConfig{TokensPerMinute: 1000}) + assert.GreaterOrEqual(t, e.TokensPerMinute, int64(1), "minute token counter must surface") + raw, err := json.Marshal(e) + require.NoError(t, err) + var m map[string]any + require.NoError(t, json.Unmarshal(raw, &m)) + assert.Contains(t, m, "tokensPerMinute") +} + +func TestAdminUsage_TokensPerMinuteZeroWithoutLimit(t *testing.T) { + e := tpmUsage(t, &LimitsConfig{RequestsPerDay: 100}) + assert.EqualValues(t, 0, e.TokensPerMinute) +} + +func TestAdminUsageChunk_FitsKeyBudget(t *testing.T) { + assert.LessOrEqual(t, adminUsageChunkScopes*usageKeysPerScope, usageModelsChunkKeys) + assert.Equal(t, 13, usageKeysPerScope) +} diff --git a/limits.go b/limits.go index 411d2ee..1c653a1 100644 --- a/limits.go +++ b/limits.go @@ -2950,7 +2950,7 @@ func (l *limiter) providerUsage(scopes []limitScope) []providerCounters { // rejectionsPerDay counter (metricRej) usageWindowKeys now appends. Grows // once more, 9 to 10, for the last-seen counter (Q6, DECISIONS) — see // usageWindowKeys' own doc comment for the appended field's position. -const usageKeysPerScope = 11 +const usageKeysPerScope = 13 // scopeUsage is one scope's (a user's, a group's, or the synthetic total // scope's — totalScopeKind) current-window counter values — the admin @@ -2985,6 +2985,8 @@ type scopeUsage struct { lastSeen int64 // exemptPerMonth is the scope's limit-exempt request count this month. exemptPerMonth int64 + // tokensPerMinute is the minute-window uncached input plus output; 0 for scopes with no tokensPerMinute limit (nothing is written). + tokensPerMinute int64 // storeDown reports whether reading any of the usageKeysPerScope // counters above failed closed (a configured store errored and // failOpen is false) — mirrors limitViolation.storeDown. Every value @@ -3017,6 +3019,8 @@ func usageWindowKeys(sc limitScope, now time.Time) []string { windowKey(sc.kind, sc.id, metricRej, windowDay, now), lastSeenKey(sc.kind, sc.id), windowKey(sc.kind, sc.id, metricExempt, windowMonth, now), + windowKey(sc.kind, sc.id, metricTokIn, windowMin, now), + windowKey(sc.kind, sc.id, metricTokOut, windowMin, now), } } @@ -3092,6 +3096,7 @@ func (l *limiter) currentUsage(scopes []limitScope) []scopeUsage { tokensOutPerDay: v[4], tokensOutPerMonth: v[5], costPerDayMicros: v[6], costPerMonthMicros: v[7], rejectionsPerDay: v[8], lastSeen: v[9], exemptPerMonth: v[10], + tokensPerMinute: v[11] + v[12], } } return out diff --git a/webui/src/types/api.ts b/webui/src/types/api.ts index 0cb0b0b..f0a2ada 100644 --- a/webui/src/types/api.ts +++ b/webui/src/types/api.ts @@ -443,6 +443,8 @@ export interface AdminUsageEntryView { mcpServers?: string[] agents?: string[] requestsPerMinute: number + /** Live minute-window tokens (uncached input + output); 0 when the scope has no tokensPerMinute limit. */ + tokensPerMinute?: number requestsPerDay: number tokensInPerDay: number tokensOutPerDay: number