CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Python agent-frameworkagent-harnessagent-runtimeagentic-aiai-agentsai-frameworkanthropicharnessharness-engineeringharness-frameworklanggraphlanggraph-alternativellmmcpopenaipi-agentpi-agent-corepythonpython-agent
fix(anthropic): compute max_tokens from actual capability budget
xf gong0658b445199b
diff --git a/cubepi/providers/anthropic.py b/cubepi/providers/anthropic.pyindex 0e6fc91..7bdbdcc 100644--- a/cubepi/providers/anthropic.py+++ b/cubepi/providers/anthropic.py@@ -23,7 +23,6 @@ from cubepi.providers.base import (UserMessage,_fire_request_listeners,_fire_response_listeners,- adjust_max_tokens_for_thinking,invoke_on_payload,invoke_on_response,)@@ -157,21 +156,9 @@ class AnthropicProvider(BaseProvider):indices = self._cache_policy.message_breakpoint_indices(messages)self._apply_indices_markers(api_messages, indices, cache_control)- # Preserve legacy max_tokens adjustment: when thinking is on, we- # reserve thinking_budget on top of model.max_tokens (clamped to- # model.context_window). The capability writes thinking.budget_tokens;- # this helper computes max_tokens.- max_tokens, _ = adjust_max_tokens_for_thinking(- base_max_tokens=model.max_tokens,- model_max_tokens=model.context_window,- reasoning_level=thinking,- custom_budgets=opts.thinking_budgets,- )-kwargs: dict[str, Any] = {"model": model.id,"messages": api_messages,- "max_tokens": max_tokens,}if system_prompt:if cache_control and self._cache_policy.mark_system():@@ -199,15 +186,25 @@ class AnthropicProvider(BaseProvider):# stripped because Anthropic rejects custom temperature with# extended thinking enabled. See# https://platform.claude.com/docs/en/build-with-claude/extended-thinking#feature-compatibility+ #+ # max_tokens is computed AFTER the capability writes the budget so it+ # always accommodates whatever budget actually landed on the wire.+ # Anthropic rejects requests where budget_tokens >= max_tokens.if thinking == "off":merge_capability_payload(kwargs, cap.reasoning_off_payload)kwargs.setdefault("temperature", model.temperature)apply_temperature(kwargs, cap.temperature)+ kwargs["max_tokens"] = min(model.max_tokens, model.context_window)else:merge_capability_payload(kwargs, cap.reasoning_on_payload)if cap.reasoning_level is not None:write_reasoning_level(kwargs, cap.reasoning_level, thinking)kwargs.pop("temperature", None)+ budget = 0+ thinking_block = kwargs.get("thinking")+ if isinstance(thinking_block, dict):+ budget = thinking_block.get("budget_tokens", 0) or 0+ kwargs["max_tokens"] = min(model.max_tokens + budget, model.context_window)async def _produce() -> None:body: dict | None = Nonediff --git a/tests/providers/test_anthropic_capability.py b/tests/providers/test_anthropic_capability.pyindex 395e8b3..b9acfb7 100644--- a/tests/providers/test_anthropic_capability.py+++ b/tests/providers/test_anthropic_capability.py@@ -154,3 +154,24 @@ async def test_model_capability_overrides_take_precedence() -> None:)payload = await _capture_anthropic(p, StreamOptions(thinking="medium"))assert payload["thinking"]["budget_tokens"] == 4242+++async def test_custom_high_budget_capability_bumps_max_tokens():+ """Regression for max_tokens/budget_tokens desync: when custom level_budgets+ writes a budget larger than ThinkingBudgets defaults, max_tokens must be+ expanded to accommodate it (else Anthropic API rejects with+ budget_tokens >= max_tokens)."""+ custom = CapabilityDescriptor(+ reasoning_on_payload={"thinking": {"type": "enabled"}},+ reasoning_level=ReasoningLevelSpec(+ path="thinking.budget_tokens", kind="int_budget",+ level_budgets={"medium": 50000}, # WAY above ThinkingBudgets.medium=8192+ ),+ )+ p = AnthropicProvider(api_key="x", capability=custom)+ payload = await _capture_anthropic(p, StreamOptions(thinking="medium"))+ assert payload["thinking"]["budget_tokens"] == 50000+ # max_tokens must be at least budget + something (model.max_tokens=8192,+ # so min(8192+50000, 200000) = 58192). Anthropic rejects budget>=max_tokens.+ assert payload["max_tokens"] >= 50000 + 1