Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio llama.cpp: remember the positive reasoning budget probe per binary (#17031)

Daniel Hand70540630d04
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index a4719025f49..bd90a0dca41 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -10252,6 +10252,8 @@ class LlamaCppBackend:
_build_number_cache: dict[tuple[str, int, int], Optional[int]] = {}
_capability_retry_after: dict[tuple[str, int, int], float] = {}
_capability_retry_backoff: dict[tuple[str, int, int], float] = {}
+ # Not on the caps dict: probe_server_capabilities returns a fresh copy on --fit-target builds.
+ _reasoning_budget_value_cache: dict[tuple[str, int, int, int], bool] = {}
_capability_cache_lock = threading.Lock()
# Older builds take a bare boolean -fa and exit on a following "on".
@@ -10888,27 +10890,47 @@ class LlamaCppBackend:
value_key = f"supports_reasoning_budget_value:{effective_budget}"
if value_key not in caps:
probe_binary = cls._exec_path_for_launch(binary)
- probe_env = cls._llama_server_env_for_binary(probe_binary)
- # llama-server parses LLAMA_ARG_* before argv, so clear stale inherited values.
- for name in tuple(probe_env):
- if name.startswith("LLAMA_ARG_"):
- probe_env.pop(name, None)
- if sys.platform == "darwin":
- probe_env["GGML_METAL_DEVICES"] = "0"
try:
- result = subprocess.run(
- [probe_binary, "--reasoning-budget", str(effective_budget), "--help"],
- capture_output = True,
- text = True,
- encoding = "utf-8",
- errors = "replace",
- timeout = 10,
- check = False,
- env = probe_env,
- )
- caps[value_key] = result.returncode == 0
- except (OSError, subprocess.SubprocessError):
- caps[value_key] = False
+ probe_stat = Path(probe_binary).stat()
+ value_cache_key = (
+ str(probe_binary),
+ probe_stat.st_mtime_ns,
+ probe_stat.st_size,
+ effective_budget,
+ )
+ except OSError:
+ value_cache_key = None
+ with cls._capability_cache_lock:
+ known = cls._reasoning_budget_value_cache.get(value_cache_key)
+ if known is not None:
+ caps[value_key] = known
+ else:
+ probe_env = cls._llama_server_env_for_binary(probe_binary)
+ # llama-server parses LLAMA_ARG_* before argv, so clear stale inherited values.
+ for name in tuple(probe_env):
+ if name.startswith("LLAMA_ARG_"):
+ probe_env.pop(name, None)
+ if sys.platform == "darwin":
+ probe_env["GGML_METAL_DEVICES"] = "0"
+ try:
+ result = subprocess.run(
+ [probe_binary, "--reasoning-budget", str(effective_budget), "--help"],
+ capture_output = True,
+ text = True,
+ encoding = "utf-8",
+ errors = "replace",
+ timeout = 10,
+ check = False,
+ env = probe_env,
+ **_windows_hidden_subprocess_kwargs(),
+ )
+ caps[value_key] = result.returncode == 0
+ # Spawn errors, timeouts, signals and NTSTATUS crashes (unsigned) probe again.
+ if value_cache_key is not None and 0 <= result.returncode < 0xC0000000:
+ with cls._capability_cache_lock:
+ cls._reasoning_budget_value_cache[value_cache_key] = caps[value_key]
+ except (OSError, subprocess.SubprocessError):
+ caps[value_key] = False
if not caps[value_key]:
raise ValueError(
f"llama-server at {binary} does not accept positive reasoning budgets. "
diff --git a/studio/backend/tests/test_llama_cpp_mtp_detection.py b/studio/backend/tests/test_llama_cpp_mtp_detection.py
index 65f9e024cee..433bbc47110 100644
--- a/studio/backend/tests/test_llama_cpp_mtp_detection.py
+++ b/studio/backend/tests/test_llama_cpp_mtp_detection.py
@@ -666,6 +666,7 @@ def _clear_caps_cache():
with LlamaCppBackend._capability_cache_lock:
LlamaCppBackend._capability_cache.clear()
LlamaCppBackend._capability_retry_after.clear()
+ LlamaCppBackend._reasoning_budget_value_cache.clear()
@_NEEDS_BASH
@@ -3764,3 +3765,84 @@ def test_positive_budget_probe_uses_resolved_launch_path(monkeypatch):
reasoning_budget_message = "",
)
assert caps["supports_reasoning_budget_value:32"] is True
+
+
+def test_positive_budget_probe_runs_once_per_binary_and_budget(tmp_path, monkeypatch):
+ """Caps are a fresh copy on --fit-target builds: one spawn per (binary, budget, build)."""
+ import core.inference.llama_cpp as llama_cpp_module
+
+ binary = tmp_path / "llama-server"
+ binary.write_bytes(b"v1")
+ monkeypatch.setattr(LlamaCppBackend, "_reasoning_budget_value_cache", {}, raising = False)
+ monkeypatch.setattr(
+ LlamaCppBackend,
+ "probe_server_capabilities",
+ classmethod(
+ lambda cls, binary = None: {
+ "supports_reasoning_budget": True,
+ "supports_reasoning_budget_message": True,
+ "supports_fit_target": True,
+ }
+ ),
+ )
+ monkeypatch.setattr(
+ LlamaCppBackend, "_llama_server_env_for_binary", staticmethod(lambda binary: {})
+ )
+ spawned = []
+
+ def _run(cmd, **kwargs):
+ spawned.append(cmd[2])
+ return _types.SimpleNamespace(returncode = 0 if cmd[2] == "512" else 1, stdout = "", stderr = "")
+
+ monkeypatch.setattr(llama_cpp_module.subprocess, "run", _run)
+
+ def _validate(budget):
+ return LlamaCppBackend.validate_reasoning_budget_capabilities(
+ str(binary),
+ extra_args = None,
+ reasoning_budget = budget,
+ reasoning_budget_message = "",
+ )
+
+ for _ in range(3):
+ assert _validate(512)["supports_reasoning_budget_value:512"] is True
+ assert spawned == ["512"]
+ for _ in range(2):
+ with pytest.raises(ValueError, match = "does not accept positive reasoning budgets"):
+ _validate(64)
+ assert spawned == ["512", "64"]
+ binary.write_bytes(b"v2 build")
+ assert _validate(512)["supports_reasoning_budget_value:512"] is True
+ assert spawned == ["512", "64", "512"]
+
+
+def test_positive_budget_probe_error_is_not_remembered(tmp_path, monkeypatch):
+ import core.inference.llama_cpp as llama_cpp_module
+
+ binary = tmp_path / "llama-server"
+ binary.write_bytes(b"v1")
+ monkeypatch.setattr(LlamaCppBackend, "_reasoning_budget_value_cache", {}, raising = False)
+ monkeypatch.setattr(
+ LlamaCppBackend,
+ "probe_server_capabilities",
+ classmethod(lambda cls, binary = None: {"supports_reasoning_budget": True}),
+ )
+ monkeypatch.setattr(
+ LlamaCppBackend, "_llama_server_env_for_binary", staticmethod(lambda binary: {})
+ )
+ outcomes = [llama_cpp_module.subprocess.TimeoutExpired("llama-server", 10), -11, 0xC0000005, 0]
+
+ def _run(cmd, **kwargs):
+ outcome = outcomes.pop(0)
+ if isinstance(outcome, Exception):
+ raise outcome
+ return _types.SimpleNamespace(returncode = outcome, stdout = "", stderr = "")
+
+ monkeypatch.setattr(llama_cpp_module.subprocess, "run", _run)
+ kwargs = dict(extra_args = None, reasoning_budget = 256, reasoning_budget_message = "")
+ for _ in range(3):
+ with pytest.raises(ValueError):
+ LlamaCppBackend.validate_reasoning_budget_capabilities(str(binary), **kwargs)
+ caps = LlamaCppBackend.validate_reasoning_budget_capabilities(str(binary), **kwargs)
+ assert caps["supports_reasoning_budget_value:256"] is True
+ assert outcomes == []