Skip to content

Studio: Respect the thinking budget on /v1/messages - #11724

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
NilayYadav:messages-thinking-budget
Sep 29, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
NilayYadav:messages-thinking-budget

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Requests to /v1/messages could set a thinking budget with budget_tokens, but Studio threw it away, so the model thought for as long as it liked. With a small max_tokens, a reply could end before any answer was written.

Studio now passes the budget on to the model when thinking is on. Requests without a budget, or with thinking off, behave as before. This follows #9107.

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Sep 23, 2026
…i#11735)

* Count the stripper's whole-buffer work instead of timing it

test_early_markup_is_not_slower_than_the_code_it_replaces compared CPU time
against the reference strip with a 10% margin. With markup at the front and
nothing settled the two arms do the same strip, so the ratio sits at 1.0 and
runner noise alone spans 0.89 to 1.09; it failed on unslothai#11724, which does not touch
the stripper, at 0.444s against 0.404s. The regression it guards, paying the
whole-buffer checks on every token, measured 1.11 to 1.33, so no margin
separates it from the noise.

Count the work instead: no token pays _needs_whole_buffer, and _full_strip
reads no more text than the reference. A second case puts the markup at offset
0, which reaches the degenerate branch the timed version never exercised.

* Count the passes inside the strip, in both arms

Counting only _full_strip's argument could not see a second strip or scan
inside it. Count the characters each arm hands to strip_outside_think and
strip_segment, and to the blocked-body masking, and hold the incremental arm
to the reference's totals. A doubled strip_outside_think or masking pass now
fails with the counts that grew.
pull Bot pushed a commit to pirica/unsloth that referenced this pull request Sep 23, 2026
…nslothai#11741)

* studiobench: compare a code fence one arm scrolled past on its text

The UI parity gate failed on backend-only pull requests (unslothai#11724, unslothai#11727,
and others on 2026-09-23) with differences like
msg15(assistant):1847269->2150610c on model_change, reasoning_toggle,
select_all_copy, settings and image_upload at r100K.

