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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Reasoning/thinking configuration types.
/// How a model expresses reasoning/thinking capability.
///
/// Each model supports one or more reasoning modes.
/// The Protocol layer uses this to decide which fields to include in the request.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReasoningMode {
/// No reasoning support (don't send any reasoning fields)
None,
/// OpenAI-style effort parameter (`reasoning_effort: "low"|"medium"|"high"`)
Effort,
/// Anthropic-style thinking block (`thinking: {type: "enabled", budget_tokens: N}`)
Thinking,
/// Both modes supported (protocol decides which to use)
Both,
}
/// Resolved reasoning specification — what to actually send to the API.
///
/// Derived from `ReasoningConfig` (user intent) + `ReasoningMode` (model capability).
#[derive(Debug, Clone)]
pub enum ReasoningSpec {
/// Don't send any reasoning fields
None,
/// Send OpenAI-style effort parameter
Effort(ReasoningEffort),
/// Send Anthropic-style thinking block
Thinking { budget_tokens: u64 },
}
/// Reasoning/thinking configuration, unifying reasoning/thinking parameters across vendors.
#[derive(Debug, Clone, Default)]
pub struct ReasoningConfig {
/// Whether to enable reasoning/thinking process
pub enabled: Option<bool>,
/// Thinking budget (token count limit)
pub budget_tokens: Option<u64>,
/// Reasoning intensity/depth (semantics vary by vendor)
pub effort: Option<ReasoningEffort>,
}
/// Reasoning intensity/depth enumeration.
#[derive(Debug, Clone, Default)]
pub enum ReasoningEffort {
#[default]
None,
Low,
Medium,
High,
XHigh,
}
impl ReasoningConfig {
/// Convert user intent (ReasoningConfig) + model capability (ReasoningMode)
/// into a concrete ReasoningSpec to send to the API.
pub fn to_spec(&self, mode: ReasoningMode) -> ReasoningSpec {
// If explicitly disabled, skip reasoning regardless of mode
if self.enabled == Some(false) {
return ReasoningSpec::None;
}
match mode {
ReasoningMode::None => ReasoningSpec::None,
ReasoningMode::Effort => {
// Only accept Effort-type parameters
self.effort
.as_ref()
.filter(|e| !matches!(e, ReasoningEffort::None))
.map(|e| ReasoningSpec::Effort(e.clone()))
.unwrap_or(ReasoningSpec::None)
}
ReasoningMode::Thinking => {
// Only accept Thinking-type parameters
ReasoningSpec::Thinking {
budget_tokens: self.budget_tokens.unwrap_or(2048),
}
}
ReasoningMode::Both => {
// Prefer Thinking over Effort.
// Thinking is more expressive; Anthropic only handles Thinking.
// If budget_tokens is provided, use Thinking; otherwise fall back to Effort.
if let Some(budget_tokens) = self.budget_tokens {
ReasoningSpec::Thinking { budget_tokens }
} else if let Some(effort) = &self.effort {
if !matches!(effort, ReasoningEffort::None) {
return ReasoningSpec::Effort(effort.clone());
}
ReasoningSpec::Thinking {
budget_tokens: 2048,
}
} else {
ReasoningSpec::Thinking {
budget_tokens: 2048,
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn to_spec_none_mode() {
let config = ReasoningConfig {
effort: Some(ReasoningEffort::High),
budget_tokens: Some(4096),
..Default::default()
};
assert!(matches!(
config.to_spec(ReasoningMode::None),
ReasoningSpec::None
));
}
#[test]
fn to_spec_effort_mode_with_effort() {
let config = ReasoningConfig {
effort: Some(ReasoningEffort::Medium),
..Default::default()
};
assert!(matches!(
config.to_spec(ReasoningMode::Effort),
ReasoningSpec::Effort(ReasoningEffort::Medium)
));
}
#[test]
fn to_spec_effort_mode_without_effort() {
let config = ReasoningConfig {
effort: None,
budget_tokens: Some(4096),
..Default::default()
};
assert!(matches!(
config.to_spec(ReasoningMode::Effort),
ReasoningSpec::None
));
}
#[test]
fn to_spec_thinking_mode() {
let config = ReasoningConfig {
budget_tokens: Some(4096),
..Default::default()
};
assert!(matches!(
config.to_spec(ReasoningMode::Thinking),
ReasoningSpec::Thinking {
budget_tokens: 4096
}
));
}
#[test]
fn to_spec_thinking_mode_default_budget() {
let config = ReasoningConfig::default();
assert!(matches!(
config.to_spec(ReasoningMode::Thinking),
ReasoningSpec::Thinking {
budget_tokens: 2048
}
));
}
#[test]
fn to_spec_both_effort_priority() {
let config = ReasoningConfig {
effort: Some(ReasoningEffort::High),
budget_tokens: Some(4096),
..Default::default()
};
// Both mode should prefer Thinking over Effort,
// because Thinking is more expressive and Effort can be derived from it.
// Anthropic only handles Thinking, so preferring Effort would silently drop reasoning.
assert!(matches!(
config.to_spec(ReasoningMode::Both),
ReasoningSpec::Thinking {
budget_tokens: 4096
}
));
}
#[test]
fn to_spec_both_fallback_to_thinking() {
let config = ReasoningConfig {
effort: None,
budget_tokens: Some(4096),
..Default::default()
};
assert!(matches!(
config.to_spec(ReasoningMode::Both),
ReasoningSpec::Thinking {
budget_tokens: 4096
}
));
}
#[test]
fn to_spec_both_effort_none_fallback() {
let config = ReasoningConfig {
effort: Some(ReasoningEffort::None),
budget_tokens: Some(4096),
..Default::default()
};
// Effort::None should be treated as "no effort", fallback to thinking
assert!(matches!(
config.to_spec(ReasoningMode::Both),
ReasoningSpec::Thinking {
budget_tokens: 4096
}
));
}
#[test]
fn to_spec_enabled_false_disables_reasoning() {
// ReasoningConfig.enabled=false now disables reasoning.
let config = ReasoningConfig {
enabled: Some(false),
effort: Some(ReasoningEffort::High),
..Default::default()
};
let spec = config.to_spec(ReasoningMode::Effort);
assert!(
matches!(spec, ReasoningSpec::None),
"enabled=false should disable reasoning"
);
let config2 = ReasoningConfig {
enabled: Some(false),
budget_tokens: Some(4096),
..Default::default()
};
let spec2 = config2.to_spec(ReasoningMode::Thinking);
assert!(
matches!(spec2, ReasoningSpec::None),
"enabled=false should disable thinking"
);
}
// ── proptest: ReasoningConfig::to_spec ──
mod proptest_tests {
use super::*;
use proptest::prelude::*;
proptest! {
#[test]
fn to_spec_never_panics(
enabled in proptest::option::of(proptest::bool::ANY),
budget_tokens in proptest::option::of(0u64..100_000),
effort_idx in 0usize..5,
mode_idx in 0usize..4,
) {
let effort = match effort_idx {
0 => None,
1 => Some(ReasoningEffort::None),
2 => Some(ReasoningEffort::Low),
3 => Some(ReasoningEffort::Medium),
4 => Some(ReasoningEffort::High),
_ => unreachable!(),
};
let mode = match mode_idx {
0 => ReasoningMode::None,
1 => ReasoningMode::Effort,
2 => ReasoningMode::Thinking,
3 => ReasoningMode::Both,
_ => unreachable!(),
};
let config = ReasoningConfig { enabled, budget_tokens, effort };
let _spec = config.to_spec(mode);
}
#[test]
fn none_mode_always_returns_none(
enabled in proptest::option::of(proptest::bool::ANY),
budget_tokens in proptest::option::of(0u64..100_000),
effort_idx in 0usize..5,
) {
let effort = match effort_idx {
0 => None,
1 => Some(ReasoningEffort::None),
2 => Some(ReasoningEffort::Low),
3 => Some(ReasoningEffort::Medium),
4 => Some(ReasoningEffort::High),
_ => unreachable!(),
};
let config = ReasoningConfig { enabled, budget_tokens, effort };
let spec = config.to_spec(ReasoningMode::None);
assert!(matches!(spec, ReasoningSpec::None),
"Mode::None should always produce Spec::None, got {:?}", spec);
}
#[test]
fn enabled_false_always_returns_none(
budget_tokens in proptest::option::of(0u64..100_000),
effort_idx in 0usize..5,
mode_idx in 0usize..4,
) {
let effort = match effort_idx {
0 => None,
1 => Some(ReasoningEffort::None),
2 => Some(ReasoningEffort::Low),
3 => Some(ReasoningEffort::Medium),
4 => Some(ReasoningEffort::High),
_ => unreachable!(),
};
let mode = match mode_idx {
0 => ReasoningMode::None,
1 => ReasoningMode::Effort,
2 => ReasoningMode::Thinking,
3 => ReasoningMode::Both,
_ => unreachable!(),
};
let config = ReasoningConfig { enabled: Some(false), budget_tokens, effort };
let spec = config.to_spec(mode);
assert!(matches!(spec, ReasoningSpec::None),
"enabled=false should disable reasoning for any mode, got {:?}", spec);
}
}
}
}