Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

fix(anthropic): honor opts.thinking_budgets per-request override

xf gonga164049bd1ed
diff --git a/cubepi/providers/anthropic.py b/cubepi/providers/anthropic.py
index c714671..7842c5b 100644
--- a/cubepi/providers/anthropic.py
+++ b/cubepi/providers/anthropic.py
@@ -200,6 +200,20 @@ class AnthropicProvider(BaseProvider):
if cap.reasoning_level is not None:
write_reasoning_level(kwargs, cap.reasoning_level, thinking)
kwargs.pop("temperature", None)
+
+ # Per-request budget override via StreamOptions.thinking_budgets
+ # takes precedence over the capability's level_budgets. Mirrors
+ # the legacy adjust_max_tokens_for_thinking(custom_budgets=...)
+ # parameter. ThinkingBudgets has no "xhigh" field, so xhigh maps
+ # to "high" (matches legacy clamp behavior).
+ if opts.thinking_budgets is not None and isinstance(
+ kwargs.get("thinking"), dict
+ ):
+ level_for_lookup = "high" if thinking == "xhigh" else thinking
+ custom_budget = getattr(opts.thinking_budgets, level_for_lookup, None)
+ if custom_budget is not None:
+ kwargs["thinking"]["budget_tokens"] = custom_budget
+
budget = 0
thinking_block = kwargs.get("thinking")
if isinstance(thinking_block, dict):
diff --git a/tests/providers/test_anthropic_capability.py b/tests/providers/test_anthropic_capability.py
index e57c198..ed3792b 100644
--- a/tests/providers/test_anthropic_capability.py
+++ b/tests/providers/test_anthropic_capability.py
@@ -255,3 +255,20 @@ async def test_capability_clamps_budget_when_context_too_small() -> None:
thinking_block = captured.get("thinking", {})
if thinking_block.get("type") == "enabled":
assert thinking_block["budget_tokens"] < captured["max_tokens"]
+
+
+async def test_capability_honors_opts_thinking_budgets_override():
+ """StreamOptions.thinking_budgets overrides capability's level_budgets per request."""
+ from cubepi.providers.base import ThinkingBudgets
+
+ # Default capability — level_budgets has medium=8192
+ p = AnthropicProvider(api_key="x")
+ custom_budgets = ThinkingBudgets(medium=12288)
+ payload = await _capture_anthropic(
+ p, StreamOptions(thinking="medium", thinking_budgets=custom_budgets)
+ )
+ assert payload["thinking"]["type"] == "enabled"
+ assert payload["thinking"]["budget_tokens"] == 12288
+ # max_tokens must accommodate the new budget.
+ assert payload["max_tokens"] >= 12288 + 512 # min_output_tokens floor