bots-garden/razorbillpublic Fork
⑂ v1.0.0
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/razorbill.git
git clone ssh://git@rickub.com/bots-garden/razorbill.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
thinking.md · 55 lines · 4.8 KBmarkdown
Blame HistoryOpen raw

Reasoning models — explanation

What is this about?

Some models (Qwen3, Qwen3.5, DeepSeek-R1…) write a chain of reasoning before their answer, between <think> and </think>. razorbill has to do three things with it: separate it from the answer (OpenAI clients expect content to be the answer only), decide whether to send it, and, if asked, stop it before it eats the whole token budget. This page explains how, and why it is done that way.

Turning it on is the model's business

Whether a hybrid model thinks is decided by its chat template, the Jinja program shipped with the model that turns the conversation into the prompt. razorbill passes enable_thinking to the template — from the request (reasoning_effort, chat_template_kwargs.enable_thinking) or else the configuration's thinking — and lets it do the rest. Models that are not reasoning models simply ignore the flag. Thinking is off unless a request or the configuration turns it on: most API clients expect fast, direct answers.

Separating reasoning from the answer

The engine receives the output in small pieces, so a tag can arrive cut in two (<thi + nk>). The ThinkingSplitter keeps back any piece that could be the start of a tag until the next piece arrives, then emits reasoning events and text events. The server turns them into reasoning_content and content, in whole answers and in stream deltas alike.

When the template opens the tag itself

Qwen3 writes <think> itself at the start of its output. Qwen3.5, and DeepSeek-R1-style templates, write it at the end of the prompt instead:

<|im_start|>assistant
<think>

The model's output then starts inside the reasoning, and only </think> appears. A splitter waiting for <think> would print the whole reasoning as the answer — which is what happened in the first version (ticket #24). The MLX engine therefore renders the model's template once when a session opens, with the same settings, and checks whether the prompt ends with an unclosed <think>. If it does, the splitter starts in reasoning mode.

Asking the template, rather than keeping a list of model names, means a new model with the same convention works without a change.

Sending it

Reasoning is useful to understand an answer and noise for a program. Sending it is therefore a separate setting from thinking: include_reasoning: false in a request, or show_reasoning: false in the configuration (in locked, for every client). The model still thinks; only reasoning_content is left out. usage.completion_tokens_details.reasoning_tokens still counts it, because those tokens took time and count in max_tokens.

A model told not to think often still emits an empty <think></think>; empty reasoning is never sent.

Limiting it

Small models can think for hundreds or thousands of tokens, and the reasoning counts inside max_tokens. thinking_budget caps it. razorbill uses mlx-swift-lm's own mechanism rather than cutting the text afterwards: a logit processor counts reasoning tokens during generation and, when the budget is spent, forces the closing sequence through the normal generation loop. The forced tokens are part of the model's context, so it continues with an answer that follows its own (truncated) reasoning, and the cache and history stay consistent.

How to close the reasoning depends on the family:

  • Qwen3 declares a transition, published by Qwen: "Considering the limited time by the user, I have to give the solution based on the thinking directly now." followed by </think>. It is used as is.
  • Families without a declared transition (Qwen3.5, Qwen3-Next…) are closed with </think> alone, the conservative choice recommended by mlx-swift-lm.
  • Models with no known reasoning protocol (Llama, Gemma) cannot be limited: razorbill refuses the setting with an error rather than ignoring it, because a silently ignored limit is worse than none.

Rejected alternatives

  • Detecting Qwen3.5 by its name: fragile; the template is the source of truth.
  • Cutting the text after N reasoning tokens and stopping generation: the model would never get to answer.
  • Raising max_tokens automatically when a budget is set: hidden behaviour; the configuration check asks for a budget smaller than max_tokens instead.
  • Sending the reasoning inside content, between <think> tags: some servers do; clients then show or parse the tags as part of the answer.

How it relates to the rest

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Reasoning models — explanation

## What is this about?

