From 5f563ce3a316fbc10c710acd86fb476d3836e0bd Mon Sep 17 00:00:00 2001 From: pete Date: Sun, 27 Sep 2026 13:11:09 +0100 Subject: [PATCH] serve: optional --fit-max-tokens clamps the output cap to remaining context instead of rejecting Agentic clients legitimately request the model's full output cap with a long prompt (e.g. prompt 131,888 + max_tokens 130,298 on a 262,144 window: rejected over a 42-token overshoot). Default behaviour is unchanged: without the flag the server still rejects with 400, like llama.cpp. With --fit-max-tokens the server clamps max_tokens to the remaining room (max_context - CTX_SLACK - prompt, floor 1 token) and answers. Measured on a 262,144-token host: prompt 251,455 + max_tokens 131,072 -> HTTP 200, completion_tokens 10,681 (= 262,144 - 251,455 - 8), finish length. Flag off: byte-identical 400. Small requests unaffected in both modes. --- serve/server.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/serve/server.py b/serve/server.py index 86b0e483..0691e647 100644 --- a/serve/server.py +++ b/serve/server.py @@ -375,8 +375,10 @@ class Detokenizer: class Service: def __init__(self, engine: Engine, tokenizer, template: ChatTemplate, model_name: str = "qwen3.8-flash-next", - vision: Vision | None = None, sampling_defaults: dict | None = None): + vision: Vision | None = None, sampling_defaults: dict | None = None, + fit_max_tokens: bool = False): self.engine, self.tok, self.template, self.model, self.vision = engine, tokenizer, template, model_name, vision + self.fit_max_tokens = fit_max_tokens # --fit-max-tokens: clamp the output cap instead of 400 self.sampling_defaults = dict(sampling_defaults or {}) # the run config's `sampling` block self.fifo = threading.Lock() self.embeddings = threading.local() # the current request's image embeddings file (GENI) @@ -468,8 +470,10 @@ class Service: f"({self.engine.max_context}); requests are never truncated") max_new = room elif max_new > room: - raise ValueError(f"prompt ({len(ids)} tokens) + max tokens ({max_new}) exceeds the context " - f"({self.engine.max_context}); requests are never truncated") + if not self.fit_max_tokens: + raise ValueError(f"prompt ({len(ids)} tokens) + max tokens ({max_new}) exceeds the context " + f"({self.engine.max_context}); requests are never truncated") + max_new = max(1, room) # --fit-max-tokens: a shorter completion beats a 400 return ids, kwargs.get("enable_thinking", True) is not False, max_new def _note(self, n, evs): @@ -989,6 +993,9 @@ def main() -> int: ap.add_argument("--tokenizer", default=str(ROOT / "pack/full/tokenizer"), help="pack tokenizer directory (falls back to a byte tokenizer if absent)") 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)") 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() @@ -1034,7 +1041,7 @@ 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) + sampling_defaults=sampling_defaults, fit_max_tokens=a.fit_max_tokens) 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, "