Dashboard: time to first token tile and chart

vLLM: from the vllm:time_to_first_token_seconds histogram. MLX: the proxy
times streamed requests from arrival to the first generated token. Each
chart sample is the average over requests that got their first token in
that 2 s window; idle samples are gaps, and isolated samples now draw as
dots in every chart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QaE1hpvyZTJixEbUUMj9Ca
This commit is contained in:
neil 2026-11-02 08:12:07 -08:00
parent 8611d92cf4
commit 994659b5e2
3 changed files with 50 additions and 8 deletions

View file

@ -97,12 +97,15 @@ class Stats:
self.gen_total = 0
self.prompt_total = 0
self.requests_total = 0
self.ttft_sum = 0.0 # seconds from request received to first generated token,
self.ttft_count = 0 # streamed requests only (non-streamed ones arrive all at once)
self.ready = False # set once the model has actually answered (see _readiness_probe)
def snapshot(self):
with self.lock:
return {"running": self.running, "gen_total": self.gen_total,
"prompt_total": self.prompt_total, "requests_total": self.requests_total}
"prompt_total": self.prompt_total, "requests_total": self.requests_total,
"ttft_sum": self.ttft_sum, "ttft_count": self.ttft_count}
def make_handler(inner_port, served_name, context, key, stats):
@ -152,6 +155,7 @@ def make_handler(inner_port, served_name, context, key, stats):
def do_POST(self):
if not self._authed():
return
self._t0 = time.monotonic()
n = int(self.headers.get("Content-Length") or 0)
raw = self.rfile.read(n) if n else b""
strip_usage = False
@ -196,6 +200,7 @@ def make_handler(inner_port, served_name, context, key, stats):
self.end_headers()
self.close_connection = True
buf = b""
self._first = False
while True:
chunk = resp.read1(65536) if hasattr(resp, "read1") else resp.read(4096)
if not chunk:
@ -239,6 +244,7 @@ def make_handler(inner_port, served_name, context, key, stats):
except ValueError:
return False
u = d.get("usage")
first_token = False
with stats.lock:
if u: # final usage chunk (stream_options.include_usage) is exact; prefer it
stats.prompt_total += int(u.get("prompt_tokens") or 0)
@ -246,6 +252,11 @@ def make_handler(inner_port, served_name, context, key, stats):
delta = c.get("delta") or {}
if delta.get("content") or delta.get("reasoning") or delta.get("reasoning_content") or delta.get("tool_calls"):
stats.gen_total += 1 # mlx streams one token per chunk
first_token = True
if first_token and not self._first and getattr(self, "_t0", None) is not None:
self._first = True
stats.ttft_sum += time.monotonic() - self._t0
stats.ttft_count += 1
return bool(u) and not d.get("choices")
return Proxy