diff --git a/serve/server.py b/serve/server.py index 3f044fcf..c21f335a 100644 --- a/serve/server.py +++ b/serve/server.py @@ -142,6 +142,7 @@ VISION_START = "<|vision_start|>" # broken state that answers one token forever (an issue saw 36,689 tokens of "!"). The config's "repeat_stop_tokens" # sets it; 0 turns it off. REPEAT_STOP_TOKENS = 256 +ANSWER_RESERVE_MIN = 512 # #984: the tokens kept for the answer when a thinking budget is cut down to fit max_tokens # #123: what closes the thinking when it reaches reasoning_budget_tokens (the model's own end-of-thinking tag after it) # #1053 (opt-in "reasoning_close_retry": true): a reply that ends on its stop token still inside , with no answer # and no call, is continued once with the thinking closed (the same way the budget's wrap-up closes it) @@ -3539,6 +3540,14 @@ class Service: sampling = {**defaults, **req_values} # #123: read after the merge, so a budget shared through POST /settings is seen like the other keys budget = self.reasoning_budget(sampling) if thinking else None + if budget and max_new and max_new > 0: + # #984: a budget at or near max_tokens never fires before the reply is cut off while still thinking, so + # it has no answer. Close the thinking early enough to leave room for one. + reserve = min(max(ANSWER_RESERVE_MIN, max_new // 4), max_new // 2) + if budget > max_new - reserve: + print(f"[strata] thinking capped at {max_new - reserve} of max_tokens {max_new} to leave room for " + f"the answer (budget {budget})", flush=True) + budget = max(1, max_new - reserve) parser = OutputParser(thinking=thinking, tools=tools, stream_tools=True, recover=self.tool_call_recovery) detok, n, finish = Detokenizer(self.tok), 0, "length" run_tok, run_len, repeated = None, 0, False # #606: the current run of one repeated token diff --git a/serve/test_server.py b/serve/test_server.py index 1d56eccb..ccd8408c 100644 --- a/serve/test_server.py +++ b/serve/test_server.py @@ -3376,6 +3376,27 @@ class ThinkingBudget(unittest.TestCase): self.assertIn("answering: 10 of max 6400 tokens", lines[1]) self.assertIn("thinking: 10 of max 6400 tokens", lines[2]) + def test_a_budget_above_max_tokens_is_capped_to_leave_room_for_the_answer(self): + """#984: max_tokens 300 and a budget of 5000 never fired: the reply ended while still thinking. The thinking + is closed at max_tokens less the answer's reserve (max 512 or a quarter, at most half), with one log line.""" + out = io.StringIO() + with contextlib.redirect_stdout(out): + code, b = self.openai(max_tokens=300, reasoning_budget_tokens=5000) + self.assertEqual(code, 200, b) + msg = b["choices"][0]["message"] + self.assertTrue(msg["reasoning_content"].startswith(ThinkingEngine.THOUGHT[:150] + "\n"), msg) + self.assertEqual((msg["content"], b["choices"][0]["finish_reason"]), (ThinkingEngine.ANSWER, "stop")) + self.assertEqual(len(self.engine.prompts), 2) + self.assertIn("thinking capped at 150 of max_tokens 300 to leave room for the answer (budget 5000)", out.getvalue()) + + def test_a_budget_well_below_max_tokens_is_not_capped(self): + out = io.StringIO() + with contextlib.redirect_stdout(out): + code, b = self.openai(max_tokens=3000, reasoning_budget_tokens=20) + self.assertNotIn("thinking capped", out.getvalue()) + self.assertEqual(len(self.engine.prompts), 2) + self.assertTrue(b["choices"][0]["message"]["reasoning_content"].startswith(ThinkingEngine.THOUGHT[:20] + "\n")) + def test_a_shared_budget_reaches_a_request_that_sets_none(self): """The Chat settings shared with other apps may carry a thinking budget too, like max_tokens and the effort.""" self.svc.set_shared({"reasoning_budget_tokens": 20})