Skip to content

armgit / avibe

Public Imported from GitHub
0

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.

fix(model-hub): register a budget-only Claude model with a budget range, so the engine keeps its budget thinking (#2739)

Call Whitef4dbd3435343
diff --git a/core/agent_core/ai/anthropic.py b/core/agent_core/ai/anthropic.py
index 04bee3fb7..fba7f9860 100644
--- a/core/agent_core/ai/anthropic.py
+++ b/core/agent_core/ai/anthropic.py
@@ -17,6 +17,7 @@ from typing import Any
import httpx
+from core.agent_core.ai.claude_thinking import uses_adaptive_thinking
from core.agent_core.ai.hosted import anthropic_hosted_block, citation, reported_counts
from core.agent_core.ai._common import (
ServedHopResolver,
@@ -1008,7 +1009,7 @@ def build_messages_payload(
if tools:
payload["tools"] = tools
effort = (request.reasoning_effort or "").lower()
- adaptive = _uses_adaptive_thinking(request.endpoint.model_id)
+ adaptive = uses_adaptive_thinking(request.endpoint.model_id)
if effort in {"none", "off", "disabled"} or (
effort and not adaptive and _continues_unthought_tool_loop(payload_messages)
):
@@ -1290,29 +1291,11 @@ def sent_effort(model_id: str, effort: str) -> str | None:
"""
value = effort.lower()
- if not value or value in {"none", "off", "disabled"} or not _uses_adaptive_thinking(model_id):
+ if not value or value in {"none", "off", "disabled"} or not uses_adaptive_thinking(model_id):
return None
return "low" if value == "minimal" else value
-def _uses_adaptive_thinking(model_id: str) -> bool:
- normalized = model_id.lower().replace(".", "-")
- return any(
- family in normalized
- for family in (
- "claude-opus-4-6",
- "claude-opus-4-7",
- "claude-opus-4-8",
- "claude-sonnet-4-6",
- "claude-sonnet-5",
- "claude-opus-5",
- "claude-fable-5",
- "claude-mythos-5",
- "claude-haiku-5",
- )
- )
-
-
def _string(value: Any) -> str:
return value if isinstance(value, str) else ""
diff --git a/core/agent_core/ai/claude_thinking.py b/core/agent_core/ai/claude_thinking.py
new file mode 100644
index 000000000..0e8fdc679
--- /dev/null
+++ b/core/agent_core/ai/claude_thinking.py
@@ -0,0 +1,35 @@
+"""Which Claude models take adaptive thinking, and which take a token budget.
+
+Adaptive thinking (``thinking: {type: "adaptive"}`` with ``output_config.effort``)
+starts with Claude 4.6; Claude 4.5 and older take only a token budget
+(``thinking: {type: "enabled", budget_tokens}``) and refuse ``adaptive``. Vibey's
+Anthropic provider and the Model Hub engine registration both decide by this one
+rule, so a model is never sent one kind by the agent and rewritten to the other on
+the way (#2737).
+
+The budget-only models are a closed set, every Claude release since 4.6 takes
+adaptive thinking, so they are named positively by version. A Claude ID that names
+no version (an alias such as ``claude-latest``, or a family released later) is
+taken as adaptive.
+"""
+
+from __future__ import annotations
+
+import re
+from typing import Final
+
+# A Claude version in a model ID: ``claude-sonnet-4-5-20250929``, ``claude-3-5-haiku``, ``claude-opus-4``.
+_CLAUDE_VERSION: Final = re.compile(r"claude-(?:[a-z]+-)?(\d+)(?:-(\d{1,2}))?(?![0-9])")
+_FIRST_ADAPTIVE_VERSION: Final = (4, 6)
+
+
+def uses_adaptive_thinking(model_id: str) -> bool:
+ """Whether this Claude model takes adaptive thinking; False for a Claude version before 4.6 and for non-Claude."""
+
+ normalized = model_id.lower().replace(".", "-")
+ if "claude" not in normalized:
+ return False
+ version = _CLAUDE_VERSION.search(normalized)
+ if version is None:
+ return True
+ return (int(version.group(1)), int(version.group(2) or 0)) >= _FIRST_ADAPTIVE_VERSION
diff --git a/tests/agent_core/ai/test_claude_thinking.py b/tests/agent_core/ai/test_claude_thinking.py
new file mode 100644
index 000000000..800592182
--- /dev/null
+++ b/tests/agent_core/ai/test_claude_thinking.py
@@ -0,0 +1,32 @@
+"""The one rule for which Claude models take adaptive thinking and which take a token budget (#2737)."""
+
+from __future__ import annotations
+
+import pytest
+
+from core.agent_core.ai.claude_thinking import uses_adaptive_thinking
+
+
[email protected](("model_id", "adaptive"), [
+ # Claude 4.5 and older take a token budget, whatever the ID's date, separator or relay prefix.
+ ("claude-sonnet-4-5-20250929", False),
+ ("claude-haiku-4-5", False),
+ ("claude-opus-4.5", False),
+ ("claude-opus-4-1-20250805", False),
+ ("claude-opus-4-20250514", False),
+ ("claude-3-5-haiku-20241022", False),
+ ("claude-3-opus-20240229", False),
+ ("anthropic/claude-sonnet-4-5", False),
+ # Claude 4.6 and later take adaptive thinking.
+ ("claude-sonnet-4-6", True),
+ ("claude-opus-4-7", True),
+ ("claude-opus-5-5", True),
+ ("claude-fable-5-1", True),
+ ("claude-opus-4-6-thinking", True),
+ # A Claude ID that names no version is a later release or an alias: adaptive, not guessed legacy.
+ ("claude-latest", True),
+ # Not a Claude model.
+ ("kimi-k2", False),
+])
+def test_only_a_claude_version_before_4_6_takes_a_token_budget(model_id, adaptive):
+ assert uses_adaptive_thinking(model_id) is adaptive
diff --git a/tests/e2e/test_model_hub_engine_wire.py b/tests/e2e/test_model_hub_engine_wire.py
index 058be21b9..50f3153af 100644
--- a/tests/e2e/test_model_hub_engine_wire.py
+++ b/tests/e2e/test_model_hub_engine_wire.py
@@ -172,8 +172,8 @@ class _Harness:
@asynccontextmanager
-async def _through_engine(tmp_path, monkeypatch, protocol: str, *handlers):
- """A route of one Source per handler, in order, all speaking ``protocol``."""
+async def _through_engine(tmp_path, monkeypatch, protocol: str, *handlers, model_id: str = MODEL, efforts=()):
+ """A route of one Source per handler, in order, all speaking ``protocol`` and serving ``model_id``."""
requests: list[list[dict]] = [[] for _ in handlers]
def recording(index, handler):
@@ -193,7 +193,8 @@ async def _through_engine(tmp_path, monkeypatch, protocol: str, *handlers):
for index, handler in enumerate(handlers):
root = await stack.enter_async_context(_server(recording(index, handler)))
source = _source(f"src_wire{index:04d}", f"Wire fixture {index}", vendor="custom",
- protocol=protocol, model_id=MODEL)
+ protocol=protocol, model_id=model_id)
+ source.models[0].reasoning_efforts = list(efforts)
source.base_url = root + ("/v1" if protocol == "openai_responses" else "")
source.credential_ref = adapter.state_store.store_api_key(
f"sk-synthetic-wire-{index}", vendor="custom", protocol=protocol, base_url=source.base_url,
@@ -561,3 +562,32 @@ async def test_engine_stop_ends_open_streams_without_a_forced_kill(tmp_path, mon
_record(tmp_path, {"signals": signals, "stop_seconds": stop_seconds, "stream_end": stream_end})
assert signals == [int(signal.SIGTERM)], signals
assert stream_end != "still_open"
+
+
[email protected](("model_id", "thinking"), [
+ # Claude 4.5 takes a token budget, and refuses adaptive thinking.
+ ("claude-sonnet-4-5", {"type": "enabled", "budget_tokens": 4000}),
+ # Claude 4.6 and later take adaptive thinking; the engine maps a budget to it.
+ ("claude-sonnet-4-6", {"type": "adaptive"}),
+])
+async def test_budget_thinking_reaches_a_claude_model_as_the_kind_it_takes(tmp_path, monkeypatch, model_id, thinking):
+ """#2737: the pinned engine rewrote budget thinking for a Claude 4.5 model into adaptive thinking."""
+
+ async def answer(_request, _body):
+ return web.json_response({
+ "id": "msg_wire", "type": "message", "role": "assistant", "model": model_id,
+ "content": [{"type": "text", "text": "."}], "stop_reason": "end_turn",
+ "usage": {"input_tokens": 3, "output_tokens": 1},
+ })
+
+ async with _through_engine(
+ tmp_path, monkeypatch, "anthropic", answer, model_id=model_id, efforts=("low", "medium", "high", "max"),
+ ) as harness:
+ url, body = _frontend_request("anthropic", harness.hop, stream=False, extra={
+ "max_tokens": 8000, "thinking": {"type": "enabled", "budget_tokens": 4000},
+ })
+ observed = await _post(url, harness.hop, body)
+ assert observed["status"] == 200, observed
+ (sent,) = harness.requests[0]
+ assert sent["body"]["thinking"] == thinking
+ assert ("output_config" in sent["body"]) is (thinking["type"] == "adaptive")
diff --git a/tests/test_model_hub_runtime.py b/tests/test_model_hub_runtime.py
index 46e2aed24..11df32e3d 100644
--- a/tests/test_model_hub_runtime.py
+++ b/tests/test_model_hub_runtime.py
@@ -1611,6 +1611,41 @@ def test_engine_version_check_uses_minimal_environment(
assert captured_env == engine_subprocess_environment()
+def test_a_claude_model_is_registered_with_the_thinking_it_takes_upstream(tmp_path: Path) -> None:
+ """A budget-only Claude model registers a budget range: CPA reads ``levels`` as adaptive thinking on its Claude
+ path and rewrites budget thinking to ``adaptive``, which Claude 4.5 and older refuse (#2737)."""
+
+ store = EngineStateStore(tmp_path / "state")
+ instance_dir, runtime_secrets = store.prepare_instance("install-1")
+ ref = store.store_api_key(
+ "relay-secret", vendor="custom", protocol="anthropic", base_url="https://relay.example.test",
+ )
+ efforts = ("low", "medium", "high", "max")
+ models = ("claude-sonnet-4-5-20250929", "claude-sonnet-4-6", "claude-opus-4.5", "claude-latest", "kimi-k2")
+ store.sync_sources([_binding(
+ ref, source_id="src_relayclaude", vendor="custom", protocol="anthropic",
+ base_url="https://relay.example.test", model_ids=models,
+ model_reasoning_efforts=tuple((model, efforts) for model in models),
+ )])
+ config_path = instance_dir / "config.yaml"
+ write_engine_config(
+ config_path, host="127.0.0.1", port=18231, auth_dir=store.auth_dir, runtime_secrets=runtime_secrets,
+ sources=store.list_sources(), state_store=store,
+ )
+ (entry,) = yaml.safe_load(config_path.read_text())["claude-api-key"]
+ thinking = {model["name"]: model["thinking"] for model in entry["models"]}
+ budget = {"min": 1024, "max": 128000, "zero-allowed": True}
+ assert thinking == {
+ "claude-sonnet-4-5-20250929": budget,
+ "claude-sonnet-4-6": {"levels": ["max", "high", "medium", "low"]},
+ "claude-opus-4.5": budget,
+ # An alias or a later family names no budget-only version, so it keeps its levels.
+ "claude-latest": {"levels": ["max", "high", "medium", "low"]},
+ # Not a Claude model: the Anthropic protocol alone says nothing about its thinking.
+ "kimi-k2": {"levels": ["max", "high", "medium", "low"]},
+ }
+
+
def test_config_generation_is_private_and_never_logs_secrets(
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
diff --git a/vibe/model_hub_runtime/config.py b/vibe/model_hub_runtime/config.py
index 468cf5b47..e470959fc 100644
--- a/vibe/model_hub_runtime/config.py
+++ b/vibe/model_hub_runtime/config.py
@@ -1,12 +1,13 @@
from __future__ import annotations
from pathlib import Path
-from typing import Any, Iterable
+from typing import Any, Final, Iterable
from urllib.parse import urlsplit
import yaml
from config.atomic_io import write_atomic
+from core.agent_core.ai.claude_thinking import uses_adaptive_thinking
from config.v2_config import normalize_model_hub_base_url
from vibe.model_hub_runtime.api_key_vendors import (
google_api_base_url,
@@ -116,6 +117,25 @@ def render_engine_config(
return yaml.safe_dump(payload, sort_keys=False, default_flow_style=False)
+# How CLIProxyAPI's own catalog registers a Claude model that takes a token budget (registry/models/models.json).
+_CLAUDE_BUDGET_THINKING: Final = {"min": 1024, "max": 128000, "zero-allowed": True}
+
+
+def _thinking_support(source: SourceRecord, model: str, reasoning_efforts: tuple[str, ...]) -> dict[str, Any]:
+ """The thinking a model is registered with in the engine, by the kind it takes upstream.
+
+ CLIProxyAPI reads ``levels`` as adaptive thinking on its Claude path: a budget
+ request to such a model goes up as ``adaptive`` with an effort, which Claude 4.5
+ and older refuse. So a Claude model on the Anthropic protocol that takes a token
+ budget registers a budget range, as the engine's own catalog does; every other
+ model keeps its levels, strongest first (#2737).
+ """
+
+ if source.protocol == "anthropic" and "claude" in model.lower() and not uses_adaptive_thinking(model):
+ return dict(_CLAUDE_BUDGET_THINKING)
+ return {"levels": list(reversed(reasoning_efforts))}
+
+
def _append_source(
payload: dict[str, Any],
source: SourceRecord,
@@ -155,8 +175,7 @@ def _append_source(
}
reasoning_efforts = reasoning_by_model.get(model, ())
if reasoning_efforts:
- # CLIProxyAPI's measured model-registration shape is strongest-first.
- entry["thinking"] = {"levels": list(reversed(reasoning_efforts))}
+ entry["thinking"] = _thinking_support(source, model, reasoning_efforts)
if model in text_only:
entry["input-modalities"] = ["text"]
if model in images and source.protocol == "openai_chat":