Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(agent-core): treat Anthropic's input-plus-max_tokens refusal as overflow and its window stop as length (#2495)
Call White99ece76a6699
diff --git a/core/agent_core/ai/anthropic.py b/core/agent_core/ai/anthropic.pyindex 99b8dfc32..b2c1bb394 100644--- a/core/agent_core/ai/anthropic.py+++ b/core/agent_core/ai/anthropic.py@@ -1053,6 +1053,9 @@ def _normalize_stop_reason(value: Any) -> str:"stop_sequence": "stop","pause_turn": "stop","max_tokens": "length",+ # Generation reached the context window (Claude 4.5 and newer accept input plus max_tokens over the window);+ # Anthropic says to treat the response as truncated, as at max_tokens.+ "model_context_window_exceeded": "length","tool_use": "tool_use","refusal": "refusal","safety": "safety",diff --git a/core/agent_core/ai/errors.py b/core/agent_core/ai/errors.pyindex e34417d36..8f1c9de8d 100644--- a/core/agent_core/ai/errors.py+++ b/core/agent_core/ai/errors.py@@ -1,8 +1,9 @@"""Provider error classification.Overflow expressions are ported from Pi's ``packages/ai/src/utils/overflow.ts``-(MIT, revision 7fbbd5f). The adapter layer adds HTTP and transport semantics-from C-2.+(MIT, revision 7fbbd5f), plus one Pi lacks: Anthropic's refusal of input plus+``max_tokens`` over the window. The adapter layer adds HTTP and transport+semantics from C-2."""from __future__ import annotations@@ -55,6 +56,10 @@ _OVERFLOW_PATTERNS = (r"too many tokens",r"token limit exceeded",r"4(?:00|13)\s*(?:status code)?\s*\(no body\)",+ # Not in Pi. Anthropic models before Claude 4.5 refuse a request whose input plus max_tokens exceeds the window+ # with a 400 invalid_request_error: "input length and `max_tokens` exceed context limit: 188240 + 21333 > 200000,+ # decrease input length or `max_tokens` and try again". Later models accept it (conversion-tables.md).+ r"input length and `?max_tokens`? exceed context limit",)_OVERFLOW_RE = tuple(re.compile(pattern, re.IGNORECASE) for pattern in _OVERFLOW_PATTERNS)_NON_OVERFLOW_RE = (diff --git a/docs/plans/agent-core-contracts/conversion-tables.md b/docs/plans/agent-core-contracts/conversion-tables.mdindex 715bdcfb6..9c121e43f 100644--- a/docs/plans/agent-core-contracts/conversion-tables.md+++ b/docs/plans/agent-core-contracts/conversion-tables.md@@ -134,10 +134,10 @@ or extracted message/code pair that its wire format makes available.| Protocol | Auth | Rate limit | Overload/server | Overflow/context || --- | --- | --- | --- | --- |-| Anthropic | `authentication_error`, `invalid_api_key`, `permission_error`, `permission` | `rate_limit_error` | `overloaded_error` → `overloaded`; `api_error` → `server` | `invalid_request_error` with context text; `context_length_exceeded` → `overflow` |+| Anthropic | `authentication_error`, `invalid_api_key`, `permission_error`, `permission` | `rate_limit_error` | `overloaded_error` → `overloaded`; `api_error` → `server` | `invalid_request_error` with context text (`ai/errors.is_overflow_message`), which includes the refusal of input plus `max_tokens` over the window by models before Claude 4.5 (``input length and `max_tokens` exceed context limit``); `context_length_exceeded` → `overflow` || OpenAI Chat | `authentication_error`, `invalid_api_key`, `permission` | `rate_limit_error`, `too_many_requests` | `server_error`, `api_error` | `context_length_exceeded`, `request_too_large` || OpenAI Responses | `unauthenticated`, `permission_denied`, `invalid_api_key` | `rate_limit`, `rate_limit_error` | `overloaded`, `server_error`, `service_unavailable` | `context_length_exceeded`, `request_too_large` |-| Model Hub relays, any protocol | | | `upstream_error`, `service_unavailable_error`; `request_timeout` and `timeout_error` (CLIProxyAPI, Model Hub's engine: `request_timeout` for an upstream `408` or an upstream stream that closed before its terminal, nested under `error` with type `server_error`; `timeout_error`, its Claude type for an upstream `408` or `504`) → `server` | |+| Model Hub relays, any protocol | | | `upstream_error`, `service_unavailable_error`; `request_timeout` and `timeout_error` (CLIProxyAPI, Model Hub's engine: `request_timeout` for an upstream `408` or an upstream stream that closed before its terminal, nested under `error` with type `server_error`; `timeout_error`, its Claude type for an upstream `408` or `504`) → `server` | `upstream_request_invalid` (the gateway's code for a request its upstream refused; its copy quotes the upstream's message) → `overflow` when the quoted message is context text || Model Hub's own frame, OpenAI Responses and Chat | | | `modelHub.errors.stream_interrupted` (the gateway's code for an upstream stream that ended after output without a terminal) → `interrupted` | |HTTP `401/403` always map to `auth`, `413` to `overflow`, `429` to`rate_limit` with `Retry-After`, and `5xx` to `server` or `overloaded`. A server code follows the same rule as a@@ -539,10 +539,17 @@ Anthropic stop reasons:| Wire value | Canonical || --- | --- || `end_turn`, `stop_sequence`, `pause_turn` | `stop` |-| `max_tokens` | `length` |+| `max_tokens`, `model_context_window_exceeded` | `length` || `tool_use` | `tool_use` || `refusal` | `refusal` || `safety` | `safety` |+| any other value | `error` |++`model_context_window_exceeded` ends a response whose request asked for more output than the window had room for:+Claude 4.5 and newer models accept such a request and stop when generation reaches the window, and Anthropic says to+treat the response as truncated. Earlier models refuse the request instead, unless it carries the+`model-context-window-exceeded-2025-08-26` beta header, which this adapter does not send; the refusal is an+`overflow` (Provider error classification).## OpenAI Chat Completionsdiff --git a/docs/plans/agent-core-contracts/provider.md b/docs/plans/agent-core-contracts/provider.mdindex f1d55dffb..e6b8ba2dd 100644--- a/docs/plans/agent-core-contracts/provider.md+++ b/docs/plans/agent-core-contracts/provider.md@@ -75,7 +75,7 @@ Error classification is part of the contract because the loop branches on it:| `kind` | Meaning | Loop action || --- | --- | --- |-| `overflow` | the request exceeded the model's context (Pi's overflow patterns, HTTP 413, `context_length_exceeded`) | overflow recovery (plan §5.2) |+| `overflow` | the request exceeded the model's context (Pi's overflow patterns and Anthropic's refusal of input plus `max_tokens` over the window, HTTP 413, `context_length_exceeded`) | overflow recovery (plan §5.2) || `rate_limit`, `overloaded`, `network`, `stalled`, `server`, `interrupted` | transient (`network`: the provider could not be reached; `stalled`: a connected provider sent nothing for the silence bound; `interrupted`: the provider's stream ended after output without a terminal, as Model Hub reports with its own `modelHub.errors.stream_interrupted` frame) | retry with backoff, honoring `retry_after_s`, only when nothing was streamed (no deltas, no `partial`), at most 3 retries within 120 s; then the run ends with that error. After streamed text or a tool call the error is terminal for the request, matching Model Hub's first-byte rule; after thinking alone it is retried once (below) || `auth`, `invalid_request` | not retryable | end the run with the error || `aborted` | cancelled by the caller | end the run as aborted |diff --git a/tests/agent_core/agent/test_compaction.py b/tests/agent_core/agent/test_compaction.pyindex b45a8bbfe..7244e107f 100644--- a/tests/agent_core/agent/test_compaction.py+++ b/tests/agent_core/agent/test_compaction.py@@ -35,6 +35,7 @@ from core.agent_core.agent.loop import Agentfrom core.agent_core.agent.models import RetryPolicyfrom core.agent_core.ai._common import endpoint_origin, prepare_messagesfrom core.agent_core.ai.anthropic import build_messages_payload+from core.agent_core.ai.errors import classify_errorfrom core.agent_core.ai.openai_chat import build_chat_payloadfrom core.agent_core.ai.openai_responses import build_responses_payloadfrom core.agent_core.ai.provider import Done, ModelEndpoint, ProviderError, TextDelta@@ -72,8 +73,11 @@ from tests.agent_core.fakes import (FakeModelRouter,FakeTool,InMemoryTranscriptStore,+ WINDOW_REFUSAL,+ anthropic_error,assistant,input_row,+ model_hub_error,)DENIED, BUDGET_USED = DREAMING.denied, DREAMING.budget_used@@ -643,6 +647,32 @@ async def test_a_checkpoint_request_that_cannot_fit_a_new_route_is_audited_and_tassert events[-1].reason == "completed"+#: A request the provider refuses because its input plus ``max_tokens`` exceeds the window (an Anthropic model before+#: Claude 4.5), at the endpoint and through Model Hub, classified as the Anthropic adapter classifies an HTTP error.+WINDOW_REFUSALS = [+ pytest.param(classify_error(status=400, body=json.dumps(envelope(WINDOW_REFUSAL)), protocol="anthropic"), id=name)+ for name, envelope in (("window-endpoint", anthropic_error), ("window-model-hub", model_hub_error))+]++[email protected]("refusal", WINDOW_REFUSALS)+async def test_a_threshold_checkpoint_the_provider_refuses_for_its_window_rolls(refusal):+ # The threshold checkpoint's request can fit by the estimate, but the provider refuses it for its window: an+ # overflow, so the ladder goes on at (b) and rolls (section 3, section 8 a).+ store = InMemoryTranscriptStore()+ await run(make_agent(Model(history()), store=store, tools=[reader(tokens(3_000))], selection=ROOMY))+ model = Model([[Done(assistant("after"))]], [[refusal], [Done(assistant(CHECKPOINT))]])+ agent = make_agent(model, store=store, tools=[reader(tokens(3_000))], selection=ROOMY)+ events = await cross(agent)+ finished = [(event.reason, event.mode) for event in events if isinstance(event, CompactionFinished)]+ assert finished == [("overflow", "rolling")]+ failed = [event for event in events if isinstance(event, CompactionFailed)]+ assert [event.reason for event in failed] == ["threshold"] and failed[0].error.startswith("overflow: ")+ normal, rolling = model.checkpoint_requests+ assert len(rolling.messages) < len(normal.messages) # the rolling fork carries a prefix of the context+ assert events[-1].reason == "completed"++#: Checkpoint turns that fail without a repair: a length stop, and a provider error.FAILURES = [[Done(assistant(CHECKPOINT, stop_reason="length"))],@@ -854,9 +884,10 @@ async def test_two_ineffective_checkpoints_stop_compaction_for_the_run():OVERFLOW = ProviderError("overflow", "prompt is too long: 40000 tokens > 32000 maximum", False, status=400)-async def test_a_provider_overflow_below_T_takes_a_normal_checkpoint_and_retries():[email protected]("refusal", [pytest.param(OVERFLOW, id="prompt-too-long"), *WINDOW_REFUSALS])+async def test_a_provider_overflow_below_T_takes_a_normal_checkpoint_and_retries(refusal):model = Model(- [call("read", "r0", path="f"), call("read", "r1", path="f"), [OVERFLOW], [Done(assistant("done"))]],+ [call("read", "r0", path="f"), call("read", "r1", path="f"), [refusal], [Done(assistant("done"))]],[[Done(assistant(CHECKPOINT))]],)agent = make_agent(model, tools=[reader(tokens(4_000))])diff --git a/tests/agent_core/ai/test_provider_adapters.py b/tests/agent_core/ai/test_provider_adapters.pyindex b2516a461..9ed410f11 100644--- a/tests/agent_core/ai/test_provider_adapters.py+++ b/tests/agent_core/ai/test_provider_adapters.py@@ -44,6 +44,7 @@ from core.agent_core.messages import (Usage,)from core.agent_core.tools.base import ToolSpec+from tests.agent_core.fakes import WINDOW_REFUSAL, anthropic_error, model_hub_errordef _request(@@ -648,6 +649,19 @@ async def test_adapters_join_protocol_path_before_existing_query('data: {"type":"message_stop"}\n\n'),),+ (+ # Generation reached the context window first: Anthropic says to treat the response as truncated.+ AnthropicAdapter,+ "anthropic",+ (+ 'data: {"type":"content_block_start","index":0,'+ '"content_block":{"type":"tool_use","id":"toolu_1","name":"read"}}\n\n'+ 'data: {"type":"content_block_delta","index":0,'+ '"delta":{"type":"input_json_delta","partial_json":"{\\"path\\":\\"x"}}\n\n'+ 'data: {"type":"message_delta","delta":{"stop_reason":"model_context_window_exceeded"}}\n\n'+ 'data: {"type":"message_stop"}\n\n'+ ),+ ),(OpenAIResponsesAdapter,"openai_responses",@@ -3087,6 +3101,36 @@ async def test_http_413_is_classified_as_overflow() -> None:assert error.retryable is False+#: Anthropic's 400 for a ``max_tokens`` above the model's output limit: it names ``max_tokens`` too, but no smaller+#: context can fix it.+OUTPUT_LIMIT_REFUSAL = (+ "max_tokens: 200000 > 128000, which is the maximum allowed number of output tokens for claude-opus-4-1-20250805"+)++[email protected]("envelope", [anthropic_error, model_hub_error], ids=["endpoint", "model-hub"])+ ("message", "kind"),+ [(WINDOW_REFUSAL, "overflow"), (OUTPUT_LIMIT_REFUSAL, "invalid_request")],+ ids=["window", "output-limit"],+)+async def test_anthropic_window_refusal_is_overflow_and_output_limit_refusal_is_not(+ envelope: Any, message: str, kind: str,+) -> None:+ # Models before Claude 4.5 refuse a request whose input plus max_tokens exceeds the window; later ones stop at the+ # window instead. Through Model Hub the upstream's message reaches the adapter inside the gateway's copy and code.+ def handler(_: httpx.Request) -> httpx.Response:+ return httpx.Response(400, json=envelope(message))++ async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:+ events = await _events(AnthropicAdapter(client), _request("anthropic"))++ (error,) = events+ assert isinstance(error, ProviderError)+ assert (error.kind, error.status, error.retryable, error.partial) == (kind, 400, False, None)++@pytest.mark.asyncioasync def test_http_redirect_is_terminal_invalid_request() -> None:def handler(_: httpx.Request) -> httpx.Response:diff --git a/tests/agent_core/fakes.py b/tests/agent_core/fakes.pyindex 2525f5903..211896178 100644--- a/tests/agent_core/fakes.py+++ b/tests/agent_core/fakes.py@@ -29,6 +29,26 @@ SELECTION = ModelSelection()Script = Sequence[ProviderEvent] | Callable[[ModelRequest, CancelToken], AsyncIterator[ProviderEvent]]+#: How an Anthropic model before Claude 4.5 refuses a request whose input plus ``max_tokens`` exceeds its window.+WINDOW_REFUSAL = (+ "input length and `max_tokens` exceed context limit: 188240 + 21333 > 200000, "+ "decrease input length or `max_tokens` and try again"+)+++def anthropic_error(message: str) -> dict:+ """An Anthropic endpoint's 400 body for a request it refuses."""+ return {"type": "error", "error": {"type": "invalid_request_error", "message": message}}+++def model_hub_error(message: str) -> dict:+ """Model Hub's gateway body for a request its upstream refused (Vibey's route): the gateway's own code, and the+ upstream's message inside the copy the gateway's renderer writes."""+ from core.handlers.model_hub.provenance import produce_turn_outcome, render_turn_outcome_copy++ copy = render_turn_outcome_copy(produce_turn_outcome("turn.request_nonfallback", upstream_detail=message), "en")+ return {"error": {"type": "upstream_request_invalid", "code": "upstream_request_invalid", "message": copy}}+def user(value: str) -> UserMessage:return UserMessage((text(value),))