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.
This commit is contained in:
InferenceBot committed 2026-11-01 18:01:46 +00:00
1 parent 700339aea6
commit c28a5b3fe7
1 file changed
+29 -3
+29 -3
View File
@@ -413,6 +413,9 @@ PAGE_HTML = """<!DOCTYPE html>
.key-row:last-child { border-bottom: none; } .key-row:last-child { border-bottom: none; }
.key-name { font-weight: 600; font-size: 14px; } .key-name { font-weight: 600; font-size: 14px; }
.key-meta { color: var(--muted); font-size: 12px; margin-top: 2px; } .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 { .key-token {
font-family: ui-monospace, 'SF Mono', Menlo, monospace; font-family: ui-monospace, 'SF Mono', Menlo, monospace;
font-size: 12px; color: var(--muted); font-size: 12px; color: var(--muted);
@@ -597,6 +600,7 @@ PAGE_HTML = """<!DOCTYPE html>
<h2>API keys</h2> <h2>API keys</h2>
<p class="hint">Keys let you use the co-op's AI from your own tools. They draw from the same balance as chat.</p> <p class="hint">Keys let you use the co-op's AI from your own tools. They draw from the same balance as chat.</p>
<div class="keys" id="keys"><div class="empty">No API keys yet.</div></div> <div class="keys" id="keys"><div class="empty">No API keys yet.</div></div>
<div class="key-chat" id="key-chat"></div>
<div class="new-key"> <div class="new-key">
<input id="new-name" type="text" placeholder="Key name (e.g. my-script)" maxlength="64" /> <input id="new-name" type="text" placeholder="Key name (e.g. my-script)" maxlength="64" />
<button class="primary" id="create-btn" onclick="createKey()">Create key</button> <button class="primary" id="create-btn" onclick="createKey()">Create key</button>
@@ -692,7 +696,7 @@ async function load() {
renderModels(d.models || []); renderModels(d.models || []);
renderBoard(d.coop_models || []); renderBoard(d.coop_models || []);
renderKeys(d.keys || []); renderKeys(d.keys || [], d.key_spend || []);
loadMxid(); loadMxid();
} catch (e) { } catch (e) {
flash('Could not load usage: ' + e.message, false); flash('Could not load usage: ' + e.message, false);
@@ -997,23 +1001,45 @@ function renderBoard(models) {
}).join(''); }).join('');
} }
function renderKeys(keys) { function renderKeys(keys, keySpend) {
const el = $('keys'); 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) { if (!keys.length) {
el.innerHTML = '<div class="empty">No API keys yet. Create one below.</div>'; el.innerHTML = '<div class="empty">No API keys yet. Create one below.</div>';
renderChatSpend(keySpend);
return; return;
} }
el.innerHTML = keys.map(k => { el.innerHTML = keys.map(k => {
const fp = k.fingerprint || ''; const fp = k.fingerprint || '';
const created = k.created_at ? ' · created ' + k.created_at.slice(0,10) : ''; 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 '<div class="key-row">' + return '<div class="key-row">' +
'<div><div class="key-name">' + escapeHtml(k.name) + '</div>' + '<div><div class="key-name">' + escapeHtml(k.name) + '</div>' +
'<div class="key-meta">' + escapeHtml(created) + '<div class="key-meta">' + escapeHtml(created) +
(fp ? ' · ends …' + escapeHtml(fp) : '') + '</div>' + (fp ? ' · ends …' + escapeHtml(fp) : '') + escapeHtml(spent) + '</div>' +
'</div>' + '</div>' +
'<button class="ghost" data-name="' + escapeHtml(k.name) + '" onclick="revokeKey(this.dataset.name)">Revoke</button>' + '<button class="ghost" data-name="' + escapeHtml(k.name) + '" onclick="revokeKey(this.dataset.name)">Revoke</button>' +
'</div>'; '</div>';
}).join(''); }).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 = '<span class="key-chat-name">Chat (built-in)</span> · ' +
'<span class="key-chat-num">' + fmtMoney(chat.spend) + '</span> spent' +
(chat.requests ? ' · ' + chat.requests.toLocaleString() + ' requests' : '');
} }
function escapeHtml(s) { function escapeHtml(s) {