litellm-rs 0.8.2

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
Documentation
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! Anthropic request transformation.

use serde_json::{Value, json};

use crate::core::providers::anthropic::error::anthropic_api_error;
use crate::core::providers::anthropic::models::{ModelFeature, get_anthropic_registry};
use crate::core::providers::unified_provider::ProviderError;
#[cfg(any(feature = "gateway", test))]
use crate::core::providers::{
    AnthropicContentBlockOrder, ChatContinuationRequest, ChatContinuationResponse,
    ChatMessageContinuation,
};
#[cfg(any(feature = "gateway", test))]
use crate::core::types::anthropic_continuation::AnthropicThinkingBlock;
use crate::core::types::chat::{ChatMessage, ChatRequest};
use crate::core::types::content::ContentPart;
use crate::core::types::message::{MessageContent, MessageRole};
use crate::core::types::thinking::ThinkingEffort;

use super::AnthropicClient;

#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum AnthropicEffort {
    Low,
    Medium,
    High,
    XHigh,
    Max,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Claude5Model {
    Fable,
    Opus,
    Sonnet,
}

impl AnthropicEffort {
    pub(super) fn as_str(self) -> &'static str {
        match self {
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
            Self::XHigh => "xhigh",
            Self::Max => "max",
        }
    }
}

impl From<ThinkingEffort> for AnthropicEffort {
    fn from(value: ThinkingEffort) -> Self {
        match value {
            ThinkingEffort::Low => Self::Low,
            ThinkingEffort::Medium => Self::Medium,
            ThinkingEffort::High => Self::High,
        }
    }
}

impl AnthropicClient {
    #[cfg(any(feature = "gateway", test))]
    pub(crate) async fn chat_with_continuation(
        &self,
        envelope: ChatContinuationRequest,
    ) -> Result<ChatContinuationResponse, ProviderError> {
        let (request, extensions) = envelope.into_parts();
        let tool_name_map = self.anthropic_tool_name_map_for_request(&request)?;
        let body = self.transform_chat_request_with_extensions(&request, &extensions)?;
        let mut headers = self.get_request_headers();
        headers.extend(self.compute_beta_headers_with_extensions(&request, &extensions));
        let response = self.send_request("/v1/messages", body, headers).await?;
        self.transform_chat_response_with_continuation(response, &tool_name_map)
    }