Some models (Qwen3, Qwen3.5, DeepSeek-R1…) write a chain of reasoning before their answer, between `<think>` and `</think>`. razorbill has to do three things with it: separate it from the answer (OpenAI clients expect `content` to be the answer only), decide whether to send it, and, if asked, stop it before it eats the whole token budget. This page explains how, and why it is done that way.

## Turning it on is the model's business

Whether a hybrid model thinks is decided by its **chat template**, the Jinja program shipped with the model that turns the conversation into the prompt. razorbill passes `enable_thinking` to the template — from the request (`reasoning_effort`, `chat_template_kwargs.enable_thinking`) or else the configuration's `thinking` — and lets it do the rest. Models that are not reasoning models simply ignore the flag. Thinking is off unless a request or the configuration turns it on: most API clients expect fast, direct answers.

## Separating reasoning from the answer

The engine receives the output in small pieces, so a tag can arrive cut in two (`<thi` + `nk>`). The `ThinkingSplitter` keeps back any piece that could be the start of a tag until the next piece arrives, then emits *reasoning* events and *text* events. The server turns them into `reasoning_content` and `content`, in whole answers and in stream deltas alike.

### When the template opens the tag itself

Qwen3 writes `<think>` itself at the start of its output. Qwen3.5, and DeepSeek-R1-style templates, write it **at the end of the prompt** instead:

```
<|im_start|>assistant
<think>
```

The model's output then starts inside the reasoning, and only `</think>` appears. A splitter waiting for `<think>` would print the whole reasoning as the answer — which is what happened in the first version (ticket #24). The MLX engine therefore renders the model's template once when a session opens, with the same settings, and checks whether the prompt ends with an unclosed `<think>`. If it does, the splitter starts in reasoning mode.

Asking the template, rather than keeping a list of model names, means a new model with the same convention works without a change.

## Sending it

Reasoning is useful to understand an answer and noise for a program. Sending it is therefore a separate setting from thinking: `include_reasoning: false` in a request, or `show_reasoning: false` in the configuration (in `locked`, for every client). The model still thinks; only `reasoning_content` is left out. `usage.completion_tokens_details.reasoning_tokens` still counts it, because those tokens took time and count in `max_tokens`.

A model told not to think often still emits an empty `<think></think>`; empty reasoning is never sent.

## Limiting it

Small models can think for hundreds or thousands of tokens, and the reasoning counts inside `max_tokens`. `thinking_budget` caps it. razorbill uses mlx-swift-lm's own mechanism rather than cutting the text afterwards: a logit processor counts reasoning tokens during generation and, when the budget is spent, forces the closing sequence **through the normal generation loop**. The forced tokens are part of the model's context, so it continues with an answer that follows its own (truncated) reasoning, and the cache and history stay consistent.

How to close the reasoning depends on the family:

- Qwen3 declares a transition, published by Qwen: *"Considering the limited time by the user, I have to give the solution based on the thinking directly now."* followed by `</think>`. It is used as is.
- Families without a declared transition (Qwen3.5, Qwen3-Next…) are closed with `</think>` alone, the conservative choice recommended by mlx-swift-lm.
- Models with no known reasoning protocol (Llama, Gemma) cannot be limited: razorbill refuses the setting with an error rather than ignoring it, because a silently ignored limit is worse than none.

## Rejected alternatives

- **Detecting Qwen3.5 by its name**: fragile; the template is the source of truth.
- **Cutting the text after `N` reasoning tokens and stopping generation**: the model would never get to answer.
- **Raising `max_tokens` automatically when a budget is set**: hidden behaviour; the configuration check asks for a budget smaller than `max_tokens` instead.
- **Sending the reasoning inside `content`, between `<think>` tags**: some servers do; clients then show or parse the tags as part of the answer.

## How it relates to the rest

- The settings: [control reasoning](../how-to/configure-thinking.md), the [HTTP API reference](../reference/http-api.md#request-fields) and the [configuration reference](../reference/configuration.md#defaults-and-locked).
- Reasoning before structured output: [constrained decoding](constrained-decoding.md).
- Where the engine sits: [architecture](architecture.md).