From 121d23ac2e3bf7ee1d251a34aa9db950ee2ff0d5 Mon Sep 17 00:00:00 2001 From: Robert Karl Date: Mon, 12 Oct 2026 19:27:36 -0700 Subject: [PATCH] Front end: leave the engine's 8-slot reserve out of max_new The engine refuses a request unless prompt + max_new + 8 <= context, but the front end sized max_new as context - prompt, so any client asking for more tokens than remained (pi sends max_tokens 32768 on a 64K card) was refused with "prompt (N) + max_new (M) exceeds the context" on every prompt longer than context - max_tokens. Seen on the 1080 Ti Coder right after a pi compaction left a 48K prompt. Co-Authored-By: Claude Fable 5.1 --- frontend/main.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/frontend/main.cpp b/frontend/main.cpp index d0ff38a..760bc78 100644 --- a/frontend/main.cpp +++ b/frontend/main.cpp @@ -227,13 +227,17 @@ struct Server { p.parser.parse_tool_calls = true; if (!cp.parser.empty()) p.parser.parser.load(cp.parser); + // The engine refuses a request unless prompt + max_new + 8 <= context (generate.cpp, the GEN handler), so + // the slots it keeps back are not ours to promise. Without this a client that asks for "as much as fits" + // (pi sends max_tokens 32768 on a 64K card) was refused on every prompt longer than ctx - max_tokens. + constexpr long long kEngineReserve = 8; const long long n = (long long) p.ids.size(); - if (n + 1 > opt.ctx) { + if (n + 1 + kEngineReserve > opt.ctx) { err = "the prompt is " + std::to_string(n) + " tokens, which does not fit the context of " + std::to_string(opt.ctx); return false; } - long long max_new = opt.ctx - n; + long long max_new = opt.ctx - n - kEngineReserve; for (const char* key : {"max_completion_tokens", "max_tokens"}) if (body.contains(key) && body.at(key).is_number()) { const long long want = body.at(key).get();