    pub(crate) fn transform_chat_request(
        &self,
        request: &ChatRequest,
    ) -> Result<Value, ProviderError> {
        if self.config.uses_compatible_model_allow_list()
            && !self.config.allows_unknown_model(&request.model)
        {
            return Err(anthropic_api_error(
                400,
                format!("Unsupported model: {}", request.model),
            ));
        }

        let registry = get_anthropic_registry();
        let model_spec = if self.config.uses_compatible_model_allow_list() {
            None
        } else {
            registry.get_model_spec(&request.model)
        };
        let claude_5 = !self.config.uses_compatible_model_allow_list()
            && Self::claude_5_model(&request.model).is_some();
        if model_spec.is_none() && !claude_5 && !self.config.allows_unknown_model(&request.model) {
            return Err(anthropic_api_error(
                400,
                format!("Unsupported model: {}", request.model),
            ));
        }
        if claude_5 {
            Self::validate_claude_5_legacy_functions(request)?;
            Self::validate_claude_5_request_shape(request)?;
        } else if request.reasoning_effort.is_some() {
            return Err(ProviderError::not_supported(
                "anthropic",
                format!(
                    "Model {} does not support reasoning_effort; exact Claude 5 models are required",
                    request.model
                ),
            ));
        }
        if model_spec.is_none()
            && !claude_5
            && (request
                .tools
                .as_ref()
                .is_some_and(|tools| !tools.is_empty())
                || Self::has_anthropic_tools_extra_param(request)
                || request.functions.as_ref().is_some_and(|f| !f.is_empty())
                || request.function_call.is_some())
        {
            return Err(ProviderError::not_supported(
                "anthropic",
                format!(
                    "Unknown model {} cannot declare tool calling support",
                    request.model
                ),
            ));
        }
        if model_spec.is_none() && !claude_5 && Self::has_unsupported_unknown_model_content(request)
        {
            return Err(ProviderError::not_supported(
                "anthropic",
                format!(
                    "Unknown model {} only supports text and image content",
                    request.model
                ),
            ));
        }
        if model_spec.is_none()
            && !claude_5
            && Self::has_image_content(request)
            && !self.config.allows_unknown_model_image_input(&request.model)
        {
            return Err(ProviderError::not_supported(
                "anthropic",
                format!(
                    "Unknown model {} does not support image input",
                    request.model
                ),
            ));
        }

        if let Some(n) = request.n
            && n != 1
        {
            return Err(ProviderError::invalid_request(
                "anthropic",
                format!("anthropic only supports n=1 (got n={})", n),
            ));
        }

        let mut ignored_params = Vec::new();
        if request.frequency_penalty.is_some() {
            ignored_params.push("frequency_penalty");
        }
        if request.presence_penalty.is_some() {
            ignored_params.push("presence_penalty");
        }
        if request.seed.is_some() {
            ignored_params.push("seed");
        }
        if request.logit_bias.is_some() {
            ignored_params.push("logit_bias");
        }
        if !ignored_params.is_empty() {
            tracing::warn!(
                "Anthropic request ignores unsupported parameters: {}",
                ignored_params.join(", ")
            );
        }

        let (system_message, messages) = self.separate_system_messages(&request.messages)?;
        let tool_name_map = self.anthropic_tool_name_map_for_request(request)?;

        let anthropic_messages =
            self.transform_messages(messages, &request.model, model_spec, &tool_name_map)?;

        let mut anthropic_request = json!({
            "model": request.model,
            "max_tokens": request.max_tokens.unwrap_or(4096),
            "messages": anthropic_messages,
        });

        if let Some(system) = system_message {
            anthropic_request["system"] = json!(system);
        }

        if let Some(temperature) = request.temperature {
            anthropic_request["temperature"] = json!(temperature);
        }

        if let Some(top_p) = request.top_p {
            anthropic_request["top_p"] = json!(top_p);
        }

        if let Some(user) = &request.user {
            anthropic_request["metadata"] = json!({ "user_id": user });
        }

        if self.config.enable_cache_control
            && let Some(cache_control) = request.extra_params.get("cache_control")
        {
            Self::ensure_cache_control_supported(&request.model, model_spec)?;
            anthropic_request["cache_control"] = cache_control.clone();
        }

        if let Some(stop) = &request.stop {
            anthropic_request["stop_sequences"] = json!(stop);
        }

        if let Some(tools) = &request.tools
            && !tools.is_empty()
        {
            if model_spec.is_none() && !claude_5 {
                return Err(ProviderError::not_supported(
                    "anthropic",
                    format!(
                        "Unknown model {} cannot declare tool calling support",
                        request.model
                    ),
                ));
            }
            if model_spec.is_some_and(|spec| !spec.features.contains(&ModelFeature::ToolCalling)) {
                return Err(ProviderError::not_supported(
                    "anthropic",
                    format!("Model {} does not support tool calling", request.model),
                ));
            }
            let anthropic_tools = self.transform_tools(tools)?;
            anthropic_request["tools"] = json!(anthropic_tools);

            if let Some(tool_choice) = &request.tool_choice {
                anthropic_request["tool_choice"] =
                    self.transform_tool_choice(tool_choice, &tool_name_map)?;
            }
        }

        // Add thinking configuration. Claude 5 reasoning_effort and typed
        // thinking are normalized here so Chat and Responses share one rule.
        if claude_5 {
            if let Some((thinking, effort)) = Self::claude_5_thinking_config(request)? {
                anthropic_request["thinking"] = thinking;
                if let Some(effort) = effort {
                    anthropic_request["output_config"]["effort"] = json!(effort.as_str());
                }
            }
        } else if let Some(thinking) = &request.thinking
            && thinking.enabled
        {
            if !Self::manual_thinking_supports_tool_choice(request.tool_choice.as_ref()) {
                return Err(ProviderError::invalid_request(
                    "anthropic",
                    "Anthropic manual thinking only supports tool_choice auto or none",
                ));
            }
            let Some(model_spec) = model_spec else {
                return Err(ProviderError::not_supported(
                    "anthropic",
                    format!(
                        "Unknown model {} cannot declare thinking support",
                        request.model
                    ),
                ));
            };
            if !model_spec.features.contains(&ModelFeature::ThinkingMode) {
                return Err(ProviderError::not_supported(
                    "anthropic",
                    format!("Model {} does not support thinking", request.model),
                ));
            }
            let budget = thinking.budget_tokens.unwrap_or(10_000);
            // Anthropic requires max_tokens > budget_tokens. If the default (4096)
            // is not greater than budget_tokens, raise max_tokens to budget + 1.
            let current_max = request.max_tokens.unwrap_or(4096);
            if current_max <= budget {
                anthropic_request["max_tokens"] = json!(budget + 1);
            }
            anthropic_request["thinking"] = json!({
                "type": "enabled",
                "budget_tokens": budget
            });
        }

        if let Some(rf) = &request.response_format
            && rf.format_type == "json_schema"
            && let Some(schema) = &rf.json_schema
        {
            if claude_5 {
                anthropic_request["output_config"]["format"] =
                    json!({"type": "json_schema", "schema": schema});
            } else {
                anthropic_request["response_format"] =
                    json!({"type": "json_schema", "json_schema": schema});
            }
        }

        if let Some(arr) = request
            .extra_params
            .get("anthropic_tools")
            .and_then(|v| v.as_array())
            .filter(|a| !a.is_empty())
        {
            let mut merged: Vec<Value> = anthropic_request
                .get("tools")
                .and_then(|v| v.as_array())
                .cloned()
                .unwrap_or_default();
            merged.extend(arr.iter().cloned());
            anthropic_request["tools"] = json!(merged);
        }

        Ok(anthropic_request)
    }

    #[cfg(any(feature = "gateway", test))]
    pub(crate) fn transform_chat_request_with_extensions(
        &self,
        request: &ChatRequest,
        extensions: &[ChatMessageContinuation],
    ) -> Result<Value, ProviderError> {
        if request.messages.len() != extensions.len() {
            return Err(ProviderError::invalid_request(
                "anthropic",
                "message extension length mismatch",
            ));
        }
        let mut transformed = self.transform_chat_request(request)?;
        Self::enable_legacy_continuation_thinking(request, extensions, &mut transformed)?;
        let messages = transformed["messages"].as_array_mut().ok_or_else(|| {
            anthropic_api_error(500, "Anthropic request messages must be an array")
        })?;
        let mut wire_index = 0;
        for (message, extension) in request.messages.iter().zip(extensions) {
            extension
                .validate()
                .map_err(|message| ProviderError::invalid_request("anthropic", message))?;
            if matches!(message.role, MessageRole::System | MessageRole::Developer) {
                if !extension.is_empty() {
                    return Err(ProviderError::invalid_request(
                        "anthropic",
                        "Anthropic continuation is only valid on assistant messages",
                    ));
                }
                continue;
            }
            let wire_message = messages.get_mut(wire_index).ok_or_else(|| {
                anthropic_api_error(500, "Anthropic message extension index drift")
            })?;
            wire_index += 1;
            let Some(thinking) = extension.anthropic_thinking() else {
                continue;
            };
            if message.role != MessageRole::Assistant {
                return Err(ProviderError::invalid_request(
                    "anthropic",
                    "Anthropic continuation is only valid on assistant messages",
                ));
            }
            let wire_content = wire_message
                .get_mut("content")
                .ok_or_else(|| anthropic_api_error(500, "Anthropic message content is missing"))?;
            let mut content = match std::mem::take(wire_content) {
                Value::Array(blocks) => blocks,
                Value::String(text) if text.is_empty() => Vec::new(),
                Value::String(text) => vec![json!({"type": "text", "text": text})],
                Value::Null => Vec::new(),
                _ => {
                    return Err(anthropic_api_error(
                        500,
                        "Anthropic message content must be text or an array",
                    ));
                }
            };
            if let Some(order) = extension.anthropic_block_order() {
                content = Self::replay_ordered_continuation(content, thinking.blocks(), order)?;
            } else {
                content.splice(
                    0..0,
                    thinking.blocks().iter().map(Self::thinking_block_to_value),
                );
            }
            *wire_content = Value::Array(content);
        }
        if !self.config.uses_compatible_model_allow_list()
            && Self::is_claude_5_protocol_model(&request.model)
            && messages.last().is_some_and(|message| {
                message["role"] == "assistant"
                    && message["content"]
                        .as_array()
                        .is_some_and(|blocks| !blocks.is_empty())
            })
        {
            return Err(ProviderError::invalid_request(
                "anthropic",
                format!("Model {} does not support assistant prefill", request.model),
            ));
        }
        Ok(transformed)
    }

