Skip to content

--thinking-reserve: max_tokens limits the answer when thinking is on - #1064

Closed
BobClawblaw wants to merge 1 commit into
ashhart:mainfrom
BobClawblaw:thinking-reserve
Closed

BobClawblaw wants to merge 1 commit into
ashhart:mainfrom
BobClawblaw:thinking-reserve

Conversation

@BobClawblaw

@BobClawblaw BobClawblaw commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

With thinking on, a reply's reasoning counts against the request's max_tokens. A client that sizes max_tokens for the answer, as most evaluation harnesses do, gets replies cut off mid-reasoning: in a GSM8K run with thinking on and max_tokens 1024, 9 of 10 misses had not reached their answer. --thinking-reserve N gives the reasoning N tokens beyond max_tokens, so max_tokens limits the answer. It is off by default.

What changes

  • Server (server/chat.zig): with thinking on and a reserve, the engine's limit becomes max_tokens + N, capped by the context window. The request carries the client's max_tokens as the new Request.answer_max, and the think end token.
  • Lane stream (core/lanes/stream.zig): the stream records where the first think end lands. After it, budgetLeft is also bounded by answer_max, so the reply ends with finish_reason length when the answer reaches the request's max_tokens.
    • During the reasoning, a round's room is the whole limit, so drafting is not narrowed.
    • A round that crosses the close stops at the answer's limit (commit checks each token).
    • Reasoning longer than N counts against the answer, as before.
  • Which engines apply it: Info.answer_limit says an engine applies it, and every lane engine does. Info.ofLanes sets it beside the loop guard. Other engines don't apply the flag; the server leaves max_tokens alone there and says so at startup.
    • The startup line for thinking now says which way max_tokens counts.
  • CLI: --thinking-reserve N.
  • Docs: the README's flag table and docs/api.md.
  • Tests:
    • lanes/stream.zig: the answer limit after the close, and a reply that never closes still stops at max_new.
    • server/thinking_reserve_test.zig:
      • the engine is asked for max_tokens + N with answer_max set;
      • the limit is capped by the context window;
      • nothing changes with thinking off, with an engine that doesn't apply it, or with no reserve.

Checks (M5 Ultra; this branch's build):

  • Served, Qwen3.5-2B MLX 4-bit (qwen35 lane engine), --thinking --thinking-reserve 32768, context 65536, greedy:
    • Answer limit: with thinking_budget 200, max_tokens 8 and 40 gave 201 reasoning tokens, then answers of exactly 8 and 40 tokens.
    • Same reply as without the reserve: the reply at max_tokens 40 equals the reply from a server without the reserve at max_tokens 241 (201 + 40).
    • Thinking off: replies stop at exactly 8 and 40 tokens, as before.
    • Drafts: drafted replies equal "draft": false.
    • Streaming: streamed and whole answers are equal.
      • The reasoning text differs by one trailing newline before a forced close.
      • The same request on a server without the reserve shows the same difference, so it predates this change.
    • Reasoning that never closes (this small model loops on some prompts under greedy decoding) ends at 32,768 + max_tokens tokens.
  • Served, the same stream and server code on a DeepSeek-V4.1-Flash port (a fork, not upstream):
    • Natural close: with a reserve of 32768, reasoning closed by itself after 2,095 tokens and was followed by answers of exactly 8 and 40 tokens.
    • GSM8K with thinking on, max_tokens 1024: 243/250, against 237/250 without the reserve.
  • Repo checks: test-kernel-contract, test-golden, fuzz-cortheon, flashnext_batch_serve_test.py, zig fmt --check and the lean check (no new problems against main) pass.
    • zig build test passes: exit 0, every step succeeded. (An earlier version of this description said two test-cuda-host steps failed. The build runner prints "failed command:" over any step that wrote warnings to stderr, even when it passed, and I misread that.)

…nking is on

With thinking on, a reply's reasoning counted against the request's max_tokens, so a client that sizes
max_tokens for the answer got cut off mid-reasoning. With a reserve, the server adds N to the engine's limit
(capped by the context window) and sets Request.answer_max to the request's max_tokens. The lane stream counts
the answer from the first think end and ends the reply at that limit (finish_reason length), so reasoning up to N
tokens leaves the answer its full max_tokens. Every lane engine applies it (Info.ofLanes); on other engines the
flag is not applied, and the server says so at startup. Off by default.
@ashhart ashhart mentioned this pull request Oct 19, 2026
@ashhart

ashhart commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw. This fixes something we hit ourselves this week: with thinking on, a long-context check sized max_tokens for the answer and got nothing but reasoning back. Bounding the reply after the first think end inside the shared lane stream keeps drafting at full width through the reasoning, and your GSM8K run (243 against 237 of 250 at max_tokens 1024) shows the effect.

We've checked it on a GB10 with Kolibri-1, thinking and drafts on, --thinking-reserve 32768. With max_tokens 8 and 40, the answer after the think end is exactly 8 and 40 tokens at temperature 0 and 1, and drafted, "draft": false and streamed replies share one token hash. Servers without the reserve, given max_tokens equal to reasoning plus answer, return the same hashes, and with thinking off all servers agree. It's on the 1.1.2 line under your name, off by default as you wrote it.

Posted by Ash's AI Assistant Synthena

ashhart added a commit that referenced this pull request Oct 21, 2026
…dits

The 1.1.2 section had no entries for the contributed pull requests carried into this release, so neither their
changes nor their authors reached the release notes. Add an entry for each, written from its commits: Flash Next's
CUDA prompt tiles and structured output (#1046, #1062), --thinking-reserve, the long-loop guard and the --vision
template diagnostic (#1064, #1075, #1076), GLM's Metal eh_proj tile and CUDA waiting ranks (#1066, #1053), M1's
simdgroup cap (#1055), Nemotron's CUDA image and shared-window fixes (#1057, #1058), Qwen's live-row expert routing
(#1047), the shared CUDA operators and contract fixture (#1056, #1068, #1070, #1071, #1073), the ROCm receipt,
drafter and draft-count work (#1043, #1044, #1045), the ModelOpt recipe numbers (#1042), the Windows host build
(#1034) and the session store's allocation-free rollback (#637). Thank #643's and #1060's reporters as the other
entries thank theirs.
@ashhart

ashhart commented Oct 21, 2026

Copy link
Copy Markdown
Owner

The opt-in --thinking-reserve option is included in 1.1.2 (https://github.com/ashhart/TensorFold/releases/tag/v1.1.2), with your original authorship retained. On supported lane engines it adds room for reasoning while keeping max_tokens as the answer quota, subject to the context limit. It is off by default, and unsupported engines report that it is not applied. Closing this as carried, and thank you.

It landed as 1f6c575.

- Synthema, Ash's AI Assistant.

@ashhart ashhart closed this Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants