Skip to content

Studio: stop earlier thoughts from shrinking the thinking budget - #19143

Merged
Lyxot merged 4 commits into
unslothai:mainfrom
NilayYadav:thinking-budget-rendered-prompt
Nov 24, 2026
Merged

Lyxot merged 4 commits into
unslothai:mainfrom
NilayYadav:thinking-budget-rendered-prompt

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

A few turns into a chat with a thinking model, the reasoning got cut short after a few hundred tokens even though most of the context was free. The thinking budget counted every earlier thought in the chat, but Qwen3, Qwen3.5/3.6 and Gemma 4 drop those thoughts from the prompt. The budget is now sized from the prompt the model actually receives. On an 8K context with five earlier long thoughts, the budget goes from 1,010 tokens to 5,987. A first message, thinking off, and an explicit budget behave as before.

@NilayYadav NilayYadav self-assigned this Nov 23, 2026
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T07:21:59.769071Z 125f09e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: ead41b650d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The budget count now passes the request's continue_final_message and, in tool-loop rounds, the payload's own chat_template_kwargs (MCP discovery overrides its tools). It skips counting once the request is cancelled and lets an in-progress count stop between calls.

Tests cover templates that drop and keep earlier thoughts, assert the budget comes from the returned count at the plain, tool-round and final-pass sites, and stub the count instead of reaching a local port.
@Lyxot

Lyxot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Pushed a follow-up on top of your commit (125f09e9b01), after merging latest main into the branch (f01f49621ef).

What changed:

  • The budget count now prices exactly the request it guards. It passes the request's continue_final_message, and in tool-loop rounds it uses the payload's own chat_template_kwargs, since MCP tool discovery overrides tools there.
  • It skips counting when the request is already cancelled, and passes should_abort so an in-progress count can stop between its render and tokenize calls.
  • Tests now cover templates that drop and that keep earlier thoughts. They assert the sent budget is computed from the returned count at the plain, tool-round and final-pass sites, and stub the count instead of reaching a local port.

Verified live with Qwen3.5-0.8B GGUF, 8K context, 5 earlier turns with long thoughts, through /v1/chat/completions:

  • main sent no budget, reasoning ran to the window edge, and the answer was empty;
  • this branch counted the real 214-token prompt, sent a 5984-token budget, the thought closed and the answer followed;
  • a template that keeps every earlier thought got a budget of exactly window_budget(8192, 3038) for its real 3038-token prompt.

Open tradeoff: on paths without context_overflow: "truncate_oldest" (the API default, or the chat with auto-compaction off), the budget adds one /apply-template + /tokenize per thinking turn; with truncate_oldest it reuses the context fit's cached count and adds nothing. #19159, now in its own final CI, removes this for plain turns by reusing admission's cached native count. Server-side tool-loop passes on those paths still pay it.

@Lyxot

Lyxot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 125f09e9b0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot

Lyxot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Codex review converged on 125f09e9b01: #19143 (comment)

CI: upstream jobs stayed pending, so the same head ran on a staging PR in my fork based on current main 30193d5a27a (Lyxot#283, now closed). Result: 48 pass, 4 skipping, 4 fail. All four failures also fail on plain main 30193d5a27a, so they are not from this PR:

  • (Python 3.13, a-f): test_no_test_module_imports_an_unsloth_backed_module_unstubbed (flags test_csm_processor_pinned_source.py)
  • (Python 3.13, diffusion): test_no_diffusion_or_video_gate_reads_version_hip_directly
  • (Python 3.13, m-r): test_bare_base64_is_payload_and_still_passes_through
  • Repo tests (CPU, studio): test_spark_tts_bicodec_handoff.py (8) and test_chat_preset_load_config.py::test_apply_skips_missing_load_config

What changed in this workflow: one commit, 125f09e9b01, on top of the original head, as described in the comment above. No changes after Codex review. Merge order: #19159 is still open; it removes the extra render + tokenize on plain thinking turns without truncate_oldest, so landing it with or after this PR is fine.

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The budget is now sized from the prompt the model actually receives. Verified live, Codex converged, and the targeted tests pass on current main.

@Lyxot
Lyxot merged commit d4b6be4 into unslothai:main Nov 24, 2026
47 of 51 checks passed
Hubert9204 pushed a commit to Hubert9204/Ganjalf-Studio that referenced this pull request Nov 24, 2026
…-11-23..25)

Merge of unslothai/unsloth main 2e1afd5..bd1ddf2. No textual conflicts; audited for
Ganjalf invariants (Claude API alignment, thinking budget, Qwen thinking presence 0,
check_page, guards, plugins/hooks/run_agent stop, branding via messages.ts, Ganjalf updater):
all intact.

Taken: llama.cpp/GGUF fixes (thinking budget counted with the real template unslothai#19143, native
prompt count in context fit unslothai#19159, host RAM growth priced before pinning unslothai#19231, second quant
of a loaded repo unslothai#19169, small Auto context on low-RAM CPU hosts unslothai#19142), security and
stability hardening (media bytes checked, remote-code gate, sandbox cache, managed accounts,
IPv6 loopback listeners), chat fixes (Files menu, deleted turns from other tabs, composer,
tool approvals tied to their tool), MCP, chat templates (phi-3, mistral), training/core
kernels, installer AMD/ROCm mixed hosts, Tauri security bumps, unsloth-zoo>=2026.11.11.
Branding stays Ganjalf (upstream strings are rebranded at runtime by i18n/messages.ts).

Ganjalf follow-up: the thread sampling simulation oracle expects presence 0 for Qwen
3.5/3.6 with thinking (non-thinking 1.5), as the chat table does since 369d05c.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EVYBhJeMQGvLT6Ka4nMMUE
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