opensourcellmrouter 0.6.0

A fast, local-first LLM router — proxy any OpenAI/Anthropic/Ollama client to your own provider pipeline with classifiers, cost/latency/random routing rules, plugins, a live dashboard, and a TUI.
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Wire types for the Anthropic `/v1/messages` shape, and conversions
//! to/from the [`canonical`](crate::canonical) representation.
//!
//! These types are used both when the router receives a Claude-shaped
//! request from a client, and when it forwards a request to a provider that
//! itself speaks the Anthropic Messages API.

use serde::{Deserialize, Serialize};

use crate::canonical::{ChatRequest, ChatResponse, Message, PluginRequest, Role, StopReason, Usage};

/// `content` may be a plain string or a list of content blocks. Anthropic
/// accepts both shapes on input; only blocks are accepted on output, but we
/// keep this untagged so a single type covers requests in either shape.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum AnthropicContent {
    Text(String),
    Blocks(Vec<ContentBlock>),
}

impl AnthropicContent {
    pub fn into_text(self) -> String {
        match self {
            AnthropicContent::Text(text) => text,
            AnthropicContent::Blocks(blocks) => blocks
                .into_iter()
                .filter(|b| b.block_type == "text")
                .map(|b| b.text)
                .collect::<Vec<_>>()
                .join(""),
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ContentBlock {
    #[serde(rename = "type")]
    pub block_type: String,
    #[serde(default)]
    pub text: String,
}

impl ContentBlock {
    pub fn text(text: impl Into<String>) -> Self {
        ContentBlock {
            block_type: "text".to_string(),
            text: text.into(),
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AnthropicMessage {
    pub role: String,
    pub content: AnthropicContent,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AnthropicMessagesRequest {
    pub model: String,
    pub max_tokens: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system: Option<String>,
    pub messages: Vec<AnthropicMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    /// Extended-thinking config, e.g. `{"type": "adaptive"}`. Passed through
    /// opaquely — the router doesn't interpret it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_config: Option<OutputConfig>,
    #[serde(default)]
    pub stream: bool,
    /// Plugins to run for this request, e.g. `[{"id": "response-healing"}]`.
    /// Not part of the standard Anthropic API; stripped before forwarding.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub plugins: Vec<PluginRequest>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct OutputConfig {
    /// `"low"`, `"medium"`, `"high"`, `"xhigh"`, or `"max"`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effort: Option<String>,
    /// e.g. `{"type": "tokens", "total": 64000}`. Requires the
    /// `task-budgets-2026-03-13` beta header — see
    /// `provider::anthropic_beta_header`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_budget: Option<serde_json::Value>,
    /// Structured-outputs config: `{"type": "json_schema", "schema": {...}}`.
    /// See <https://platform.claude.com/docs/en/build-with-claude/structured-outputs>.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<OutputFormat>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct OutputFormat {
    #[serde(rename = "type")]
    pub format_type: String,
    pub schema: serde_json::Value,
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct AnthropicUsage {
    #[serde(default)]
    pub input_tokens: u32,
    #[serde(default)]
    pub output_tokens: u32,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AnthropicMessagesResponse {
    pub id: String,
    #[serde(rename = "type")]
    pub response_type: String,
    pub role: String,
    pub model: String,
    pub content: Vec<ContentBlock>,
    pub stop_reason: Option<String>,
    #[serde(default)]
    pub usage: AnthropicUsage,
}

/// Fallback `max_tokens` for requests where the client (or our own
/// translation from an OpenAI-shaped request) didn't specify one, since
/// Anthropic's API requires the field.
const DEFAULT_MAX_TOKENS: u32 = 4096;

/// An inbound request from a client speaking the Anthropic format.
impl From<AnthropicMessagesRequest> for ChatRequest {
    fn from(req: AnthropicMessagesRequest) -> Self {
        let messages = req
            .messages
            .into_iter()
            .map(|msg| Message {
                role: match msg.role.as_str() {
                    "assistant" => Role::Assistant,
                    _ => Role::User,
                },
                content: msg.content.into_text(),
            })
            .collect();

        let (effort, task_budget, output_schema) = match req.output_config {
            Some(c) => (c.effort, c.task_budget, c.format.map(|f| f.schema)),
            None => (None, None, None),
        };

        ChatRequest {
            model: req.model,
            system: req.system,
            messages,
            max_tokens: Some(req.max_tokens),
            temperature: req.temperature,
            thinking: req.thinking,
            effort,
            task_budget,
            output_schema,
            stream: req.stream,
            plugins: req.plugins,
            forced_provider: None,
            tags: Vec::new(),
        }
    }
}

/// An outbound request to a provider that speaks the Anthropic format.
impl From<&ChatRequest> for AnthropicMessagesRequest {
    fn from(req: &ChatRequest) -> Self {
        let messages = req
            .messages
            .iter()
            .map(|msg| AnthropicMessage {
                role: match msg.role {
                    Role::Assistant => "assistant".to_string(),
                    Role::User => "user".to_string(),
                },
                content: AnthropicContent::Text(msg.content.clone()),
            })
            .collect();

        let output_config = if req.effort.is_some() || req.task_budget.is_some() || req.output_schema.is_some() {
            Some(OutputConfig {
                effort: req.effort.clone(),
                task_budget: req.task_budget.clone(),
                format: req.output_schema.clone().map(|schema| OutputFormat {
                    format_type: "json_schema".to_string(),
                    schema,
                }),
            })
        } else {
            None
        };

        AnthropicMessagesRequest {
            model: req.model.clone(),
            max_tokens: req.max_tokens.unwrap_or(DEFAULT_MAX_TOKENS),
            system: req.system.clone(),
            messages,
            temperature: req.temperature,
            thinking: req.thinking.clone(),
            output_config,
            stream: false,
            plugins: Vec::new(),
        }
    }
}

/// A reply from a provider that speaks the Anthropic format.
impl From<AnthropicMessagesResponse> for ChatResponse {
    fn from(resp: AnthropicMessagesResponse) -> Self {
        let content = resp
            .content
            .into_iter()
            .filter(|b| b.block_type == "text")
            .map(|b| b.text)
            .collect::<Vec<_>>()
            .join("");

        let stop_reason = match resp.stop_reason.as_deref() {
            Some("end_turn") => StopReason::EndTurn,
            Some("max_tokens") => StopReason::MaxTokens,
            _ => StopReason::Other,
        };

        ChatResponse {
            id: resp.id,
            model: resp.model,
            content,
            stop_reason,
            usage: Usage {
                input_tokens: resp.usage.input_tokens,
                output_tokens: resp.usage.output_tokens,
            },
            tags: Vec::new(),
        }
    }
}

/// A reply rendered for a client that speaks the Anthropic format.
impl From<ChatResponse> for AnthropicMessagesResponse {
    fn from(resp: ChatResponse) -> Self {
        let stop_reason = match resp.stop_reason {
            StopReason::EndTurn => "end_turn",
            StopReason::MaxTokens => "max_tokens",
            StopReason::Other => "end_turn",
        };

        AnthropicMessagesResponse {
            id: resp.id,
            response_type: "message".to_string(),
            role: "assistant".to_string(),
            model: resp.model,
            content: vec![ContentBlock::text(resp.content)],
            stop_reason: Some(stop_reason.to_string()),
            usage: AnthropicUsage {
                input_tokens: resp.usage.input_tokens,
                output_tokens: resp.usage.output_tokens,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn anthropic_request(
        thinking: Option<serde_json::Value>,
        effort: Option<&str>,
        task_budget: Option<serde_json::Value>,
        output_schema: Option<serde_json::Value>,
    ) -> AnthropicMessagesRequest {
        let output_config = if effort.is_some() || task_budget.is_some() || output_schema.is_some() {
            Some(OutputConfig {
                effort: effort.map(str::to_string),
                task_budget,
                format: output_schema.map(|schema| OutputFormat {
                    format_type: "json_schema".to_string(),
                    schema,
                }),
            })
        } else {
            None
        };

        AnthropicMessagesRequest {
            model: "claude-opus-4-8".to_string(),
            max_tokens: 1024,
            system: None,
            messages: vec![AnthropicMessage {
                role: "user".to_string(),
                content: AnthropicContent::Text("hi".to_string()),
            }],
            temperature: None,
            thinking,
            output_config,
            stream: false,
            plugins: Vec::new(),
        }
    }

    fn chat_request(
        thinking: Option<serde_json::Value>,
        effort: Option<&str>,
        task_budget: Option<serde_json::Value>,
        output_schema: Option<serde_json::Value>,
    ) -> ChatRequest {
        ChatRequest {
            model: "claude-opus-4-8".to_string(),
            system: None,
            messages: vec![Message {
                role: Role::User,
                content: "hi".to_string(),
            }],
            max_tokens: Some(1024),
            temperature: None,
            thinking,
            effort: effort.map(str::to_string),
            task_budget,
            output_schema,
            stream: false,
            plugins: Vec::new(),
            forced_provider: None,
            tags: Vec::new(),
        }
    }

    #[test]
    fn inbound_thinking_and_effort_map_to_chat_request() {
        let req = anthropic_request(Some(json!({"type": "adaptive"})), Some("xhigh"), None, None);
        let chat: ChatRequest = req.into();
        assert_eq!(chat.thinking, Some(json!({"type": "adaptive"})));
        assert_eq!(chat.effort, Some("xhigh".to_string()));
        assert_eq!(chat.task_budget, None);
    }

    #[test]
    fn inbound_without_thinking_or_effort_leaves_both_none() {
        let req = anthropic_request(None, None, None, None);
        let chat: ChatRequest = req.into();
        assert_eq!(chat.thinking, None);
        assert_eq!(chat.effort, None);
        assert_eq!(chat.task_budget, None);
        assert_eq!(chat.output_schema, None);
    }

    #[test]
    fn inbound_task_budget_maps_to_chat_request_without_effort() {
        let budget = json!({"type": "tokens", "total": 64000});
        let req = anthropic_request(None, None, Some(budget.clone()), None);
        let chat: ChatRequest = req.into();
        assert_eq!(chat.task_budget, Some(budget));
        assert_eq!(chat.effort, None);
    }

    #[test]
    fn inbound_output_schema_extracted_from_json_schema_format() {
        let schema = json!({"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"], "additionalProperties": false});
        let req = anthropic_request(None, None, None, Some(schema.clone()));
        let chat: ChatRequest = req.into();
        assert_eq!(chat.output_schema, Some(schema));
    }

    #[test]
    fn outbound_thinking_and_effort_forwarded_under_output_config() {
        let chat = chat_request(Some(json!({"type": "adaptive"})), Some("high"), None, None);
        let req = AnthropicMessagesRequest::from(&chat);
        assert_eq!(req.thinking, Some(json!({"type": "adaptive"})));
        assert_eq!(req.output_config.unwrap().effort, Some("high".to_string()));
    }

    #[test]
    fn outbound_output_schema_wrapped_as_json_schema_format() {
        let schema = json!({"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"], "additionalProperties": false});
        let chat = chat_request(None, None, None, Some(schema.clone()));
        let req = AnthropicMessagesRequest::from(&chat);
        let format = req.output_config.unwrap().format.unwrap();
        assert_eq!(format.format_type, "json_schema");
        assert_eq!(format.schema, schema);
    }

    #[test]
    fn outbound_without_effort_or_task_budget_omits_output_config() {
        let chat = chat_request(None, None, None, None);
        let req = AnthropicMessagesRequest::from(&chat);
        assert_eq!(req.thinking, None);
        assert!(req.output_config.is_none());
    }

    #[test]
    fn outbound_task_budget_alone_still_creates_output_config() {
        let budget = json!({"type": "tokens", "total": 64000});
        let chat = chat_request(None, None, Some(budget.clone()), None);
        let req = AnthropicMessagesRequest::from(&chat);
        let output_config = req.output_config.unwrap();
        assert_eq!(output_config.task_budget, Some(budget));
        assert_eq!(output_config.effort, None);
    }

    #[test]
    fn outbound_request_serializes_effort_task_budget_and_format_under_output_config_key() {
        let budget = json!({"type": "tokens", "total": 64000});
        let schema = json!({"type": "object", "properties": {}, "additionalProperties": false});
        let chat = chat_request(Some(json!({"type": "adaptive"})), Some("high"), Some(budget.clone()), Some(schema.clone()));
        let req = AnthropicMessagesRequest::from(&chat);
        let value = serde_json::to_value(&req).unwrap();
        assert_eq!(value["thinking"], json!({"type": "adaptive"}));
        assert_eq!(
            value["output_config"],
            json!({"effort": "high", "task_budget": budget, "format": {"type": "json_schema", "schema": schema}})
        );
    }
}