Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

feat(local-api): the Local API panel shows what went through it

purpleacf8c3defff9
diff --git a/src-tauri/src/commands/local_api.rs b/src-tauri/src/commands/local_api.rs
index c5e891ae..ec0bdb38 100644
--- a/src-tauri/src/commands/local_api.rs
+++ b/src-tauri/src/commands/local_api.rs
@@ -146,11 +146,13 @@ pub fn modell_zurueckschreiben(json: &mut serde_json::Value, qualifiziert: &str)
pub struct SseModellUmschreiber {
qualifiziert: String,
rest: Vec<u8>,
+ /// Das letzte `usage`, das im Strom vorbeikam (fuer die Statistik).
+ pub usage: Option<(u64, u64)>,
}
impl SseModellUmschreiber {
pub fn neu(qualifiziert: &str) -> Self {
- Self { qualifiziert: qualifiziert.to_string(), rest: Vec::new() }
+ Self { qualifiziert: qualifiziert.to_string(), rest: Vec::new(), usage: None }
}
/// Ein Schub roher Upstream-Bytes rein, die fertigen Zeilen raus.
@@ -173,7 +175,7 @@ impl SseModellUmschreiber {
self.zeile_umschreiben(&zeile)
}
- fn zeile_umschreiben(&self, zeile: &[u8]) -> Vec<u8> {
+ fn zeile_umschreiben(&mut self, zeile: &[u8]) -> Vec<u8> {
let Ok(text) = std::str::from_utf8(zeile) else {
return zeile.to_vec(); // kein UTF-8 — nicht unser Protokoll
};
@@ -196,6 +198,9 @@ impl SseModellUmschreiber {
let Ok(mut json) = serde_json::from_str::<serde_json::Value>(nutz_trim) else {
return zeile.to_vec(); // nicht lesbar — durchreichen, nicht raten
};
+ if let Some(u) = usage_lesen(&json) {
+ self.usage = Some(u);
+ }
if !modell_zurueckschreiben(&mut json, &self.qualifiziert) {
return zeile.to_vec(); // nichts zu tun, also nichts anfassen
}
@@ -552,10 +557,118 @@ impl Default for LocalApiConfig {
}
}
+// ── Was ueber die API gelaufen ist (GH Discussion #4, kreake 2026-09-22) ──
+//
+// "If I connect an IDE with the Local API, I really would appreciate some
+// statistics like connections or context (tokens, etc.)." Gezaehlt wird seit
+// dem letzten Start der API, im Speicher und nirgends sonst: was ein Client
+// fragt, landet in keiner Datei. Tokens kennt nur der Modellserver; wir
+// uebernehmen sein `usage`, und eine Antwort ohne `usage` zaehlt als "ohne
+// Zahlen" statt als geschaetzt.
+
+#[derive(Default)]
+pub struct Nutzung {
+ anfragen: std::sync::atomic::AtomicU64,
+ fehlgeschlagen: std::sync::atomic::AtomicU64,
+ laufend: std::sync::atomic::AtomicU64,
+ prompt_tokens: std::sync::atomic::AtomicU64,
+ antwort_tokens: std::sync::atomic::AtomicU64,
+ ohne_zahlen: std::sync::atomic::AtomicU64,
+ /// Modell und Zeitpunkt (Unix-ms) der letzten Anfrage, seit wann gezaehlt wird.
+ letzte: std::sync::Mutex<Option<(String, u64)>>,
+ seit: std::sync::atomic::AtomicU64,
+}
+
+fn jetzt_ms() -> u64 {
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .map(|d| d.as_millis() as u64)
+ .unwrap_or(0)
+}
+
+impl Nutzung {
+ /// Frisch gezaehlt, ab jetzt. Jeder Start der API bekommt eine neue.
+ pub fn neu() -> Self {
+ let n = Self::default();
+ n.seit.store(jetzt_ms(), std::sync::atomic::Ordering::Relaxed);
+ n
+ }
+
+ /// Eine Anfrage beginnt. Der Waechter zaehlt sie als laufend, bis er
+ /// faellt, beim Streaming also erst mit dem letzten Byte.
+ fn beginn(self: &Arc<Self>, modell: &str) -> Laufend {
+ use std::sync::atomic::Ordering::Relaxed;
+ self.anfragen.fetch_add(1, Relaxed);
+ self.laufend.fetch_add(1, Relaxed);
+ if let Ok(mut l) = self.letzte.lock() {
+ *l = Some((modell.to_string(), jetzt_ms()));
+ }
+ Laufend { nutzung: Arc::clone(self) }
+ }
+
+ fn fehler(&self) {
+ self.fehlgeschlagen.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ }
+
+ /// Das `usage` einer fertigen Antwort, oder keins.
+ fn zahlen(&self, usage: Option<(u64, u64)>) {
+ use std::sync::atomic::Ordering::Relaxed;
+ match usage {
+ Some((p, a)) => {
+ self.prompt_tokens.fetch_add(p, Relaxed);
+ self.antwort_tokens.fetch_add(a, Relaxed);
+ }
+ None => {
+ self.ohne_zahlen.fetch_add(1, Relaxed);
+ }
+ }
+ }
+
+ pub fn als_json(&self) -> serde_json::Value {
+ use std::sync::atomic::Ordering::Relaxed;
+ let letzte = self.letzte.lock().ok().and_then(|l| l.clone());
+ serde_json::json!({
+ "since": self.seit.load(Relaxed),
+ "requests": self.anfragen.load(Relaxed),
+ "failed": self.fehlgeschlagen.load(Relaxed),
+ "active": self.laufend.load(Relaxed),
+ "promptTokens": self.prompt_tokens.load(Relaxed),
+ "completionTokens": self.antwort_tokens.load(Relaxed),
+ "withoutCounts": self.ohne_zahlen.load(Relaxed),
+ "lastModel": letzte.as_ref().map(|(m, _)| m.clone()),
+ "lastAt": letzte.map(|(_, t)| t),
+ })
+ }
+}
+
+/// Haelt eine Anfrage als laufend, solange er lebt.
+pub struct Laufend {
+ nutzung: Arc<Nutzung>,
+}
+
+impl Drop for Laufend {
+ fn drop(&mut self) {
+ self.nutzung.laufend.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
+ }
+}
+
+/// `usage` im OpenAI-Format: Chat und Completions melden prompt_tokens und
+/// completion_tokens, Embeddings nur prompt_tokens.
+pub fn usage_lesen(json: &serde_json::Value) -> Option<(u64, u64)> {
+ let u = json.get("usage")?;
+ let p = u.get("prompt_tokens").and_then(|v| v.as_u64());
+ let a = u.get("completion_tokens").and_then(|v| v.as_u64());
+ if p.is_none() && a.is_none() {
+ return None;
+ }
+ Some((p.unwrap_or(0), a.unwrap_or(0)))
+}
+
#[derive(Clone)]
pub struct LocalApiState {
pub cfg: Arc<RwLock<LocalApiConfig>>,
pub http: reqwest::Client,
+ pub nutzung: Arc<Nutzung>,
}
/// Was `stop` braucht, um wirklich zu stoppen.
@@ -563,6 +676,7 @@ pub struct LocalApiServer {
pub addr: SocketAddr,
pub shutdown: Option<tokio::sync::oneshot::Sender<()>>,
pub handle: Option<tauri::async_runtime::JoinHandle<()>>,
+ pub nutzung: Arc<Nutzung>,
}
impl LocalApiServer {
@@ -704,6 +818,27 @@ async fn handle_models(AxumState(state): AxumState<LocalApiState>) -> Response {
/// ohne Zwischenspeicher weitergegeben. Alles andere waere ein Puffer, der
/// genau die Eigenschaft zerstoert, wegen der jemand `stream: true` schreibt.
async fn weiterleiten(state: LocalApiState, pfad: &str, pflichtfeld: &str, headers: HeaderMap, body: axum::body::Bytes) -> Response {
+ let modell = serde_json::from_slice::<serde_json::Value>(&body)
+ .ok()
+ .and_then(|j| j.get("model").and_then(|m| m.as_str()).map(str::to_string))
+ .unwrap_or_default();
+ let nutzung = Arc::clone(&state.nutzung);
+ let waechter = nutzung.beginn(&modell);
+ let antwort = weiterleiten_mit(state, pfad, pflichtfeld, headers, body, waechter).await;
+ if antwort.status().is_client_error() || antwort.status().is_server_error() {
+ nutzung.fehler();
+ }
+ antwort
+}
+
+async fn weiterleiten_mit(
+ state: LocalApiState,
+ pfad: &str,
+ pflichtfeld: &str,
+ headers: HeaderMap,
+ body: axum::body::Bytes,
+ waechter: Laufend,
+) -> Response {
let mut json: serde_json::Value = match serde_json::from_slice(&body) {
Ok(v) => v,
Err(e) => {
@@ -813,6 +948,10 @@ async fn weiterleiten(state: LocalApiState, pfad: &str, pflichtfeld: &str, heade
)
}
};
+ if status.is_success() {
+ state.nutzung.zahlen(serde_json::from_slice::<serde_json::Value>(&bytes).ok().as_ref().and_then(usage_lesen));
+ }
+ drop(waechter);
let raus = match serde_json::from_slice::<serde_json::Value>(&bytes) {
Ok(mut v) => {
modell_zurueckschreiben(&mut v, &qualifiziert);
@@ -832,9 +971,14 @@ async fn weiterleiten(state: LocalApiState, pfad: &str, pflichtfeld: &str, heade
// im Rest liegt — ein abgerissener Upstream soll Bytes verlieren duerfen,
// wir nicht.
let quelle = antwort.bytes_stream();
+ // Der Waechter reist im Zustand mit: laufend ist die Anfrage, bis das
+ // letzte Byte draussen ist oder der Client auflegt (dann faellt der Strom
+ // samt Waechter).
+ let erfolg = status.is_success();
+ let nutzung = Arc::clone(&state.nutzung);
let strom = futures_util::stream::unfold(
- (quelle, SseModellUmschreiber::neu(&qualifiziert), false),
- |(mut quelle, mut um, fertig)| async move {
+ (quelle, SseModellUmschreiber::neu(&qualifiziert), false, Some(waechter), nutzung),
+ move |(mut quelle, mut um, fertig, mut waechter, nutzung)| async move {
use futures_util::StreamExt;
if fertig {
return None;
@@ -842,15 +986,20 @@ async fn weiterleiten(state: LocalApiState, pfad: &str, pflichtfeld: &str, heade
match quelle.next().await {
Some(Ok(chunk)) => {
let raus = um.schub(&chunk);
- Some((Ok::<_, std::io::Error>(axum::body::Bytes::from(raus)), (quelle, um, false)))
+ Some((Ok::<_, std::io::Error>(axum::body::Bytes::from(raus)), (quelle, um, false, waechter, nutzung)))
+ }
+ Some(Err(e)) => {
+ nutzung.fehler();
+ waechter.take();
+ Some((Err(std::io::Error::other(e.to_string())), (quelle, um, true, waechter, nutzung)))
}
- Some(Err(e)) => Some((
- Err(std::io::Error::other(e.to_string())),
- (quelle, um, true),
- )),
None => {
let rest = um.schluss();
- Some((Ok(axum::body::Bytes::from(rest)), (quelle, um, true)))
+ if erfolg {
+ nutzung.zahlen(um.usage);
+ }
+ waechter.take();
+ Some((Ok(axum::body::Bytes::from(rest)), (quelle, um, true, waechter, nutzung)))
}
}
},
@@ -1182,7 +1331,9 @@ pub async fn start_local_api(
.connect_timeout(std::time::Duration::from_secs(5))
.build()
.map_err(|e| e.to_string())?,
+ nutzung: Arc::new(Nutzung::neu()),
};
+ let nutzung = Arc::clone(&api_state.nutzung);
let listener = tokio::net::TcpListener::bind(addr)
.await
@@ -1200,7 +1351,7 @@ pub async fn start_local_api(
});
if let Ok(mut slot) = state.local_api.lock() {
- *slot = Some(LocalApiServer { addr: echte_addr, shutdown: Some(tx), handle: Some(handle) });
+ *slot = Some(LocalApiServer { addr: echte_addr, shutdown: Some(tx), handle: Some(handle), nutzung });
}
Ok(serde_json::json!({
@@ -1237,6 +1388,17 @@ pub async fn local_api_status(state: tauri::State<'_, crate::state::AppState>) -
Ok(laeuft)
}
+/// Die Zahlen seit dem letzten Start der API, fuer Settings, Local API.
+#[tauri::command]
+pub fn local_api_usage(state: tauri::State<'_, crate::state::AppState>) -> serde_json::Value {
+ state
+ .local_api
+ .lock()
+ .ok()
+ .and_then(|s| s.as_ref().map(|x| x.nutzung.als_json()))
+ .unwrap_or_else(|| serde_json::json!({ "running": false }))
+}
+
/// Ein frisches Token. Aufbewahrt wird es vom Frontend (dort liegt der
/// Schluesselbund-Pfad mitsamt Linux-Rueckfall); hier wird es nur erzeugt,
/// damit der Zufall aus dem System kommt und nicht aus `Math.random`.
@@ -1381,7 +1543,7 @@ mod local_api_echt {
upstreams: default_upstreams(),
cors_origins: cors,
};
- let state = LocalApiState { cfg: Arc::new(RwLock::new(cfg)), http };
+ let state = LocalApiState { cfg: Arc::new(RwLock::new(cfg)), http, nutzung: Arc::new(Nutzung::neu()) };
let l = tokio::net::TcpListener::bind(bind_addr(false, 0)).await.ok()?;
let addr = l.local_addr().ok()?;
let router = build_local_api_router(state);
@@ -1564,6 +1726,7 @@ mod local_api_echt {
})),
http: reqwest::Client::builder()
.connect_timeout(std::time::Duration::from_secs(5)).build().unwrap(),
+ nutzung: Arc::new(Nutzung::neu()),
};
let l = tokio::net::TcpListener::bind(bind_addr(false, port)).await
.unwrap_or_else(|e| panic!("Port {} nicht frei: {}", port, e));
@@ -1839,6 +2002,79 @@ mod local_api_echt {
mod tests {
use super::*;
+ /// GH Discussion #4 (kreake): what went through the API, counted per start.
+ /// A stand-in model server answers once plainly, once as a stream, once
+ /// without `usage`; a fourth request names a model nobody has.
+ #[tokio::test]
+ async fn die_api_zaehlt_was_durch_sie_laeuft() {
+ async fn chat(body: axum::body::Bytes) -> Response {
+ let j: serde_json::Value = serde_json::from_slice(&body).unwrap();
+ let stumm = j["messages"][0]["content"] == "no usage";
+ if j["stream"] == true {
+ let sse = "data: {\"model\":\"tiny\",\"choices\":[{\"delta\":{\"content\":\"hi\"}}]}\n\n\
+ data: {\"model\":\"tiny\",\"choices\":[],\"usage\":{\"prompt_tokens\":7,\"completion_tokens\":3}}\n\n\
+ data: [DONE]\n\n";
+ return Response::builder().header(header::CONTENT_TYPE, "text/event-stream").body(Body::from(sse)).unwrap();
+ }
+ let mut v = serde_json::json!({ "model": "tiny", "choices": [{ "message": { "content": "hi" } }] });
+ if !stumm {
+ v["usage"] = serde_json::json!({ "prompt_tokens": 5, "completion_tokens": 2 });
+ }
+ Json(v).into_response()
+ }
+ let attrappe = Router::new()
+ .route("/v1/models", get(|| async { Json(serde_json::json!({ "data": [{ "id": "tiny" }] })) }))
+ .route("/v1/chat/completions", post(chat));
+ let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let oben = l.local_addr().unwrap();
+ tokio::spawn(async move { let _ = axum::serve(l, attrappe).await; });
+
+ let nutzung = Arc::new(Nutzung::neu());
+ let state = LocalApiState {
+ cfg: Arc::new(RwLock::new(LocalApiConfig {
+ port: 0, lan: false, token: "t0k".into(),
+ upstreams: vec![Upstream { lane_prefix: "ollama".into(), base: format!("http://{}/v1", oben) }],
+ cors_origins: Vec::new(),
+ })),
+ http: reqwest::Client::new(),
+ nutzung: Arc::clone(&nutzung),
+ };
+ let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let api = l.local_addr().unwrap();
+ let router = build_local_api_router(state);
+ tokio::spawn(async move { let _ = axum::serve(l, router).await; });
+
+ let c = reqwest::Client::new();
+ let frage = |modell: &str, stream: bool, text: &str| {
+ c.post(format!("http://{}/v1/chat/completions", api)).bearer_auth("t0k")
+ .json(&serde_json::json!({ "model": modell, "stream": stream, "messages": [{ "role": "user", "content": text }] }))
+ .send()
+ };
+ assert_eq!(frage("ollama/tiny", false, "hi").await.unwrap().status(), 200);
+ let strom = frage("ollama/tiny", true, "hi").await.unwrap();
+ assert!(strom.text().await.unwrap().contains("[DONE]"));
+ assert_eq!(frage("ollama/tiny", false, "no usage").await.unwrap().status(), 200);
+ assert_eq!(frage("ollama/nobody", false, "hi").await.unwrap().status(), 404);
+
+ let z = nutzung.als_json();
+ assert_eq!(z["requests"], 4);
+ assert_eq!(z["failed"], 1);
+ assert_eq!(z["active"], 0, "a finished stream is no longer running");
+ assert_eq!(z["promptTokens"], 12);
+ assert_eq!(z["completionTokens"], 5);
+ assert_eq!(z["withoutCounts"], 1);
+ assert_eq!(z["lastModel"], "ollama/nobody");
+ assert!(z["since"].as_u64().unwrap() > 0);
+ }
+
+ #[test]
+ fn usage_wird_nur_gelesen_wo_es_steht() {
+ assert_eq!(usage_lesen(&serde_json::json!({ "usage": { "prompt_tokens": 4, "completion_tokens": 1 } })), Some((4, 1)));
+ assert_eq!(usage_lesen(&serde_json::json!({ "usage": { "prompt_tokens": 9, "total_tokens": 9 } })), Some((9, 0)));
+ assert_eq!(usage_lesen(&serde_json::json!({ "usage": {} })), None);
+ assert_eq!(usage_lesen(&serde_json::json!({ "choices": [] })), None);
+ }
+
fn katalog() -> Vec<(Lane, String)> {
vec![
(Lane::Engine, "qwen3-8b".into()),
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index f1a24699..a1900aad 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -630,6 +630,7 @@ fn main() {
commands::local_api::stop_local_api,
commands::local_api::local_api_status,
commands::local_api::local_api_new_token,
+ commands::local_api::local_api_usage,
commands::remote::start_remote_server,
commands::remote::revoke_remote_memory,
commands::remote::stop_remote_server,
diff --git a/src/components/settings/LocalApiSettings.tsx b/src/components/settings/LocalApiSettings.tsx
index 22e07bfd..5d419db2 100644
--- a/src/components/settings/LocalApiSettings.tsx
+++ b/src/components/settings/LocalApiSettings.tsx
@@ -3,8 +3,9 @@ import { Copy, Check, KeyRound, Eye, EyeOff, Globe } from 'lucide-react'
import { useLocalApiStore } from '../../stores/localApiStore'
import {
localApiBaseUrl, curlBeispiel, clientFelder, reichweiteText, kannStarten, pruefePort,
- corsText,
+ corsText, nutzungZeilen, ohneZahlenText, type LocalApiNutzung,
} from '../../lib/local-api'
+import { backendCall } from '../../api/backend'
import { InlineToggle } from './InlineToggle'
import { HINWEIS_TEXT } from '../../lib/hinweis'
@@ -33,6 +34,26 @@ export function LocalApiSettings() {
useEffect(() => { auffrischen() }, [auffrischen])
+ // GH Discussion #4 (kreake): what went through the API since it started.
+ // Asked every three seconds while it runs and the panel is open.
+ // Mit dem Zeitpunkt des Abrufs, damit "vor wie langem" nicht im Rendern rechnet.
+ const [nutzung, setNutzung] = useState<{ u: LocalApiNutzung; jetzt: number } | null>(null)
+ useEffect(() => {
+ // Gestoppt: nichts fragen. Das Feld blendet sich ueber `laeuft` aus, und
+ // ein Neustart holt sofort frische Zahlen.
+ if (!laeuft) return
+ let aus = false
+ const holen = async () => {
+ try {
+ const u = await backendCall<LocalApiNutzung & { running?: boolean }>('local_api_usage')
+ if (!aus) setNutzung(u.running === false ? null : { u, jetzt: Date.now() })
+ } catch { /* die Zahlen sind Beiwerk, die API laeuft auch ohne */ }
+ }
+ void holen()
+ const t = setInterval(holen, 3000)
+ return () => { aus = true; clearInterval(t) }
+ }, [laeuft])
+
const base = localApiBaseUrl(port, lan)
const startbar = kannStarten(token, port)
const portUrteil = pruefePort(port)
@@ -82,6 +103,21 @@ export function LocalApiSettings() {
)}
{fehler && <div className={`t-micro ${HINWEIS_TEXT.fehler}`}>{fehler}</div>}
+ {laeuft && nutzung && (
+ <div className="space-y-1" data-testid="local-api-usage">
+ <div className="t-micro text-gray-500">Since the API started</div>
+ <div className="grid grid-cols-2 gap-x-4 gap-y-0.5">
+ {nutzungZeilen(nutzung.u, nutzung.jetzt).map(({ feld, wert }) => (
+ <div key={feld} className={`flex items-center justify-between gap-2 ${feld === 'Last model' ? 'col-span-2' : ''}`}>
+ <span className="t-micro text-gray-500">{feld}</span>
+ <span className="t-mono text-gray-300 truncate" title={wert}>{wert}</span>
+ </div>
+ ))}
+ </div>
+ {ohneZahlenText(nutzung.u) && <div className="t-micro text-gray-600">{ohneZahlenText(nutzung.u)}</div>}
+ </div>
+ )}
+
<div className="grid grid-cols-2 gap-3">
<label className="t-micro text-gray-500">
Port
diff --git a/src/components/settings/__tests__/the-local-api-shows-what-went-through-it.test.tsx b/src/components/settings/__tests__/the-local-api-shows-what-went-through-it.test.tsx
new file mode 100644
index 00000000..e38bf1cd
--- /dev/null
+++ b/src/components/settings/__tests__/the-local-api-shows-what-went-through-it.test.tsx
@@ -0,0 +1,60 @@
+// @vitest-environment jsdom
+/**
+ * GH Discussion #4 (kreake, 2026-09-22): "If I connect an IDE with the Local
+ * API, I really would appreciate some statistics like connections or context
+ * (tokens, etc.)." The panel now shows what went through the API since it
+ * started; the counting itself is tested in commands/local_api.rs.
+ *
+ * Run: npx vitest run src/components/settings/__tests__/the-local-api-shows-what-went-through-it.test.tsx
+ */
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+import { cleanup, render, screen } from '@testing-library/react'
+
+const usage = {
+ since: Date.now() - 600_000, requests: 1234, failed: 2, active: 1,
+ promptTokens: 48_210, completionTokens: 9_034, withoutCounts: 3,
+ lastModel: 'ollama/qwen3:8b', lastAt: Date.now() - 5_000,
+}
+const backendCall = vi.fn(async (cmd: string) => (cmd === 'local_api_usage' ? usage : cmd === 'local_api_status' ? { running: true } : null))
+vi.mock('../../../api/backend', async (o) => ({
+ ...(await o<typeof import('../../../api/backend')>()),
+ isTauri: () => true,
+ backendCall: (...a: unknown[]) => backendCall(...(a as [string])),
+}))
+
+import { LocalApiSettings } from '../LocalApiSettings'
+import { useLocalApiStore } from '../../../stores/localApiStore'
+import { nutzungZeilen, ohneZahlenText } from '../../../lib/local-api'
+
+beforeEach(() => {
+ cleanup()
+ backendCall.mockClear()
+ useLocalApiStore.setState({ laeuft: true, token: 'abc', auffrischen: async () => {} } as never)
+})
+
+describe('the Local API panel', () => {
+ it('shows requests, running, tokens and the last model while it runs', async () => {
+ render(<LocalApiSettings />)
+ await screen.findByTestId('local-api-usage')
+ expect(screen.getByText('1,234 (2 failed)')).toBeTruthy()
+ expect(screen.getByText('48,210')).toBeTruthy()
+ expect(screen.getByText('9,034')).toBeTruthy()
+ expect(screen.getByText('ollama/qwen3:8b, just now')).toBeTruthy()
+ expect(screen.getByText('3 answers came without token counts from the model server, so the totals above leave them out.')).toBeTruthy()
+ })
+
+ // Negative control: a stopped API asks for nothing and shows nothing.
+ it('shows nothing and asks nothing while stopped', () => {
+ useLocalApiStore.setState({ laeuft: false } as never)
+ render(<LocalApiSettings />)
+ expect(screen.queryByTestId('local-api-usage')).toBeNull()
+ expect(backendCall.mock.calls.filter((c) => c[0] === 'local_api_usage')).toHaveLength(0)
+ })
+
+ it('a clean run has no failed count and no missing-counts line', () => {
+ const clean = { ...usage, failed: 0, withoutCounts: 0, lastModel: null, lastAt: null }
+ expect(nutzungZeilen(clean, Date.now())[0].wert).toBe('1,234')
+ expect(nutzungZeilen(clean, Date.now())[4].wert).toBe('none yet')
+ expect(ohneZahlenText(clean)).toBeNull()
+ })
+})
diff --git a/src/lib/local-api.ts b/src/lib/local-api.ts
index 1088f0d2..2a9a214e 100644
--- a/src/lib/local-api.ts
+++ b/src/lib/local-api.ts
@@ -167,3 +167,48 @@ export function corsText(origins: string[]): string {
? `Open to ${origins[0]} only.`
: `Open to ${origins.length} origins: ${origins.join(', ')}.`
}
+
+// ── Was ueber die API gelaufen ist (GH Discussion #4, kreake 2026-09-22) ──
+
+/** Die Zahlen, die `local_api_usage` seit dem letzten Start liefert. */
+export interface LocalApiNutzung {
+ since: number
+ requests: number
+ failed: number
+ active: number
+ promptTokens: number
+ completionTokens: number
+ withoutCounts: number
+ lastModel: string | null
+ lastAt: number | null
+}
+
+const zahl = (n: number) => n.toLocaleString('en-US')
+
+function vorWieLangem(ms: number, jetzt: number): string {
+ const s = Math.max(0, Math.round((jetzt - ms) / 1000))
+ if (s < 60) return 'just now'
+ const m = Math.round(s / 60)
+ if (m < 60) return `${m} min ago`
+ const h = Math.round(m / 60)
+ return h < 24 ? `${h} h ago` : `${Math.round(h / 24)} d ago`
+}
+
+/** Die Zeilen fuer das Panel. Sichtbarer Text ist Englisch. */
+export function nutzungZeilen(u: LocalApiNutzung, jetzt: number): Array<{ feld: string; wert: string }> {
+ return [
+ { feld: 'Requests', wert: u.failed > 0 ? `${zahl(u.requests)} (${zahl(u.failed)} failed)` : zahl(u.requests) },
+ { feld: 'Running now', wert: zahl(u.active) },
+ { feld: 'Tokens in', wert: zahl(u.promptTokens) },
+ { feld: 'Tokens out', wert: zahl(u.completionTokens) },
+ { feld: 'Last model', wert: u.lastModel ? `${u.lastModel}, ${vorWieLangem(u.lastAt ?? jetzt, jetzt)}` : 'none yet' },
+ ]
+}
+
+/** Tokens kennt nur der Modellserver. Wo er keine meldet, steht das da,
+ * statt dass eine Schaetzung als Zaehlung auftritt. */
+export function ohneZahlenText(u: LocalApiNutzung): string | null {
+ if (u.withoutCounts === 0) return null
+ const wer = u.withoutCounts === 1 ? '1 answer' : `${zahl(u.withoutCounts)} answers`
+ return `${wer} came without token counts from the model server, so the totals above leave them out.`
+}