Suggested Title

"llama.cpp Just Got a Reasoning Kill Switch"

Word Count

1,847

Body

Hybrid reasoning models are amazing right up until they are not.

You fire off a simple question. The model opens a <think> block. Then it thinks. And thinks. Three hundred tokens later it is still exploring edge cases you did not ask about. Your GPU fans are screaming. Your latency budget is in ruins. And the actual answer has not even started yet.

This is the dark side of models like Qwen 3.5 and DeepSeek V3. They can reason. They are trained to reason. But they do not know when to stop.

llama.cpp just shipped a fix. PR #20297, merged in March 2026, adds a --reasoning-budget flag that works at the sampler level. It is not a prompt hack. It is not a post-process filter. It is a hard token cap enforced inside the decode loop itself. When the budget runs out, the sampler forces the model to close its reasoning block and move on.

Here is how it works, why it matters, and how to tune it.

The Problem: Infinite Think Loops

Hybrid reasoning models output their internal monologue inside special tags. Qwen 3.5 uses <think> and </think>. DeepSeek uses similar markers. The model decides when reasoning is "done" and emits the closing tag.

That decision is soft. The model keeps going until its internal pattern matcher decides the chain is complete. For easy questions this might be 20 tokens. For hard questions it can balloon to 500, 1000, or more. The model has no concept of your latency requirements. It does not know you are paying per token. It does not care that you are running on a laptop with 8GB of VRAM.

Before PR #20297, llama.cpp had --reasoning-budget as a stub parameter. Setting it to 0 disabled reasoning by flipping enable_thinking to false. Any other value did nothing. There was no actual enforcement.

What Changed: A State Machine Inside the Sampler

The new implementation adds a reasoning-budget sampler that sits in the sampling chain and runs a small state machine across five states:

  • IDLE — Waiting for the opening tag sequence (e.g., <think>)
  • COUNTING — Counting down tokens inside the reasoning block
  • WAITING_UTF8 — Budget expired, but the current token is mid-UTF-8 sequence; hold until the character completes
  • FORCING — Injecting the forced end sequence token by token
  • DONE — Reasoning block closed, pass through everything else normally

The sampler tracks three token sequences: a start trigger, an end trigger, and a forced sequence. When the start trigger fires, the countdown begins. The sampler watches for the natural end trigger the whole time. If the model emits it before the budget runs out, the sampler transitions straight to DONE. No interference.

If the budget hits zero first, the sampler waits for any incomplete UTF-8 character to finish. Then it enters FORCING mode. In this mode it sets every logit to negative infinity except the next required token in the end sequence. The model has no choice. It must emit the closing tag.

The UTF-8 wait is important. Without it, the sampler could cut a multi-byte character in half and produce invalid Unicode. The code checks commonutf8is_complete() on each token's piece before forcing the shutdown.

The sampler also re-arms if it sees another start tag later in the generation. Some models emit multiple reasoning blocks per response. Each one gets its own fresh budget window.

Two New Flags

PR #20297 introduced:

  • --reasoning on|off — Enables or disables reasoning via model kwargs
  • --reasoning-budget N — Caps reasoning to N tokens
  • --reasoning-budget-message "text" — Appends a custom message before the forced close tag

Setting --reasoning-budget 0 is now different from --reasoning off. A budget of 0 lets the model open a reasoning block, then immediately forces it shut. This works even on models that do not officially support disabling reasoning. The PR author notes this is possible but not recommended for models trained to always reason. They may try to open a second reasoning block.

The budget message is a nice touch. Instead of a hard cutoff, you can inject something like "Okay, time to answer." The model receives this text before the </think> tag, which can improve answer quality after an interrupted reasoning chain.

