Skip to content

The agent that grows with you

Port from cline/cline#12876: classify Anthropic output-cap errors

Teknium225d53f953c2
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index c9a7824a81b..97a00499a72 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -1162,9 +1162,12 @@ def parse_available_output_tokens_from_error(error_msg: str) -> Optional[int]:
if not _any_phrase_group(error_lower, _PARSEABLE_OUTPUT_CAP_SIGNALS):
return None
# Direct cap figures, most specific first: "exceeds model's maximum output tokens (65536)", "Range of
- # max_tokens should be [1, 65536]" (upper bound is the cap), Anthropic "= available_tokens: 10000", last "= N".
+ # max_tokens should be [1, 65536]" (upper bound is the cap), Anthropic "max_tokens: 100000 > 64000, which
+ # is the maximum allowed number of output tokens" (the ceiling is the right-hand side), Anthropic
+ # "= available_tokens: 10000", last "= N".
for pattern in (
r'exceeds model(?:\'s)? maximum output tokens\s*\(?\s*(\d+)\s*\)?',
+ r'max_tokens\s*:\s*\d+\s*>\s*(\d+)\s*,?\s*which is the maximum allowed number of output tokens',
r'range of max_tokens should be\s*\[\s*\d+\s*,\s*(\d+)\s*\]',
r'available_tokens[:\s]+(\d+)',
r'available\s+tokens[:\s]+(\d+)',
@@ -1208,12 +1211,13 @@ def parse_available_output_tokens_from_error(error_msg: str) -> Optional[int]:
# Each entry is a phrase group; the group matches when ALL phrases are present.
-# DashScope, Anthropic, OpenRouter/Nous, LM Studio/llama.cpp, generic "should be <= N", OpenAI-compat relays.
+# DashScope, Anthropic (available_tokens / "maximum allowed number of output tokens"), OpenRouter/Nous,
+# LM Studio/llama.cpp, generic "should be <= N", OpenAI-compat relays.
_OUTPUT_CAP_SIGNALS = (
("range of max_tokens should be",), ("available_tokens",), ("available tokens",),
("in the output", "maximum context length"), ("requested", "output tokens"),
("should be",), ("less than or equal",), ("must be",), ("exceeds model", "maximum output tokens"),
- ("output limit",),
+ ("output limit",), ("maximum allowed number of output tokens",),
)
_INPUT_OVERFLOW_SIGNALS = (
"prompt is too long", "prompt too long", "input is too long", "input token",
@@ -1228,7 +1232,7 @@ _PARSEABLE_OUTPUT_CAP_SIGNALS = (
("in the output", "maximum context length"),
("maximum context length", "requested", "output tokens"),
("range of max_tokens should be",), ("exceeds model", "maximum output tokens"),
- ("output limit",),
+ ("output limit",), ("max_tokens", "maximum allowed number of output tokens"),
)
diff --git a/tests/agent/test_output_cap_parsing.py b/tests/agent/test_output_cap_parsing.py
index aa64d09a7e0..745e86a0caf 100644
--- a/tests/agent/test_output_cap_parsing.py
+++ b/tests/agent/test_output_cap_parsing.py
@@ -54,6 +54,13 @@ class TestParseDashScopeOutputCap:
"""DashScope / Alibaba Cloud (Qwen) reject an over-cap output request with
a bounded range whose upper bound is the real max-output cap (#55546)."""
+ def test_anthropic_output_ceiling_format(self):
+ msg = (
+ "max_tokens: 100000 > 64000, which is the maximum allowed number "
+ "of output tokens for claude-sonnet-4-5"
+ )
+ assert parse_available_output_tokens_from_error(msg) == 64000
+
def test_dashscope_range_format(self):
msg = ("HTTP 400: InternalError.Algo.InvalidParameter: "
"Range of max_tokens should be [1, 65536]")
@@ -101,6 +108,16 @@ class TestIsOutputCapError:
"input_tokens: 190000 = available_tokens: 10000"
) is True
+ def test_anthropic_max_tokens_output_ceiling_is_output_cap(self):
+ # Anthropic invalid_request_error uses the same error type for bad
+ # schemas, images, and output-cap violations. This message is about the
+ # requested response budget, not input/context overflow, so it must stay
+ # out of the compression path. Port of cline/cline#12876.
+ assert is_output_cap_error(
+ "max_tokens: 100000 > 64000, which is the maximum allowed number "
+ "of output tokens for claude-sonnet-4-5"
+ ) is True
+
def test_real_input_overflow_is_not_output_cap(self):
# Mentions max_tokens but the INPUT is the problem -> compression path.
assert is_output_cap_error(