Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
serve: report the cached prompt tokens, llama.cpp timings and GET /v1/status
architectdsc4d549aa25c4
diff --git a/serve/server.py b/serve/server.pyindex 2cdd84f76..cdc96237b 100644--- a/serve/server.py+++ b/serve/server.py@@ -547,6 +547,9 @@ class Service:# since the server started (the Monitor's totals, issue #35)self.totals = {"since": time.time(), "requests": 0, "prompt_tokens": 0, "reused": 0, "output_tokens": 0,"prompt_ms": 0.0, "decode_ms": 0.0}+ self.last_timings = None # the last finished request's, llama.cpp's names (/v1/status)+ self.last_request_at = None # when a request last started or finished+ self.started_at = time.time()self.status_lock = threading.Lock()self.mcp = None # serve/mcp.py's McpHub when MCP servers are configuredself.stop_ids = set(tokenizer.encode(IM_END, parse_special=True) +@@ -632,6 +635,41 @@ class Service:"hardware_static":tel["static"], "history": tel["history"], "time": now}+ def v1_status(self) -> dict:+ """GET /v1/status: what this server is and does, for a client that would rather ask than guess (a front-end+ that polls its OpenAI-compatible server's status, collabosm's for one): the model and its window, images,+ the APIs, what is running, and the last request's timings in llama.cpp's names. /metrics has the rest."""+ with self.status_lock:+ s, totals = dict(self.status), dict(self.totals)+ last_t, last_at = (dict(self.last_timings) if self.last_timings else None), self.last_request_at+ tel = self.telemetry.snapshot() if getattr(self, "telemetry", None) else {"now": {}, "static": {}}+ hw, static = tel.get("now") or {}, tel.get("static") or {}++ def scaled(v, unit, digits=0):+ return round(v / unit, digits) if isinstance(v, (int, float)) else None++ busy, ctx = bool(s.get("busy")), self.engine.max_context+ images = self.vision is not None+ return {+ "service": "strata", "model": self.model,+ "engine": (getattr(self.engine, "info", {}) or {}).get("version"),+ "started": int(self.started_at), "uptime_s": int(time.time() - self.started_at),+ "cache_max_tokens": ctx,+ "context": {"native": ctx, "max_positions": ctx},+ "concurrency": {"serving": 1, "requested": 1}, # one request at a time; more wait their turn+ "dialects": ["/v1/chat/completions", "/v1/messages"],+ "vision": {"enabled": images, "available": images, "error": None},+ "activity": {"requests": totals["requests"] + int(busy), "in_flight": int(busy) + int(s.get("queued") or 0),+ "last_request_at": int(last_at) if last_at else None},+ "last_timings": last_t,+ "machine": {+ "at": int(time.time()),+ "gpu": {"name": static.get("gpu_name"), "used_mib": scaled(hw.get("gpu_mem_used"), 2 ** 20),+ "total_mib": scaled(hw.get("gpu_mem_total"), 2 ** 20), "util_pct": hw.get("gpu_util"),+ "temp_c": hw.get("gpu_temp"), "power_w": hw.get("gpu_power")} if static.get("gpu_name") else None,+ "ram": {"used_gib": scaled(hw.get("ram_used"), 2 ** 30, 1),+ "total_gib": scaled(hw.get("ram_total"), 2 ** 30, 1)} if hw.get("ram_total") else None}}+def prepare(self, messages, tools, kwargs, max_new=None):"""-> (ids, thinking, max_new). An unset or non-positive max_new (some clients send -1) means "unlimited":the rest of the context."""@@ -721,6 +759,7 @@ class Service:sampling = {**defaults, **req_values}parser = OutputParser(thinking=thinking, tools=tools, stream_tools=True)detok, n, finish = Detokenizer(self.tok), 0, "length"+ timings, before = None, None # this request's timings; the engine's `last` before itraw_ids = [] # every generated id (STRATA_DEBUG: dump raw model text)emb = getattr(self.embeddings, "path", None)with self.status_lock:@@ -739,6 +778,8 @@ class Service:with self.status_lock:self.status.update(busy=True, phase="reading the prompt", prompt_tokens=len(ids), generated=0,started=time.time(), first_token=None, tool=None, tail="", max_tokens=max_new)+ self.last_request_at = time.time()+ before = getattr(self.engine, "last", None)last_print = time.time()gen = self.engine.generate(ids, max_new, sampling, cancel, embeddings=emb) if emb else \self.engine.generate(ids, max_new, sampling, cancel)@@ -804,6 +845,11 @@ class Service:t["output_tokens"] += nt["prompt_ms"] += last.get("prompt_ms") or 0.0t["decode_ms"] += last.get("decode_ms") or 0.0+ fresh = getattr(self.engine, "last", None)+ if fresh is not None and fresh is not before: # the engine's clock for THIS request+ timings = request_timings(len(ids), n, last)+ self.last_timings = dict(timings, at=int(time.time())) if timings else None+ self.last_request_at = time.time()now = time.time()el = now - self.status.get("started", now)ft = self.status.get("first_token")@@ -816,7 +862,25 @@ class Service:self.status["busy"] = Falsefor ev in parser.finish():yield "event", ev- yield "done", {"finish": finish, "completion_tokens": n}+ yield "done", {"finish": finish, "completion_tokens": n, "reused": (timings or {}).get("cache_n", 0),+ "timings": timings}+++def request_timings(prompt_tokens: int, generated: int, last: dict) -> dict | None:+ """One request's `timings` in llama.cpp's names (what its clients show as speed), from the engine's own clock+ (StrataEngine.last): prompt_n is what was read, cache_n what the conversation cache already held. None when the+ engine keeps no clock (MockEngine)."""+ if last.get("prompt_ms") is None:+ return None+ cache_n = int(last.get("reused") or 0)+ prompt_n, prompt_ms, decode_ms = max(0, prompt_tokens - cache_n), float(last["prompt_ms"]), float(last.get("decode_ms") or 0)+ decoded = int(last.get("generated") or generated) # the engine's count gives its rate, as /metrics does+ return {"cache_n": cache_n, "prompt_n": prompt_n, "prompt_ms": round(prompt_ms, 1),+ "prompt_per_token_ms": round(prompt_ms / prompt_n, 3) if prompt_n else None,+ "prompt_per_second": round(prompt_n / (prompt_ms / 1000), 1) if prompt_n and prompt_ms > 0 else None,+ "predicted_n": generated, "predicted_ms": round(decode_ms, 1),+ "predicted_per_token_ms": round(decode_ms / decoded, 3) if decoded else None,+ "predicted_per_second": round(decoded / (decode_ms / 1000), 1) if decoded and decode_ms > 0 else None}def _debug_req(api, req, messages, tools, max_new, thinking, prompt_tokens):@@ -967,7 +1031,11 @@ def openai_chunks(svc: Service, req: dict, ids, thinking, tools, max_new, cancellast = chunk({}, finish)pt = x.get("prompt_tokens", len(ids)) # after MCP rounds: the last round's promptlast["usage"] = {"prompt_tokens": pt, "completion_tokens": x["completion_tokens"],- "total_tokens": pt + x["completion_tokens"]}+ "total_tokens": pt + x["completion_tokens"],+ # the part of the prompt the conversation cache already held (OpenAI's field)+ "prompt_tokens_details": {"cached_tokens": x.get("reused") or 0}}+ if x.get("timings"):+ last["timings"] = x["timings"] # llama.cpp's field: the speed its clients showyield last@@ -997,9 +1065,12 @@ def openai_collect(chunks) -> dict:msg["tool_calls"] = callsif mcp:msg["strata_mcp"] = mcp- return {"id": last["id"], "object": "chat.completion", "created": last["created"], "model": last["model"],- "choices": [{"index": 0, "message": msg, "finish_reason": last["choices"][0]["finish_reason"]}],- "usage": last["usage"]}+ out = {"id": last["id"], "object": "chat.completion", "created": last["created"], "model": last["model"],+ "choices": [{"index": 0, "message": msg, "finish_reason": last["choices"][0]["finish_reason"]}],+ "usage": last["usage"]}+ if last.get("timings"):+ out["timings"] = last["timings"]+ return out# ------------------------------------------------------------------------------------------------ Anthropic@@ -1059,8 +1130,12 @@ def anthropic_events(svc: Service, req: dict, ids, thinking, tools, max_new, canyield close()stop = "tool_use" if used_tool and x["finish"] == "stop" else \{"stop": "end_turn", "length": "max_tokens", "cancel": "end_turn"}[x["finish"]]+ # the final counts, Anthropic's way: input_tokens leaves out what the conversation cache already held,+ # which is cache_read_input_tokens (message_start could only say the whole prompt)+ reused = min(x.get("reused") or 0, len(ids))yield "message_delta", {"type": "message_delta", "delta": {"stop_reason": stop, "stop_sequence": None},- "usage": {"output_tokens": x["completion_tokens"]}}+ "usage": {"input_tokens": len(ids) - reused, "cache_read_input_tokens": reused,+ "output_tokens": x["completion_tokens"]}}yield "message_stop", {"type": "message_stop"}@@ -1087,7 +1162,7 @@ def anthropic_collect(events) -> dict:b["input"] = json.loads(b.pop("_json") or "{}")elif name == "message_delta":msg["stop_reason"] = e["delta"]["stop_reason"]- msg["usage"]["output_tokens"] = e["usage"]["output_tokens"]+ msg["usage"].update(e["usage"])msg["content"] = blocksreturn msg@@ -1191,6 +1266,9 @@ def make_handler(svc: Service):elif path == "/v1/models":if self._authorized():self._json(200, {"object": "list", "data": [{"id": svc.model, "object": "model"}]})+ elif path == "/v1/status":+ if self._authorized():+ self._json(200, svc.v1_status())else:self._json(404, {"error": {"message": "not found"}})diff --git a/serve/test_server.py b/serve/test_server.pyindex 7ce5ec9ea..8bd5e7fb1 100644--- a/serve/test_server.py+++ b/serve/test_server.py@@ -526,5 +526,108 @@ class WebApp(unittest.TestCase):self.svc.api_key = ""+class ClockedEngine(MockEngine):+ """The mock engine with StrataEngine's clock: `last` as the engine's DONE line gives it, the conversation cache+ holding the first REUSED tokens of every prompt."""+ REUSED = 5++ def generate(self, ids, max_new, sampling, cancel, embeddings=None):+ n = 0+ try:+ for t in super().generate(ids, max_new, sampling, cancel, embeddings):+ n += 1+ yield t+ finally: # as StrataEngine reads its DONE line: also when the server closes the request at a stop token+ self.last = {"generated": n, "prompt_tokens": len(ids), "prompt_ms": 40.0, "decode_ms": 20.0 * n,+ "finish": "stop", "reused": min(self.REUSED, len(ids)), "hits": 9, "lookups": 10}+++class UsageAndStatus(unittest.TestCase):+ """What clients read besides the text: the part of the prompt the conversation cache held (OpenAI's+ prompt_tokens_details.cached_tokens, Anthropic's cache_read_input_tokens), llama.cpp's timings, GET /v1/status."""++ @classmethod+ def setUpClass(cls):+ tok = ByteTokenizer()+ cls.engine = ClockedEngine(tok, "</think>\n\nok", max_context=CTX)+ cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja"))+ cls.httpd = serve(cls.svc, port=0)+ cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}"++ @classmethod+ def tearDownClass(cls):+ cls.httpd.shutdown()+ cls.httpd.server_close()++ def request(self, path, body=None):+ req = urllib.request.Request(self.base + path, data=None if body is None else json.dumps(body).encode(),+ headers={"Content-Type": "application/json", "anthropic-version": "2023-06-01"})+ with urllib.request.urlopen(req, timeout=30) as r:+ return r.status, r.read()++ def chat(self, path, stream=False):+ body = {"model": "x", "max_tokens": 20, "messages": [{"role": "user", "content": "hi"}], "stream": stream}+ status, raw = self.request(path, body)+ self.assertEqual(status, 200)+ if not stream:+ return json.loads(raw)+ return [json.loads(line[6:]) for line in raw.decode().splitlines()+ if line.startswith("data: {")]++ def test_openai(self):+ b = self.chat("/v1/chat/completions")+ u, t = b["usage"], b["timings"]+ self.assertEqual(u["prompt_tokens_details"]["cached_tokens"], ClockedEngine.REUSED)+ self.assertEqual(t["cache_n"], ClockedEngine.REUSED)+ self.assertEqual(t["prompt_n"] + t["cache_n"], u["prompt_tokens"])+ self.assertEqual(t["predicted_n"], u["completion_tokens"])+ self.assertAlmostEqual(t["prompt_per_second"], t["prompt_n"] / 0.040, delta=0.1)+ self.assertAlmostEqual(t["predicted_per_second"], 50.0, delta=0.1) # 20 ms a token++ def test_openai_stream(self):+ last = self.chat("/v1/chat/completions", stream=True)[-1]+ self.assertEqual(last["usage"]["prompt_tokens_details"]["cached_tokens"], ClockedEngine.REUSED)+ self.assertEqual(last["timings"]["cache_n"], ClockedEngine.REUSED)++ def test_anthropic(self):+ u = self.chat("/v1/messages")["usage"]+ self.assertEqual(u["cache_read_input_tokens"], ClockedEngine.REUSED)+ self.assertEqual(u["input_tokens"] + u["cache_read_input_tokens"], len(self.engine.last_prompt))+ self.assertGreater(u["output_tokens"], 0)++ def test_v1_status(self):+ self.chat("/v1/chat/completions")+ status, raw = self.request("/v1/status")+ self.assertEqual(status, 200)+ s = json.loads(raw)+ self.assertEqual(s["model"], self.svc.model)+ self.assertEqual(s["context"]["max_positions"], CTX)+ self.assertEqual(s["concurrency"]["serving"], 1)+ self.assertFalse(s["vision"]["available"])+ self.assertEqual(s["activity"]["in_flight"], 0)+ self.assertGreaterEqual(s["activity"]["requests"], 1)+ self.assertEqual(s["last_timings"]["cache_n"], ClockedEngine.REUSED)+ self.assertIn("at", s["last_timings"])++ def test_no_clock(self):+ """An engine without a clock (MockEngine): no timings, nothing cached."""+ tok = ByteTokenizer()+ svc = Service(MockEngine(tok, "</think>\n\nok", max_context=CTX), tok,+ ChatTemplate(ROOT / "serve/chat_template.jinja"))+ httpd = serve(svc, port=0)+ try:+ data = json.dumps({"model": "x", "max_tokens": 5, "messages": [{"role": "user", "content": "hi"}]}).encode()+ req = urllib.request.Request(f"http://127.0.0.1:{httpd.server_address[1]}/v1/chat/completions", data=data,+ headers={"Content-Type": "application/json"})+ with urllib.request.urlopen(req, timeout=30) as r:+ b = json.loads(r.read())+ self.assertNotIn("timings", b)+ self.assertEqual(b["usage"]["prompt_tokens_details"]["cached_tokens"], 0)+ self.assertIsNone(svc.v1_status()["last_timings"])+ finally:+ httpd.shutdown()+ httpd.server_close()++if __name__ == "__main__":unittest.main()