In April 2026, a follow-up refactor (PR #22233) moved reasoningbudgetmessage into the sampling parameters and standardized on reasoningbudgettokens. The behavior stayed the same, but the API got cleaner.

Why This Matters for Self-Hosting

If you run inference locally, every token costs something. Time, electricity, VRAM bandwidth. Reasoning tokens are overhead. They do not appear in your final output. They are pure compute spent on internal deliberation.

Without a cap, a hybrid model can spend 80 percent of its generation budget on thinking. That is fine for research. It is not fine for a chatbot serving real users.

The sampler-level approach is the right place to enforce this. It happens after the model outputs logits but before the next token is chosen. The model still "thinks" up to the budget. It still produces coherent reasoning chains. But it cannot wander. The cap is deterministic. Set it to 128 tokens and the reasoning block will never exceed 128 tokens.

This is different from prompt-level approaches like "be concise" or "limit reasoning to 100 tokens." Those are suggestions. The model can ignore them. The sampler cannot be ignored. It overwrites logits.

Practical Tuning Guidance

There is no universal right number. The correct budget depends on your task, your model, and your hardware.

Here is a starting framework:

Use --reasoning-budget 0 when:

  • You need maximum speed
  • The task is trivial (summarization, simple extraction)
  • You are running on CPU or edge hardware
  • The model supports disabling reasoning cleanly via kwargs

Use --reasoning-budget 32-64 when:

  • The task needs a little scratch-pad work (arithmetic, short logic chains)
  • You want some reasoning but cannot afford long chains
  • You are serving multiple concurrent users

Use --reasoning-budget 128-256 when:

  • The task is complex (coding, multi-step planning)
  • You have GPU headroom
  • Latency is important but not critical

Use --reasoning-budget 512+ when:

  • You are doing research or analysis where deep reasoning pays off
  • You have ample compute
  • You want the model's full capability

Always test with your actual workload. A coding assistant might need 256 tokens of reasoning for hard bugs but only 64 for syntax fixes. Start low and increase until quality plateaus.

The --reasoning-budget-message flag is worth experimenting with too. A message like "That is enough thinking, give the answer now" can help the model recover gracefully from an interrupted chain. Without it, the model may produce a jarring transition from mid-thought to final answer.

A Word on Multiple Reasoning Blocks

The re-arm behavior is subtle but important. Some models emit <think>, reason, close, then later open a second <think> block for follow-up reasoning. The sampler handles each block independently with its own budget. This is correct behavior.

If you set a global budget and the model uses two reasoning blocks, each gets the full budget. This is not a bug. The sampler budgets per block, not per response. If you need a global cap, you would need to track total reasoning tokens at the application layer.

Current Status and Caveats

The feature merged in March 2026 and has been refined through April. It is in the master branch and available in the CLI, server, and API.

There is one open issue (#22717, May 2026) where users report --reasoning-budget appearing to stop working on certain builds. The root cause is still under investigation, so if you upgrade and the cap seems ignored, check your build date and the issue tracker.

Also note that the sampler does not rewrite the model's weights. It does not train the model to think faster. It is a runtime guardrail. The model will still try to reason. It just gets cut off when the budget expires. For models heavily trained on reasoning, a very low budget can sometimes produce odd transitions. Test before deploying.

Bottom Line

PR #20297 turns --reasoning-budget from a do-nothing stub into a real production control. It gives self-hosters a deterministic way to cap reasoning overhead without rewriting prompts or retraining models.

If you run Qwen 3.5, DeepSeek, or any hybrid reasoning model under llama.cpp, this flag belongs in your toolkit. Start with a budget, measure your latency and quality, and adjust. The days of helplessly watching a model think itself into a 500-token hole are over.

Citations

Fact-Check Notes

  • The merge date for PR #20297 is March 11, 2026, not May 2026. The brief said May, but GitHub API shows merged_at: 2026-03-11T09:26:12Z.
  • The "delayed launch" mechanism described in the PR body was refined in later commits. The final implementation uses the state machine described in reasoning-budget.h, not an exact "delayed grammar" approach.
  • Issue #22717 is open and unconfirmed as of May 2026. Readers should verify their build if they encounter problems.
  • The --reasoning-budget-message parameter was later refactored into sampling params. Current CLI syntax may differ from the original PR.