Skip to content

The agent that grows with you

fix(model_metadata): recognise the OpenAI-style completion split as an output-cap error

Dhruv Modi624ffd3b718d
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index 4fe598af96e..d4374d787e4 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -1279,6 +1279,26 @@ def get_context_length_from_provider_error(error_msg: str, current_context_lengt
return parsed_limit if parsed_limit is not None and parsed_limit < current_context_length else None
+# OpenAI's original overflow wording, copied by vLLM / llama-cpp-python: "(36865 in the messages,
+# 65536 in the completion)"; legacy completions: "(771 in your prompt; 4000 for the completion)".
+# The first figure is the prompt the server MEASURED, the second the requested max_tokens.
+_COMPLETION_SPLIT_RE = re.compile(
+ r'\((\d+)\s+(?:tokens\s+)?in (?:the messages|your prompt|the prompt)\s*[;,]\s*'
+ r'(\d+)\s+(?:tokens\s+)?(?:in|for) the completion\)'
+)
+
+
+def _completion_split_budget(error_lower: str) -> Optional[int]:
+ """window - measured prompt from the OpenAI-style parenthetical split, or None when the wording
+ is absent or the prompt alone fills the window (a genuine input overflow -> compress)."""
+ split = _COMPLETION_SPLIT_RE.search(error_lower)
+ ctx = re.search(r'maximum context length is (\d+)', error_lower)
+ if not split or not ctx:
+ return None
+ available = int(ctx.group(1)) - int(split.group(1))
+ return available if available >= 1 else None
+
+
def parse_available_output_tokens_from_error(error_msg: str) -> Optional[int]:
"""Available OUTPUT tokens from a "max_tokens too large" error, or None. Distinct from "prompt
too long" (-> compress): here input + requested_output > window, so the fix is a smaller
@@ -1310,6 +1330,9 @@ def parse_available_output_tokens_from_error(error_msg: str) -> Optional[int]:
_available = int(_m_ctx.group(1)) - int(_m_parts.group(1)) - int(_m_parts.group(2))
if _available >= 1:
return _available
+ _split_available = _completion_split_budget(error_lower)
+ if _split_available is not None:
+ return _split_available
# LM Studio / llama.cpp: window in tokens, prompt in CHARACTERS; ~3 chars/token over-reserves the input.
_m_ctx_tok = re.search(r'maximum context length is (\d+)\s*token', error_lower)
_m_chars = re.search(r'prompt contains (\d+)\s*character', error_lower)
@@ -1356,6 +1379,7 @@ _PARSEABLE_OUTPUT_CAP_SIGNALS = (
("max_tokens", "available_tokens"), ("max_tokens", "available tokens"),
("in the output", "maximum context length"),
("maximum context length", "requested", "output tokens"),
+ ("maximum context length", "in the completion"), ("maximum context length", "for the completion"),
("range of max_tokens should be",), ("exceeds model", "maximum output tokens"),
("output limit",), ("max_tokens", "maximum allowed number of output tokens"),
)
@@ -1370,6 +1394,10 @@ def is_output_cap_error(error_msg: str) -> bool:
output-cap 400 misclassified as context overflow death-loops the compressor (same max_tokens, same
rejection). Signal: talks about max_tokens as a cap/range/limit and NOT about an oversized input."""
error_lower = error_msg.lower()
+ # The OpenAI-style split names neither max_tokens nor "output tokens" and ends with "reduce the
+ # length", so it fails both gates below; the measured prompt decides instead (#90607).
+ if _completion_split_budget(error_lower) is not None:
+ return True
# An error that ALSO describes an oversized INPUT is a genuine overflow — compression can fix it.
return (
any(p in error_lower for p in ("max_tokens", "max_output_tokens", "max_completion_tokens"))
diff --git a/tests/agent/test_output_cap_parsing.py b/tests/agent/test_output_cap_parsing.py
index 745e86a0caf..3d10c23b3e2 100644
--- a/tests/agent/test_output_cap_parsing.py
+++ b/tests/agent/test_output_cap_parsing.py
@@ -208,3 +208,26 @@ class TestParseVllmTokenBasedOutputCap:
cap = available
assert real_input + cap <= window, f"did not converge: cap={cap}"
+
+
+class TestParseOpenAiCompletionSplit:
+ """OpenAI's original overflow wording, copied by vLLM / llama-cpp-python, splits the request
+ as "(A in the messages, B in the completion)" and never names max_tokens (#90607)."""
+
+ @pytest.mark.parametrize("msg, budget", [
+ ("This model's maximum context length is 102400 tokens. However, you requested 102401 tokens "
+ "(36865 in the messages, 65536 in the completion). Please reduce the length of the messages or completion.",
+ 102400 - 36865),
+ ("This model's maximum context length is 4097 tokens, however you requested 4771 tokens "
+ "(771 in your prompt; 4000 for the completion). Please reduce your prompt; or completion length.",
+ 4097 - 771),
+ ])
+ def test_split_is_output_cap_with_window_minus_measured_prompt(self, msg, budget):
+ assert parse_available_output_tokens_from_error(msg) == budget
+ assert is_output_cap_error(msg)
+
+ def test_split_with_prompt_filling_window_stays_on_compression(self):
+ msg = ("This model's maximum context length is 4097 tokens. However, you requested 6000 tokens "
+ "(5000 in the messages, 1000 in the completion). Please reduce the length of the messages or completion.")
+ assert parse_available_output_tokens_from_error(msg) is None
+ assert not is_output_cap_error(msg)