    #[cfg(any(feature = "gateway", test))]
    fn enable_legacy_continuation_thinking(
        request: &ChatRequest,
        extensions: &[ChatMessageContinuation],
        transformed: &mut Value,
    ) -> Result<(), ProviderError> {
        let has_signed_continuation = extensions.iter().any(|extension| {
            extension
                .anthropic_thinking()
                .is_some_and(|thinking| !thinking.blocks().is_empty())
        });
        if !has_signed_continuation || transformed.get("thinking").is_some() {
            return Ok(());
        }
        if request
            .thinking
            .as_ref()
            .is_some_and(|thinking| !thinking.enabled)
        {
            return Err(ProviderError::invalid_request(
                "anthropic",
                "Anthropic signed continuation requires thinking to be enabled",
            ));
        }

        let registry = get_anthropic_registry();
        let Some(model_spec) = registry.get_model_spec(&request.model) else {
            return Err(ProviderError::not_supported(
                "anthropic",
                format!(
                    "Model {} cannot replay Anthropic thinking continuation",
                    request.model
                ),
            ));
        };
        if !model_spec.features.contains(&ModelFeature::ThinkingMode) {
            return Err(ProviderError::not_supported(
                "anthropic",
                format!("Model {} does not support thinking", request.model),
            ));
        }

        let budget = 10_000_u32;
        let current_max = request.max_tokens.unwrap_or(4096);
        if current_max <= budget {
            return Err(ProviderError::invalid_request(
                "anthropic",
                format!(
                    "Anthropic signed continuation requires max_tokens greater than the {budget}-token thinking budget"
                ),
            ));
        }
        transformed["thinking"] = json!({
            "type": "enabled",
            "budget_tokens": budget
        });
        Ok(())
    }

    #[cfg(any(feature = "gateway", test))]
    fn thinking_block_to_value(block: &AnthropicThinkingBlock) -> Value {
        match block {
            AnthropicThinkingBlock::Thinking {
                thinking,
                signature,
            } => json!({
                "type": "thinking",
                "thinking": thinking,
                "signature": signature.expose(),
            }),
            AnthropicThinkingBlock::RedactedThinking { data } => json!({
                "type": "redacted_thinking",
                "data": data.expose(),
            }),
        }
    }

    #[cfg(any(feature = "gateway", test))]
    fn replay_ordered_continuation(
        content: Vec<Value>,
        thinking: &[AnthropicThinkingBlock],
        order: &[AnthropicContentBlockOrder],
    ) -> Result<Vec<Value>, ProviderError> {
        let mut visible_text = String::new();
        let mut tool_blocks = Vec::new();
        for block in content {
            match block.get("type").and_then(Value::as_str) {
                Some("tool_use") => tool_blocks.push(Some(block)),
                Some("text") => {
                    let text = block.get("text").and_then(Value::as_str).ok_or_else(|| {
                        ProviderError::invalid_request(
                            "anthropic",
                            "Anthropic continuation text block has no text",
                        )
                    })?;
                    visible_text.push_str(text);
                }
                other => {
                    return Err(ProviderError::invalid_request(
                        "anthropic",
                        format!(
                            "Anthropic ordered continuation cannot replay content block {other:?}"
                        ),
                    ));
                }
            }
        }
        let mut seen_thinking = vec![false; thinking.len()];
        let mut visible_cursor = 0;
        let mut replay = Vec::with_capacity(order.len());

        for marker in order {
            match *marker {
                AnthropicContentBlockOrder::Thinking { index } => {
                    let Some(block) = thinking.get(index) else {
                        return Err(ProviderError::invalid_request(
                            "anthropic",
                            format!(
                                "Anthropic continuation references missing thinking block {index}"
                            ),
                        ));
                    };
                    if std::mem::replace(&mut seen_thinking[index], true) {
                        return Err(ProviderError::invalid_request(
                            "anthropic",
                            format!("Anthropic continuation repeats thinking block {index}"),
                        ));
                    }
                    replay.push(Self::thinking_block_to_value(block));
                }
                AnthropicContentBlockOrder::Text { start, end } => {
                    replay.push(Self::ordered_visible_block(
                        "text",
                        "text",
                        &visible_text,
                        &mut visible_cursor,
                        start,
                        end,
                    )?);
                }
                AnthropicContentBlockOrder::Refusal { start, end } => {
                    replay.push(Self::ordered_visible_block(
                        "refusal",
                        "refusal",
                        &visible_text,
                        &mut visible_cursor,
                        start,
                        end,
                    )?);
                }
                AnthropicContentBlockOrder::ToolUse { index } => {
                    let Some(block) = tool_blocks.get_mut(index).and_then(Option::take) else {
                        return Err(ProviderError::invalid_request(
                            "anthropic",
                            format!(
                                "Anthropic continuation references missing or repeated tool-use block {index}"
                            ),
                        ));
                    };
                    replay.push(block);
                }
            }
        }
        if seen_thinking.iter().any(|seen| !seen)
            || tool_blocks.iter().any(Option::is_some)
            || visible_cursor != visible_text.len()
        {
            return Err(ProviderError::invalid_request(
                "anthropic",
                "Anthropic continuation block order does not cover every supported content block",
            ));
        }
        Ok(replay)
    }

    #[cfg(any(feature = "gateway", test))]
    fn ordered_visible_block(
        block_type: &str,
        value_key: &str,
        visible_text: &str,
        cursor: &mut usize,
        start: usize,
        end: usize,
    ) -> Result<Value, ProviderError> {
        if start != *cursor || end < start {
            return Err(ProviderError::invalid_request(
                "anthropic",
                format!(
                    "Anthropic continuation {block_type} span {start}..{end} does not follow visible offset {}",
                    *cursor
                ),
            ));
        }
        let text = visible_text.get(start..end).ok_or_else(|| {
            ProviderError::invalid_request(
                "anthropic",
                format!(
                    "Anthropic continuation {block_type} span {start}..{end} is not a valid UTF-8 range"
                ),
            )
        })?;
        *cursor = end;
        let mut block = serde_json::Map::with_capacity(2);
        block.insert("type".to_string(), Value::String(block_type.to_string()));
        block.insert(value_key.to_string(), Value::String(text.to_string()));
        Ok(Value::Object(block))
    }

    pub(crate) fn is_claude_5_protocol_model(model: &str) -> bool {
        Self::claude_5_model(model).is_some()
    }

    fn claude_5_model(model: &str) -> Option<Claude5Model> {
        match model {
            "claude-fable-5" | "claude-fable-5-1" => Some(Claude5Model::Fable),
            "claude-opus-5" | "claude-opus-5-5" => Some(Claude5Model::Opus),
            "claude-sonnet-5" | "claude-sonnet-5-5" => Some(Claude5Model::Sonnet),
            _ => None,
        }
    }

    pub(crate) fn validate_claude_5_request_shape(
        request: &ChatRequest,
    ) -> Result<(), ProviderError> {
        let unsupported = if request.temperature.is_some_and(|value| value != 1.0) {
            Some("temperature")
        } else if request
            .top_p
            .is_some_and(|value| !value.is_finite() || !(0.99..=1.0).contains(&value))
        {
            Some("top_p")
        } else if request.extra_params.contains_key("top_k") {
            Some("top_k")
        } else {
            None
        };
        if let Some(parameter) = unsupported {
            return Err(ProviderError::invalid_request(
                "anthropic",
                format!(
                    "Model {} does not support non-default {parameter}",
                    request.model
                ),
            ));
        }
        if Self::has_trailing_assistant_prefill(request) {
            return Err(ProviderError::invalid_request(
                "anthropic",
                format!("Model {} does not support assistant prefill", request.model),
            ));
        }
        Ok(())
    }

    fn has_trailing_assistant_prefill(request: &ChatRequest) -> bool {
        request
            .messages
            .iter()
            .rev()
            .find(|message| {
                !matches!(message.role, MessageRole::System | MessageRole::Developer)
                    && Self::message_has_payload(message)
            })
            .is_some_and(|message| message.role == MessageRole::Assistant)
    }

    fn message_has_payload(message: &ChatMessage) -> bool {
        let content = match message.content.as_ref() {
            Some(MessageContent::Text(text)) => !text.trim().is_empty(),
            Some(MessageContent::Parts(parts)) => parts.iter().any(|part| match part {
                ContentPart::Text { text } => !text.trim().is_empty(),
                _ => true,
            }),
            None => false,
        };
        content
            || message.audio.is_some()
            || message.thinking.is_some()
            || message.function_call.is_some()
            || message
                .tool_calls
                .as_ref()
                .is_some_and(|calls| !calls.is_empty())
    }