Those are not rendering differences. code-fence-defer.tsx (unslothai#9462, the
default since unslothai#9567) renders a fence as a plain shell until it first comes
within a viewport of the reader, then highlights it for the rest of the
mount. After reasoning_toggle the r100K film leaves the viewport either
near the tail (scrollTop ~50,740) or about 20,000px higher, at random per
cell on one build (78 of 172 cells across 43 recorded payloads), and the higher landing
latches the fences of msg11/13/15 until thread_reopen remounts them. A null
control whose cells all landed low measured those actions stable, so the
result's latch noise was scored as a regression. The same noise also made
a null that did see it excuse settings and model_change outright, which
hid real changes there.

scene/parity.js now reads, in the same walk that produces the unchanged
per-message digest, each fence's latch state, full digest and text digest
(whitespace dropped, since streamdown emits one span per line and the
shell one text node), plus the message with every fence replaced by a
marker. analysis/parity.fence_latch_residue treats a message as differing
only by latch when everything outside its fences matches, every fence
latched on both arms or neither matches in full, and every fence latched
on one arm only matches on its text. Nothing is excused when one arm
latched no fence anywhere, which is what a build whose fences never
upgrade looks like. A latch-only pair is a MATCH, counted and printed, so
it costs no coverage; payloads recorded before this carry no fence fields
and score exactly as before.

The selftest replays both failing runs from their recorded CI payloads,
shows they fail as recorded and pass once the latch is recorded, and
injects genuine differences (a message with no fences, a change outside a
latched message's fences, a highlighting change on a fence both arms
latched, fences that never upgrade) that must still fail.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* studiobench: keep a fence's spacing and language in the latch comparison

The one-sided fence comparison dropped every whitespace character, so
`x = 1` and `x=1`, an indentation change or a space inside a string
read as the same fence, and it never looked at the language, so a fence
that changed language but kept its body read as latch-only too. Drop line
breaks only, the one thing that differs between the shell and the
highlighted form, keep the rest of the text as it is, and record and
compare each fence's data-language.

* studiobench: read a fence's text as lines, not a run of characters

Dropping every newline made a line break moved within the code, `a\nbc`
against `ab\nc`, read as the same fence. Read both forms as lines: the
shell's text split on newlines, and the highlighted fence one line per
element directly inside its <code>, a blank line's lone newline dropped.
Join them with one newline and drop trailing blank lines, which the shell
trims.

* studiobench: compare a fence's text as written, without prose placeholders

The time and id placeholders are there for UI prose. Applied to a one-sided
fence's source they made `"295ms"` against `"310ms"`, or two different
timestamp or id literals, read as the same code.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Review + GitHub Actions evidence

Pinned review: 688a6f0df70df7ccfdb6fb6d657e5831ef87f065. No blocking code findings, and Codex review converged with no findings on the staged head.

I ran a real Studio (unsloth studio run, unsloth/Qwen3-1.7B-GGUF UD-Q4_K_XL, CPU llama-server) on fresh upstream main 81e2d07. Both arms used the same workflow and probe; the only difference is whether this PR is merged in. Each case sends /v1/messages with thinking: {type: "enabled", budget_tokens: 32} and then without a budget, with max_tokens: 1536 and temperature 0.

Path Before: run, thinking chars with budget 32 After: run, thinking chars with budget 32 After: no budget
plain 4210 (stop max_tokens) 101 (stop end_turn) 3799
plain, stream 3682 101 4278
client-tool passthrough 1820 109 3524
client-tool passthrough, stream 1242 107 4088
  • Before: budget_tokens was ignored. In the plain case the model reasoned until max_tokens ran out. All 4 cases failed the <= 600 chars assertion after Studio came up healthy.
  • After: thinking was capped at the budget on every path and the answer followed (end_turn). Requests without a budget still think freely.

Other checks:

  • llama-server reads thinking_budget_tokens from the chat body in every tag Studio installs, back to the b9415 macOS fallback pin (tools/server/server-common.cpp).
  • Focused tests on the PR head: 508 passed. The same tests on the merge base: 20 fail, so they do catch the missing forwarding.
  • The only red upstream check, studiobench UI parity, fails on unrelated PRs across the last day. It is not caused by this PR.

shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…i#11735)

* Count the stripper's whole-buffer work instead of timing it

test_early_markup_is_not_slower_than_the_code_it_replaces compared CPU time
against the reference strip with a 10% margin. With markup at the front and
nothing settled the two arms do the same strip, so the ratio sits at 1.0 and
runner noise alone spans 0.89 to 1.09; it failed on unslothai#11724, which does not touch
the stripper, at 0.444s against 0.404s. The regression it guards, paying the
whole-buffer checks on every token, measured 1.11 to 1.33, so no margin
separates it from the noise.

Count the work instead: no token pays _needs_whole_buffer, and _full_strip
reads no more text than the reference. A second case puts the markup at offset
0, which reaches the degenerate branch the timed version never exercised.

* Count the passes inside the strip, in both arms

Counting only _full_strip's argument could not see a second strip or scan
inside it. Count the characters each arm hands to strip_outside_think and
strip_segment, and to the blocked-body masking, and hold the incremental arm
to the reference's totals. A doubled strip_outside_think or masking pass now
fails with the counts that grew.
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…nslothai#11741)

* studiobench: compare a code fence one arm scrolled past on its text

The UI parity gate failed on backend-only pull requests (unslothai#11724, unslothai#11727,
and others on 2026-09-23) with differences like
msg15(assistant):1847269->2150610c on model_change, reasoning_toggle,
select_all_copy, settings and image_upload at r100K.

Those are not rendering differences. code-fence-defer.tsx (unslothai#9462, the
default since unslothai#9567) renders a fence as a plain shell until it first comes
within a viewport of the reader, then highlights it for the rest of the
mount. After reasoning_toggle the r100K film leaves the viewport either
near the tail (scrollTop ~50,740) or about 20,000px higher, at random per
cell on one build (78 of 172 cells across 43 recorded payloads), and the higher landing
latches the fences of msg11/13/15 until thread_reopen remounts them. A null
control whose cells all landed low measured those actions stable, so the
result's latch noise was scored as a regression. The same noise also made
a null that did see it excuse settings and model_change outright, which
hid real changes there.

scene/parity.js now reads, in the same walk that produces the unchanged
per-message digest, each fence's latch state, full digest and text digest
(whitespace dropped, since streamdown emits one span per line and the
shell one text node), plus the message with every fence replaced by a
marker. analysis/parity.fence_latch_residue treats a message as differing
only by latch when everything outside its fences matches, every fence
latched on both arms or neither matches in full, and every fence latched
on one arm only matches on its text. Nothing is excused when one arm
latched no fence anywhere, which is what a build whose fences never
upgrade looks like. A latch-only pair is a MATCH, counted and printed, so
it costs no coverage; payloads recorded before this carry no fence fields
and score exactly as before.

The selftest replays both failing runs from their recorded CI payloads,
shows they fail as recorded and pass once the latch is recorded, and
injects genuine differences (a message with no fences, a change outside a
latched message's fences, a highlighting change on a fence both arms
latched, fences that never upgrade) that must still fail.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* studiobench: keep a fence's spacing and language in the latch comparison

The one-sided fence comparison dropped every whitespace character, so
`x = 1` and `x=1`, an indentation change or a space inside a string
read as the same fence, and it never looked at the language, so a fence
that changed language but kept its body read as latch-only too. Drop line
breaks only, the one thing that differs between the shell and the
highlighted form, keep the rest of the text as it is, and record and
compare each fence's data-language.

* studiobench: read a fence's text as lines, not a run of characters

Dropping every newline made a line break moved within the code, `a\nbc`
against `ab\nc`, read as the same fence. Read both forms as lines: the
shell's text split on newlines, and the highlighted fence one line per
element directly inside its <code>, a blank line's lone newline dropped.
Join them with one newline and drop trailing blank lines, which the shell
trims.

* studiobench: compare a fence's text as written, without prose placeholders

The time and id placeholders are there for UI prose. Applied to a one-sided
fence's source they made `"295ms"` against `"310ms"`, or two different
timestamp or id literals, read as the same code.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@danielhanchen danielhanchen self-assigned this Sep 29, 2026
@shimmyshimmer

Copy link
Copy Markdown
Member

Review record for #11724, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#209.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror a1179593c (this PR's 70eb7eae3)

chatgpt-codex-connector[bot] on studio/backend/routes/inference.py (on the mirror)

@@ -4871,11 +4871,15 @@ def _anthropic_reasoning_args(payload) -> dict:
         resolver = getattr(payload, "resolved_enable_thinking", None)
         if resolver is not None:
             enable_thinking = resolver()
-    return {
+    args = {
         "enable_thinking": enable_thinking,
         "reasoning_effort": reasoning_effort,
         "preserve_thinking": payload.preserve_thinking,
     }
+    budget = getattr(getattr(payload, "thinking", None), "budget_tokens", None)
+    if enable_thinking and isinstance(budget, int) and budget > 0:
+        args["thinking_budget_tokens"] = budget

P2 Badge Ignore native budgets when extension controls take precedence

When a request supplies an x-unsloth reasoning control together with thinking.budget_tokens—for example, reasoning_effort="high" plus an enabled native thinking block—_resolve_reasoning_controls makes the extension authoritative, but this still reads and forwards the budget from the lower-priority native block. The backend therefore hard-stops reasoning at that native limit, potentially truncating a high-effort request despite the documented rule that the extension controls win; only forward this budget when no x-unsloth reasoning override was supplied.

Useful? React with 👍 / 👎.

Rejected on the mirror

Reply from danielhanchen:

Not changing this. The extension controls decide whether thinking is on and at what effort; when they turn it off the budget is already dropped (enable_thinking=False and reasoning_effort=none cases in the tests). A budget_tokens the client sent explicitly is a cap it asked for, not a conflicting switch, and mixing native thinking with x-unsloth fields is not something real Anthropic clients send.

Round 2: reviewed mirror a1179593c0 (this PR's 70eb7eae3)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: a1179593c0

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror a1179593c0 (this PR's 70eb7eae3).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit 7ff3d23 into unslothai:main Sep 29, 2026
46 of 49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants