From c28a5b3fe7517a658e396f085d4edb0b8b5d96cb Mon Sep 17 00:00:00 2001 From: InferenceBot Date: Sun, 1 Nov 2026 18:01:46 +0000 Subject: [PATCH] keys: show per-key spend, chat separately The API-keys card now shows each key's all-time spend (secondary, muted, appended to the key's meta line) and the built-in chat key's spend on its own line, read from the portal's key_spend field in /broker/usage. Skip rows flagged is_chat when matching spend to a member key, so an API key named 'chat' is not mistaken for the built-in. --- app/main.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 4818cfc..05b5e28 100644 --- a/app/main.py +++ b/app/main.py @@ -413,6 +413,9 @@ PAGE_HTML = """ .key-row:last-child { border-bottom: none; } .key-name { font-weight: 600; font-size: 14px; } .key-meta { color: var(--muted); font-size: 12px; margin-top: 2px; } + .key-chat { color: var(--muted); font-size: 12px; margin-top: 10px; } + .key-chat-name { font-weight: 600; } + .key-chat-num { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; } .key-token { font-family: ui-monospace, 'SF Mono', Menlo, monospace; font-size: 12px; color: var(--muted); @@ -597,6 +600,7 @@ PAGE_HTML = """

API keys

Keys let you use the co-op's AI from your own tools. They draw from the same balance as chat.

No API keys yet.
+
@@ -692,7 +696,7 @@ async function load() { renderModels(d.models || []); renderBoard(d.coop_models || []); - renderKeys(d.keys || []); + renderKeys(d.keys || [], d.key_spend || []); loadMxid(); } catch (e) { flash('Could not load usage: ' + e.message, false); @@ -997,23 +1001,45 @@ function renderBoard(models) { }).join(''); } -function renderKeys(keys) { +function renderKeys(keys, keySpend) { const el = $('keys'); + // Spend rows come from the portal, keyed by the member's key name; the chat + // key is carried separately (is_chat) and rendered on its own line. + const spendFor = {}; + (keySpend || []).forEach(s => { if (!s.is_chat) spendFor[s.name] = s; }); if (!keys.length) { el.innerHTML = '
No API keys yet. Create one below.
'; + renderChatSpend(keySpend); return; } el.innerHTML = keys.map(k => { const fp = k.fingerprint || ''; const created = k.created_at ? ' · created ' + k.created_at.slice(0,10) : ''; + const s = spendFor[k.name]; + // Secondary detail: this key's all-time spend (server-side, from the spend + // logs — LiteLLM's own per-key counter is period-only). + const spent = ' · ' + fmtMoney(s ? s.spend : 0) + ' spent'; return '
' + '
' + escapeHtml(k.name) + '
' + '
' + escapeHtml(created) + - (fp ? ' · ends …' + escapeHtml(fp) : '') + '
' + + (fp ? ' · ends …' + escapeHtml(fp) : '') + escapeHtml(spent) + '
' + '
' + '' + '
'; }).join(''); + renderChatSpend(keySpend); +} + +function renderChatSpend(keySpend) { + const el = $('key-chat'); + if (!el) return; + const chat = (keySpend || []).find(s => s.is_chat); + if (!chat) { el.innerHTML = ''; return; } + // The built-in chat key is not one of the member's API keys, so its spend is + // shown on its own line rather than mixed into the list above. + el.innerHTML = 'Chat (built-in) · ' + + '' + fmtMoney(chat.spend) + ' spent' + + (chat.requests ? ' · ' + chat.requests.toLocaleString() + ' requests' : ''); } function escapeHtml(s) {