    pub(super) fn validate_claude_5_legacy_functions(
        request: &ChatRequest,
    ) -> Result<(), ProviderError> {
        if request
            .functions
            .as_ref()
            .is_some_and(|functions| !functions.is_empty())
            || request.function_call.is_some()
            || request
                .messages
                .iter()
                .any(|message| message.function_call.is_some())
        {
            return Err(ProviderError::not_supported(
                "anthropic",
                format!(
                    "Model {} does not support legacy functions/function_call; use tools/tool_choice",
                    request.model
                ),
            ));
        }
        Ok(())
    }

    pub(super) fn claude_5_thinking_config(
        request: &ChatRequest,
    ) -> Result<Option<(Value, Option<AnthropicEffort>)>, ProviderError> {
        let model = Self::claude_5_model(&request.model).ok_or_else(|| {
            ProviderError::not_supported(
                "anthropic",
                format!("Model {} is not an exact Claude 5 model", request.model),
            )
        })?;
        let requested_effort = request
            .reasoning_effort
            .as_deref()
            .map(Self::parse_reasoning_effort)
            .transpose()?;
        let Some(thinking) = request.thinking.as_ref() else {
            return Ok(Some(match requested_effort {
                Some(effort) => (
                    json!({"type": "adaptive", "display": "summarized"}),
                    Some(effort),
                ),
                None => (json!({"type": "adaptive"}), None),
            }));
        };
        if thinking.budget_tokens.is_some() {
            return Err(ProviderError::invalid_request(
                "anthropic",
                "Claude 5 adaptive thinking does not support budget_tokens",
            ));
        }
        let typed_effort = thinking.effort.map(AnthropicEffort::from);
        if let (Some(typed), Some(requested)) = (typed_effort, requested_effort)
            && typed != requested
        {
            return Err(ProviderError::invalid_request(
                "anthropic",
                format!(
                    "Conflicting thinking effort values: thinking.effort={}, reasoning_effort={}",
                    typed.as_str(),
                    requested.as_str()
                ),
            ));
        }
        let effort = typed_effort.or(requested_effort);
        if !thinking.enabled {
            if matches!(
                request.model.as_str(),
                "claude-fable-5-1" | "claude-opus-5-5"
            ) {
                return Err(ProviderError::invalid_request(
                    "anthropic",
                    format!("{} cannot disable thinking", request.model),
                ));
            }
            if request.model == "claude-sonnet-5-5" {
                if matches!(effort, Some(AnthropicEffort::XHigh | AnthropicEffort::Max)) {
                    return Err(ProviderError::invalid_request(
                        "anthropic",
                        "claude-sonnet-5-5 requires adaptive thinking at xhigh or max effort",
                    ));
                }
                return Ok(Some((json!({"type": "between_tools"}), effort)));
            }
            if model == Claude5Model::Fable {
                return Err(ProviderError::invalid_request(
                    "anthropic",
                    "claude-fable-5 cannot disable thinking",
                ));
            }
            if model == Claude5Model::Opus
                && matches!(effort, Some(AnthropicEffort::XHigh | AnthropicEffort::Max))
            {
                return Err(ProviderError::invalid_request(
                    "anthropic",
                    "claude-opus-5 cannot disable thinking at xhigh or max effort",
                ));
            }
            return Ok(Some((json!({"type": "disabled"}), effort)));
        }
        Ok(Some((
            json!({
                "type": "adaptive",
                "display": if thinking.include_thinking { "summarized" } else { "omitted" }
            }),
            effort,
        )))
    }

    fn parse_reasoning_effort(value: &str) -> Result<AnthropicEffort, ProviderError> {
        match value {
            "low" => Ok(AnthropicEffort::Low),
            "medium" => Ok(AnthropicEffort::Medium),
            "high" => Ok(AnthropicEffort::High),
            "xhigh" => Ok(AnthropicEffort::XHigh),
            "max" => Ok(AnthropicEffort::Max),
            other => Err(ProviderError::invalid_request(
                "anthropic",
                format!(
                    "Unsupported reasoning_effort '{other}'; expected low, medium, high, xhigh, or max"
                ),
            )),
        }
    }
}