Skip to content

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: --fit-max-tokens follow-ups (PR #24)

Niko122120351c60304b
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index 39b96d257..9c3907e22 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -216,7 +216,10 @@ print(r.choices[0].message.content)
15 s, and `GET /status` says what it is doing (`reading the prompt`, `answering`, tokens so far). Closing the
connection or pressing stop in your app really stops the model, so the next request starts at once.
- **Chat apps.** Any app with an "OpenAI-compatible" provider works: base URL `http://127.0.0.1:8080/v1`, any API key.
-- **Context.** Chosen in setup (8K-262K). Requests longer than that are refused, never silently cut.
+- **Context.** Chosen in setup (8K-262K). Requests longer than that are refused, never silently cut. A request whose
+ `max_tokens` would run past the context is refused too (400); agents that always ask for their full output cap
+ can instead get it shortened to the room left: add `"fit_max_tokens": true` to `strata-<model>.json` (or pass
+ `--fit-max-tokens` to `serve/server.py`). A prompt that leaves no room at all is still refused.
- **From other devices / the internet.** The server listens on your PC only (`127.0.0.1`). To reach it from elsewhere,
put a tunnel in front of it, for example [cloudflared](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/do-more-with-tunnels/trycloudflare/):
`cloudflared tunnel --url http://127.0.0.1:8080`. **Set a key first**, or anyone with the link can use your PC:
diff --git a/serve/server.py b/serve/server.py
index 0691e6472..31800602a 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -464,7 +464,7 @@ class Service:
f.write(path.read_bytes())
self.embeddings.path = combined
room = self.engine.max_context - CTX_SLACK - len(ids)
- if max_new is None or max_new <= 0:
+ if max_new is None or max_new <= 0 or (self.fit_max_tokens and room < 1):
if room < 1:
raise ValueError(f"prompt ({len(ids)} tokens) leaves no room to answer in the context "
f"({self.engine.max_context}); requests are never truncated")
@@ -995,7 +995,7 @@ def main() -> int:
ap.add_argument("--open", action="store_true", help="open the local page in the browser once the model is ready")
ap.add_argument("--fit-max-tokens", action="store_true",
help="clamp max_tokens to the remaining context instead of rejecting the request "
- "(default: reject with 400, like llama.cpp)")
+ "(default: reject with 400, like llama.cpp; also \"fit_max_tokens\": true in the config)")
ap.add_argument("--api-key", default=os.environ.get("STRATA_API_KEY", ""),
help="require this key on /v1/* (Authorization: Bearer ... or x-api-key); also $STRATA_API_KEY")
a = ap.parse_args()
@@ -1041,7 +1041,8 @@ def main() -> int:
tpl = tpath / "chat_template.jinja"
svc = Service(engine, tok, ChatTemplate(tpl if tpl.exists() else ROOT / "serve/chat_template.jinja"),
model_name=cfg.get("model_name", "qwen3.8-flash-next"), vision=vision,
- sampling_defaults=sampling_defaults, fit_max_tokens=a.fit_max_tokens)
+ sampling_defaults=sampling_defaults,
+ fit_max_tokens=a.fit_max_tokens or cfg.get("fit_max_tokens") is True)
svc.api_key = a.api_key or cfg.get("api_key", "")
httpd = serve(svc, host=a.host, port=a.port)
print(f"ready: http://{a.host}:{a.port}/v1 (OpenAI: /v1/chat/completions, Anthropic: /v1/messages, "
diff --git a/serve/test_server.py b/serve/test_server.py
index 00cc0b6dc..baa86a62f 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -126,6 +126,45 @@ class MaxTokens(unittest.TestCase):
del os.environ["STRATA_DEBUG"]
+class FitMaxTokens(unittest.TestCase):
+ """PR #24: --fit-max-tokens clamps an explicit budget that overshoots the context instead of a 400."""
+
+ @classmethod
+ def setUpClass(cls):
+ tok = ByteTokenizer()
+ cls.engine = RecordingEngine(tok, "</think>\n\n" + ANSWER, max_context=CTX)
+ cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja"), fit_max_tokens=True)
+ 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()
+
+ post = MaxTokens.post
+ call = MaxTokens.call
+
+ def test_overshoot_is_clamped_to_the_room(self):
+ for api in ("openai", "anthropic"):
+ with self.subTest(api=api):
+ s, b, pt, ct = self.call(api, max_tokens=CTX)
+ self.assertEqual(s, 200, b)
+ self.assertEqual(self.engine.last_max_new, CTX - CTX_SLACK - pt)
+
+ def test_a_budget_that_fits_is_unchanged(self):
+ s, b, _, ct = self.call("openai", max_tokens=50)
+ self.assertEqual(s, 200, b)
+ self.assertEqual(self.engine.last_max_new, 50)
+
+ def test_no_room_is_still_a_400(self):
+ _, _, pt0, _ = self.call("openai", max_tokens=1)
+ overhead = pt0 - len("hi")
+ s, b, _, _ = self.call("openai", text="y" * (CTX - CTX_SLACK - overhead), max_tokens=100)
+ self.assertEqual(s, 400, b)
+ self.assertIn("no room to answer", b["error"]["message"])
+
+
class WebApp(unittest.TestCase):
"""The web app (PR #22's dashboard idea, rebuilt): its page and files, and GET /metrics."""