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: clamp an unavailable reasoning effort to the nearest rung, not the weakest (#10575)

Michael Handd2b0f6ac2a6
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index d436e15cbbb..0c96e7271aa 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -2552,7 +2552,8 @@ _TOOL_TEMPLATE_MARKERS = (
# Canonical reasoning_effort levels, weakest -> strongest. Used to read the
# discrete set a template branches on (e.g. GLM-5.2 uses 'high' | 'max', Inkling
# uses the full 'none'..'max' ladder) so we only ever offer levels the template
-# actually understands.
+# actually understands. Must stay in sync with REASONING_EFFORT_SCALE in
+# studio/frontend/src/features/chat/provider-capabilities.ts.
_REASONING_EFFORT_SCALE = ("none", "minimal", "low", "medium", "high", "xhigh", "max")
# Match a Qwen3.8 path/repo segment without treating future names such as
diff --git a/studio/frontend/src/features/chat/provider-capabilities.ts b/studio/frontend/src/features/chat/provider-capabilities.ts
index 4ea4218ff3c..7ef5a16a90c 100644
--- a/studio/frontend/src/features/chat/provider-capabilities.ts
+++ b/studio/frontend/src/features/chat/provider-capabilities.ts
@@ -42,8 +42,29 @@ export type ExternalReasoningCapabilities = {
)[];
};
+/** Weakest -> strongest. Must stay in sync with _REASONING_EFFORT_SCALE in
+ * backend core/inference/llama_cpp.py. */
+const REASONING_EFFORT_SCALE = [
+ "none",
+ "minimal",
+ "low",
+ "medium",
+ "high",
+ "xhigh",
+ "max",
+] as const satisfies ExternalReasoningCapabilities["reasoningEffortLevels"];
+
/** Pick a stored effort level present in `effortLevels`, mapping legacy "xhigh" to "max"
- * when only the latter is exposed (Claude 4.6). */
+ * when only the latter is exposed (Claude 4.6).
+ *
+ * An unavailable level searches DOWNWARD to the nearest offered level, so the clamp never
+ * spends more compute than was asked for; only a level under everything on offer falls up to
+ * the weakest rung.
+ *
+ * "none" is the off switch, not a rung, so it is skipped unless it is what was asked for:
+ * otherwise a thinking request clamps onto thinking disabled (Mistral's none | high turned a
+ * stored Medium into none). llama_cpp.py strips "none" from an enable_thinking_effort ladder
+ * for the same reason; a reasoning_effort ladder keeps it, and an explicit ask still gets it. */
export function clampReasoningEffortToLevels(
preferred: ExternalReasoningCapabilities["reasoningEffortLevels"][number],
effortLevels: ExternalReasoningCapabilities["reasoningEffortLevels"],
@@ -59,6 +80,25 @@ export function clampReasoningEffortToLevels(
if (effortLevels.includes(candidate)) {
return candidate;
}
+ const rank = REASONING_EFFORT_SCALE.indexOf(
+ candidate as (typeof REASONING_EFFORT_SCALE)[number],
+ );
+ if (rank !== -1) {
+ // Scale order, not the order the provider table happens to list.
+ const offered = REASONING_EFFORT_SCALE.filter((level) =>
+ effortLevels.includes(level),
+ );
+ const rungs =
+ candidate === "none" ? offered : offered.filter((level) => level !== "none");
+ const searchable = rungs.length > 0 ? rungs : offered;
+ const below = searchable.filter(
+ (level) => REASONING_EFFORT_SCALE.indexOf(level) < rank,
+ );
+ const nearest = below.length > 0 ? below[below.length - 1] : searchable[0];
+ if (nearest !== undefined) {
+ return nearest;
+ }
+ }
return effortLevels[0] ?? "low";
}
diff --git a/studio/frontend/tests/provider-capabilities-current-models.test.ts b/studio/frontend/tests/provider-capabilities-current-models.test.ts
index c67ba606902..2fefb211f3e 100644
--- a/studio/frontend/tests/provider-capabilities-current-models.test.ts
+++ b/studio/frontend/tests/provider-capabilities-current-models.test.ts
@@ -2,7 +2,10 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import path from "node:path";
import test from "node:test";
+import { fileURLToPath } from "node:url";
import { registerBundlerResolver } from "./helpers/kit.ts";
@@ -92,6 +95,118 @@ test("Astra exposes mandatory reasoning with its full effort ladder", () => {
assert.equal(clampReasoningEffortToLevels("max", caps.reasoningEffortLevels), "max");
});
+// A local GGUF's ladder is whatever its template branches on, so it can skip scale rungs.
+test("a narrower local ladder clamps to the nearest rung, not to the weakest", () => {
+ const qwen38 = ["low", "medium", "xhigh"] as const;
+
+ assert.equal(clampReasoningEffortToLevels("high", qwen38), "medium");
+ assert.equal(clampReasoningEffortToLevels("max", qwen38), "xhigh");
+ for (const effort of qwen38) {
+ assert.equal(clampReasoningEffortToLevels(effort, qwen38), effort);
+ }
+ // No lower neighbour, so the weakest rung is right.
+ assert.equal(clampReasoningEffortToLevels("none", qwen38), "low");
+ assert.equal(clampReasoningEffortToLevels("minimal", qwen38), "low");
+});
+
+test("the xhigh -> max alias still wins over the neighbour search", () => {
+ // Claude 4.6 renamed the rung rather than dropping it: same level, not a clamp.
+ assert.equal(
+ clampReasoningEffortToLevels("xhigh", ["low", "medium", "high", "max"]),
+ "max",
+ );
+});
+
+test("clamping down never lands on thinking-off", () => {
+ for (const effort of ["minimal", "low", "medium"] as const) {
+ assert.equal(clampReasoningEffortToLevels(effort, ["none", "high"]), "high", effort);
+ }
+ assert.equal(
+ clampReasoningEffortToLevels("minimal", ["none", "low", "medium", "high"]),
+ "low",
+ );
+ assert.equal(clampReasoningEffortToLevels("none", ["none", "high"]), "none");
+ assert.equal(clampReasoningEffortToLevels("high", ["none"]), "none");
+});
+
+test("the ladder order a caller passes does not change the clamp", () => {
+ // A local ladder comes from a chat-template scan, so nothing keeps it ascending.
+ const ascending = ["low", "medium", "xhigh"] as const;
+ const descending = ["xhigh", "medium", "low"] as const;
+ for (const effort of ["none", "minimal", "low", "medium", "high", "xhigh", "max"] as const) {
+ assert.equal(
+ clampReasoningEffortToLevels(effort, descending),
+ clampReasoningEffortToLevels(effort, ascending),
+ effort,
+ );
+ }
+});
+
+test("every shipped effort ladder is ordered weakest first", () => {
+ // The Think menu renders table order, and `fallbackExternalEffort` in chat-adapter reads
+ // `reasoningEffortLevels[0]` as the weakest rung; out-of-order tables break both.
+ const models: Array<[string, string]> = [
+ ["anthropic", "claude-opus-5"],
+ ["anthropic", "claude-opus-4-6"],
+ ["anthropic", "claude-opus-4-5"],
+ ["anthropic", "claude-fable-5"],
+ ["openai", "gpt-5.6-sol"],
+ ["openai", "gpt-5.5-pro"],
+ ["openai", "gpt-5.1-codex-max"],
+ ["openai", "gpt-5.1"],
+ ["openai", "gpt-5"],
+ ["openai", "o3"],
+ ["openai_codex", "gpt-6-astra"],
+ ["gemini", "gemini-2.5-flash-lite"],
+ ["gemini", "gemini-2.5-pro"],
+ ["gemini", "gemini-2.5-flash"],
+ ["gemini", "gemini-3-pro"],
+ ["gemini", "gemini-3-flash"],
+ ["mistral", "magistral-medium-latest"],
+ ["mistral", "mistral-small-latest"],
+ ["openrouter", "openai/gpt-5.5"],
+ ];
+ const scale = ["none", "minimal", "low", "medium", "high", "xhigh", "max"];
+ for (const [provider, model] of models) {
+ const levels = [
+ ...getExternalReasoningCapabilities(provider, model).reasoningEffortLevels,
+ ];
+ const ranks = levels.map((level) => scale.indexOf(level));
+ assert.ok(!ranks.includes(-1), `${model} lists a level off the scale: ${levels}`);
+ assert.deepEqual(
+ ranks,
+ [...ranks].sort((a, b) => a - b),
+ `${model} ladder is not weakest first: ${levels}`,
+ );
+ assert.equal(new Set(levels).size, levels.length, `${model} repeats a level`);
+ }
+});
+
+test("the effort scale matches the backend's _REASONING_EFFORT_SCALE", () => {
+ // The clamp ranks against the frontend copy while detect_reasoning_flags builds local
+ // ladders from the backend one, so drift hands the clamp a level it cannot rank.
+ const here = path.dirname(fileURLToPath(import.meta.url));
+ const frontend = readFileSync(
+ path.join(here, "../src/features/chat/provider-capabilities.ts"),
+ "utf8",
+ );
+ const backend = readFileSync(
+ path.join(here, "../../backend/core/inference/llama_cpp.py"),
+ "utf8",
+ );
+ const frontendScale = frontend
+ .match(/const REASONING_EFFORT_SCALE = \[([\s\S]*?)\] as const/)?.[1]
+ .match(/"([a-z]+)"/g)
+ ?.map((quoted) => quoted.slice(1, -1));
+ const backendScale = backend
+ .match(/^_REASONING_EFFORT_SCALE = \(([^)]*)\)/m)?.[1]
+ .match(/"([a-z]+)"/g)
+ ?.map((quoted) => quoted.slice(1, -1));
+ assert.ok(frontendScale, "frontend REASONING_EFFORT_SCALE not found");
+ assert.ok(backendScale, "backend _REASONING_EFFORT_SCALE not found");
+ assert.deepEqual(frontendScale, backendScale);
+});
+
test("Astra reasoning does not enable unrelated model families", () => {
for (const model of ["gpt-6-other", "gpt-60-astra"]) {
assert.equal(getExternalReasoningCapabilities("openai_codex", model).supportsReasoning, false);