zeph-llm 0.19.0

LLM provider abstraction with Ollama, Claude, OpenAI, and Candle backends
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Claude (Anthropic) LLM provider implementation.
//!
//! [`ClaudeProvider`] wraps the Anthropic Messages API and supports:
//! - Standard chat and streaming via Server-Sent Events
//! - Native tool use (function calling)
//! - Vision (image input in messages)
//! - Extended and adaptive thinking (`claude-sonnet-4-6`, `claude-opus-4-6`)
//! - Prompt caching (`cache_control` blocks) for cost reduction
//! - Server-side context compaction (compact-2026-01-12 beta)
//! - Extended context window (context-1m-2025-08-07 beta)
//!
//! # Configuration
//!
//! ```toml
//! [[llm.providers]]
//! name = "claude"
//! type = "claude"
//! model = "claude-sonnet-4-6"
//! max_tokens = 8192
//! api_key_vault = "ZEPH_CLAUDE_API_KEY"
//! ```
//!
//! # Extended Thinking
//!
//! Enable via [`ClaudeProvider::with_thinking`]:
//!
//! ```rust,no_run
//! use zeph_llm::claude::ClaudeProvider;
//! use zeph_llm::{ThinkingConfig, ThinkingEffort};
//!
//! # fn build() -> Result<ClaudeProvider, zeph_llm::LlmError> {
//! let provider = ClaudeProvider::new("key".into(), "claude-sonnet-4-6".into(), 16_000)
//!     .with_thinking(ThinkingConfig::Extended { budget_tokens: 10_000 })?;
//! # Ok(provider)
//! # }
//! ```

mod cache;
mod request;
#[cfg(test)]
mod tests;
mod types;

use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use parking_lot::Mutex;

use crate::error::LlmError;
use crate::usage::UsageTracker;

use crate::provider::{
    ChatResponse, ChatStream, GenerationOverrides, LlmProvider, Message, MessagePart, StatusTx,
    ToolDefinition,
};
use crate::retry::send_with_retry;
use crate::sse::claude_sse_to_stream;

use self::cache::{log_cache_usage, split_system_into_blocks, tool_cache_key};
use self::request::{parse_tool_response, split_messages, split_messages_structured};
use self::types::{
    AnthropicContentBlock, AnthropicTool, ContextManagement, ContextManagementTrigger,
    OutputConfig, RequestBody, StructuredApiMessage, SystemContentBlock, ToolApiResponse,
    ToolChoice, ToolRequestBody, TypedToolRequestBody, VisionRequestBody,
};

pub use self::types::{ThinkingConfig, ThinkingEffort};
use self::types::{budget_to_effort, thinking_capability};

const API_URL: &str = "https://api.anthropic.com/v1/messages";
const ANTHROPIC_VERSION: &str = "2023-06-01";
const ANTHROPIC_BETA_INTERLEAVED_THINKING: &str = "interleaved-thinking-2025-05-14";
const ANTHROPIC_BETA_COMPACT: &str = "compact-2026-01-12";
const ANTHROPIC_BETA_EXTENDED_CONTEXT: &str = "context-1m-2025-08-07";
const MAX_RETRIES: u32 = 3;

use self::types::MIN_MAX_TOKENS_WITH_THINKING;

/// [`LlmProvider`] backend for the Anthropic Claude API.
///
/// Construct with [`ClaudeProvider::new`] and then chain optional builder methods:
/// - [`with_thinking`](Self::with_thinking) — extended or adaptive thinking
/// - [`with_server_compaction`](Self::with_server_compaction) — server-side context compaction
/// - [`with_extended_context`](Self::with_extended_context) — 1M-token context window
/// - [`with_cache_user_messages`](Self::with_cache_user_messages) — prompt caching
/// - [`with_status_tx`](Self::with_status_tx) — real-time status events for the UI
/// - [`with_generation_overrides`](Self::with_generation_overrides) — temperature / top-p
pub struct ClaudeProvider {
    client: reqwest::Client,
    api_key: String,
    model: String,
    max_tokens: u32,
    thinking: Option<ThinkingConfig>,
    pub(crate) status_tx: Option<StatusTx>,
    /// Whether to attach `cache_control` to user messages in multi-turn conversations.
    cache_user_messages: bool,
    usage: UsageTracker,
    /// Cached pre-serialized tool definitions. Keyed by hash of names+schemas; invalidated when the set changes.
    tool_cache: Mutex<Option<(u64, Vec<serde_json::Value>)>>,
    generation_overrides: Option<GenerationOverrides>,
    /// Enable Claude server-side context compaction (compact-2026-01-12 beta).
    server_compaction: bool,
    /// Set to `true` at runtime when the API rejects the `compact-2026-01-12` beta header
    /// (e.g. header deprecated/removed). Shared via `Arc` so clones observe the same state.
    server_compaction_rejected: Arc<AtomicBool>,
    /// Most recent compaction summary received from the API, if any.
    last_compaction: Mutex<Option<String>>,
    enable_extended_context: bool,
}

impl fmt::Debug for ClaudeProvider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ClaudeProvider")
            .field("client", &"<reqwest::Client>")
            .field("api_key", &"<redacted>")
            .field("model", &self.model)
            .field("max_tokens", &self.max_tokens)
            .field("thinking", &self.thinking)
            .field("status_tx", &self.status_tx.is_some())
            .field("cache_user_messages", &self.cache_user_messages)
            .field("usage", &self.usage)
            .field(
                "tool_cache",
                &self.tool_cache.lock().as_ref().map(|(hash, _)| *hash),
            )
            .field("generation_overrides", &self.generation_overrides)
            .field("server_compaction", &self.server_compaction)
            .field(
                "server_compaction_rejected",
                &self.server_compaction_rejected.load(Ordering::Relaxed),
            )
            .field(
                "last_compaction",
                &self.last_compaction.lock().as_ref().map(String::len),
            )
            .field("enable_extended_context", &self.enable_extended_context)
            .finish()
    }
}

impl Clone for ClaudeProvider {
    fn clone(&self) -> Self {
        Self {
            client: self.client.clone(),
            api_key: self.api_key.clone(),
            model: self.model.clone(),
            max_tokens: self.max_tokens,
            thinking: self.thinking.clone(),
            status_tx: self.status_tx.clone(),
            cache_user_messages: self.cache_user_messages,
            usage: UsageTracker::default(),
            tool_cache: Mutex::new(None),
            generation_overrides: self.generation_overrides.clone(),
            server_compaction: self.server_compaction,
            server_compaction_rejected: Arc::clone(&self.server_compaction_rejected),
            last_compaction: Mutex::new(None),
            enable_extended_context: self.enable_extended_context,
        }
    }
}

impl ClaudeProvider {
    const MAX_CACHE_CONTROL_BLOCKS: usize = 4;

    /// Create a new provider.
    ///
    /// Warns at runtime when `model` starts with `"claude-3"` because those identifiers
    /// refer to retired models that may cause API errors.
    #[must_use]
    pub fn new(api_key: String, model: String, max_tokens: u32) -> Self {
        if model.starts_with("claude-3") {
            tracing::warn!(
                model = %model,
                "configured model is a retired Claude 3 identifier and may cause API errors; \
                consider upgrading to claude-sonnet-4-6 or claude-haiku-4-5-20251001",
            );
        }
        Self {
            client: crate::http::llm_client(600),
            api_key,
            model,
            max_tokens,
            thinking: None,
            status_tx: None,
            cache_user_messages: true,
            usage: UsageTracker::default(),
            tool_cache: Mutex::new(None),
            generation_overrides: None,
            server_compaction: false,
            server_compaction_rejected: Arc::new(AtomicBool::new(false)),
            last_compaction: Mutex::new(None),
            enable_extended_context: false,
        }
    }

    /// Override generation parameters (temperature, top-p) for this provider.
    #[must_use]
    pub fn with_generation_overrides(mut self, overrides: GenerationOverrides) -> Self {
        self.generation_overrides = Some(overrides);
        self
    }

    /// Replace the underlying HTTP client. Mainly used in tests to inject a mock transport.
    #[must_use]
    pub fn with_client(mut self, client: reqwest::Client) -> Self {
        self.client = client;
        self
    }

    /// Attach a status event sender so the UI receives retry and fallback notifications.
    #[must_use]
    pub fn with_status_tx(mut self, tx: StatusTx) -> Self {
        self.status_tx = Some(tx);
        self
    }

    /// Control whether `cache_control` breakpoints are added to user messages.
    ///
    /// Enabled by default. Disabling saves a small amount of CPU at the cost of losing
    /// prompt cache hits on repeated system prompts.
    #[must_use]
    pub fn with_cache_user_messages(mut self, enabled: bool) -> Self {
        self.cache_user_messages = enabled;
        self
    }

    /// Enable server-side context compaction (Claude compact-2026-01-12 beta).
    ///
    /// When enabled, the API automatically summarizes long conversations and returns
    /// a `compaction` content block. Client-side compaction should be skipped when
    /// this is active.
    #[must_use]
    pub fn with_server_compaction(mut self, enabled: bool) -> Self {
        if enabled && self.model.contains("haiku") {
            tracing::warn!(
                model = %self.model,
                "server-side compaction (compact-2026-01-12) not supported for Haiku models — \
                disabling"
            );
            self.server_compaction = false;
            return self;
        }
        self.server_compaction = enabled;
        self
    }

    /// Return `true` when server-side compaction is enabled.
    #[must_use]
    pub fn server_compaction_enabled(&self) -> bool {
        self.server_compaction
    }

    /// Return the compaction summary from the most recent API call, if a compaction occurred.
    /// Clears the stored value after reading.
    pub fn take_compaction_summary(&self) -> Option<String> {
        self.last_compaction.lock().take()
    }

    /// Return `true` if the `compact-2026-01-12` beta header was rejected by the API
    /// during a previous request this session.
    #[must_use]
    pub fn is_server_compaction_rejected(&self) -> bool {
        self.server_compaction_rejected.load(Ordering::Relaxed)
    }

    /// Detect whether a 400 response body indicates the `compact-2026-01-12` beta header
    /// was rejected by the API.
    fn is_compact_beta_rejection(status: reqwest::StatusCode, body: &str) -> bool {
        status == reqwest::StatusCode::BAD_REQUEST
            && (body.contains(ANTHROPIC_BETA_COMPACT)
                || body.contains("unknown beta")
                || body.contains("invalid beta")
                || body.contains("context_management"))
    }

    #[must_use]
    pub fn with_extended_context(mut self, enabled: bool) -> Self {
        self.enable_extended_context = enabled;
        if enabled {
            tracing::info!("Claude extended context (1M) enabled");
        }
        self
    }

    /// Configure thinking mode for Claude extended/adaptive thinking.
    ///
    /// # Errors
    ///
    /// Returns an error if `budget_tokens` is outside the API-allowed range
    /// `[1024, 128_000]` or if `budget_tokens >= max_tokens` after the automatic
    /// 16 000-token floor is applied.
    pub fn with_thinking(mut self, thinking: ThinkingConfig) -> Result<Self, LlmError> {
        if let ThinkingConfig::Extended { budget_tokens } = thinking {
            const MIN_BUDGET: u32 = 1_024;
            const MAX_BUDGET: u32 = 128_000;
            if !(MIN_BUDGET..=MAX_BUDGET).contains(&budget_tokens) {
                return Err(LlmError::Other(format!(
                    "budget_tokens {budget_tokens} is out of range [{MIN_BUDGET}, {MAX_BUDGET}]"
                )));
            }
            let max_tokens = self.max_tokens.max(MIN_MAX_TOKENS_WITH_THINKING);
            if budget_tokens >= max_tokens {
                return Err(LlmError::Other(format!(
                    "budget_tokens {budget_tokens} must be less than max_tokens {max_tokens}"
                )));
            }
            self.max_tokens = max_tokens;
        } else {
            self.max_tokens = self.max_tokens.max(MIN_MAX_TOKENS_WITH_THINKING);
        }
        self.thinking = Some(thinking);
        Ok(self)
    }

    /// Configure thinking mode, propagating any validation error.
    ///
    /// # Errors
    ///
    /// Forwards errors from [`Self::with_thinking`].
    pub fn with_thinking_opt(self, thinking: Option<ThinkingConfig>) -> Result<Self, LlmError> {
        match thinking {
            Some(t) => self.with_thinking(t),
            None => Ok(self),
        }
    }

    /// Fetch all available Claude models from the Anthropic API and cache them.
    ///
    /// Paginates until `has_more` is false.
    /// 401/403 responses are returned as `LlmError::Other` without touching the cache.
    ///
    /// # Errors
    ///
    /// Returns an error if the API request fails or returns an auth error.
    ///
    /// # Panics
    ///
    /// Panics if the hardcoded Anthropic API URL cannot be parsed (impossible in practice).
    pub async fn list_models_remote(
        &self,
    ) -> Result<Vec<crate::model_cache::RemoteModelInfo>, LlmError> {
        let mut models: Vec<crate::model_cache::RemoteModelInfo> = Vec::new();
        let mut after_id: Option<String> = None;

        loop {
            // Build URL with cursor as a proper query parameter to avoid injection.
            let url = {
                let mut u = reqwest::Url::parse("https://api.anthropic.com/v1/models")
                    .expect("static URL is valid");
                if let Some(ref cursor) = after_id {
                    u.query_pairs_mut().append_pair("after_id", cursor);
                }
                u
            };

            let resp = self
                .client
                .get(url)
                .header("x-api-key", &self.api_key)
                .header("anthropic-version", ANTHROPIC_VERSION)
                .send()
                .await?;

            let status = resp.status();
            if status == reqwest::StatusCode::UNAUTHORIZED
                || status == reqwest::StatusCode::FORBIDDEN
            {
                return Err(LlmError::Other(format!(
                    "Claude API auth error listing models: {status}"
                )));
            }
            if !status.is_success() {
                let body = resp.text().await.unwrap_or_default();
                tracing::debug!(status = %status, body = %body, "Claude list_models_remote error body");
                return Err(LlmError::Other(format!(
                    "Claude list models failed: {status}"
                )));
            }

            let page: serde_json::Value = resp.json().await?;
            if let Some(data) = page.get("data").and_then(|v| v.as_array()) {
                for item in data {
                    let type_field = item
                        .get("type")
                        .and_then(|v| v.as_str())
                        .unwrap_or_default();
                    if type_field != "model" {
                        continue;
                    }
                    let id = item
                        .get("id")
                        .and_then(|v| v.as_str())
                        .unwrap_or_default()
                        .to_string();
                    let display_name = item
                        .get("display_name")
                        .and_then(|v| v.as_str())
                        .unwrap_or(&id)
                        .to_string();
                    let created_at = item.get("created_at").and_then(serde_json::Value::as_i64);
                    models.push(crate::model_cache::RemoteModelInfo {
                        id,
                        display_name,
                        context_window: None,
                        created_at,
                    });
                }
            }

            let has_more = page
                .get("has_more")
                .and_then(serde_json::Value::as_bool)
                .unwrap_or(false);
            if !has_more {
                break;
            }
            after_id = page
                .get("last_id")
                .and_then(|v| v.as_str())
                .map(str::to_owned);
            if after_id.is_none() {
                break;
            }
        }

        let cache = crate::model_cache::ModelCache::for_slug("claude");
        cache.save(&models)?;
        Ok(models)
    }

    fn build_thinking_param(
        &self,
    ) -> (
        Option<types::ThinkingParam>,
        Option<f64>,
        Option<ThinkingEffort>,
    ) {
        let cap = thinking_capability(&self.model);
        match &self.thinking {
            None => (None, None, None),
            Some(ThinkingConfig::Extended { budget_tokens }) if cap.prefers_effort => {
                let effort = budget_to_effort(*budget_tokens);
                tracing::warn!(
                    model = %self.model,
                    budget_tokens,
                    ?effort,
                    "budget_tokens is deprecated for Opus 4.6; auto-converting to effort"
                );
                (
                    Some(types::ThinkingParam {
                        thinking_type: "adaptive",
                        budget_tokens: None,
                    }),
                    None,
                    Some(effort),
                )
            }
            Some(ThinkingConfig::Extended { budget_tokens }) => (
                Some(types::ThinkingParam {
                    thinking_type: "enabled",
                    budget_tokens: Some(*budget_tokens),
                }),
                None,
                None,
            ),
            Some(ThinkingConfig::Adaptive { effort }) => (
                Some(types::ThinkingParam {
                    thinking_type: "adaptive",
                    budget_tokens: None,
                }),
                None,
                *effort,
            ),
        }
    }

    fn beta_header(&self, has_tools: bool) -> Option<String> {
        let mut headers: Vec<&str> = Vec::new();

        if self.enable_extended_context {
            headers.push(ANTHROPIC_BETA_EXTENDED_CONTEXT);
        }

        let cap = thinking_capability(&self.model);
        if self.thinking.is_some()
            && has_tools
            && cap.needs_interleaved_beta
            && matches!(self.thinking, Some(ThinkingConfig::Extended { .. }))
        {
            headers.push(ANTHROPIC_BETA_INTERLEAVED_THINKING);
        }

        if self.server_compaction && !self.server_compaction_rejected.load(Ordering::Relaxed) {
            headers.push(ANTHROPIC_BETA_COMPACT);
        }

        if headers.is_empty() {
            None
        } else {
            Some(headers.join(","))
        }
    }

    /// Build the `context_management` field for server-side compaction.
    /// Returns `None` when `server_compaction` is disabled or the beta header was rejected.
    fn context_management(&self) -> Option<ContextManagement> {
        if !self.server_compaction || self.server_compaction_rejected.load(Ordering::Relaxed) {
            return None;
        }
        let context_window =
            u32::try_from(self.context_window().unwrap_or(200_000)).unwrap_or(200_000_u32);
        // Default hard_compaction_threshold of 0.90 — matches client-side default.
        // Multiply before dividing to preserve precision (avoid losing up to 99 tokens).
        let trigger_tokens = context_window * 80 / 100;
        Some(ContextManagement {
            trigger: ContextManagementTrigger {
                kind: "input_tokens",
                value: trigger_tokens,
            },
            pause_after_compaction: false,
        })
    }

    fn get_or_build_api_tools(&self, tools: &[ToolDefinition]) -> Vec<serde_json::Value> {
        let key = tool_cache_key(tools);
        let mut guard = self.tool_cache.lock();
        if let Some((cached_key, ref cached_values)) = *guard
            && cached_key == key
        {
            return cached_values.clone();
        }
        let mut serialized: Vec<serde_json::Value> = tools
            .iter()
            .map(|t| {
                serde_json::json!({
                    "name": t.name,
                    "description": t.description,
                    "input_schema": t.parameters,
                })
            })
            .collect();
        if let Some(Some(obj)) = serialized.last_mut().map(serde_json::Value::as_object_mut) {
            obj.insert(
                "cache_control".into(),
                serde_json::json!({"type": "ephemeral"}),
            );
        }
        *guard = Some((key, serialized.clone()));
        serialized
    }

    fn store_cache_usage(&self, usage: &types::ApiUsage) {
        self.usage.record_cache(
            usage.cache_creation_input_tokens,
            usage.cache_read_input_tokens,
        );
        self.usage
            .record_usage(usage.input_tokens, usage.output_tokens);
    }

    fn has_image_parts(messages: &[Message]) -> bool {
        messages
            .iter()
            .any(|m| m.parts.iter().any(|p| matches!(p, MessagePart::Image(_))))
    }

    fn cap_block_cache_controls(
        tool_blocks: usize,
        system_blocks: Option<&[SystemContentBlock]>,
        chat_messages: Option<&mut Vec<StructuredApiMessage>>,
    ) {
        let tagged_blocks = tool_blocks
            + system_blocks.map_or(0, |system| {
                system
                    .iter()
                    .filter(|block| block.cache_control.is_some())
                    .count()
            });

        if tagged_blocks >= Self::MAX_CACHE_CONTROL_BLOCKS {
            Self::clear_message_cache_controls(chat_messages);
            return;
        }

        let remaining = Self::MAX_CACHE_CONTROL_BLOCKS - tagged_blocks;
        Self::retain_last_message_cache_controls(chat_messages, remaining);
    }

    fn clear_message_cache_controls(chat_messages: Option<&mut Vec<StructuredApiMessage>>) {
        Self::retain_last_message_cache_controls(chat_messages, 0);
    }

    fn retain_last_message_cache_controls(
        chat_messages: Option<&mut Vec<StructuredApiMessage>>,
        keep: usize,
    ) {
        let mut seen = 0usize;
        if let Some(chat) = chat_messages {
            for message in chat.iter_mut().rev() {
                let types::StructuredContent::Blocks(blocks) = &mut message.content else {
                    continue;
                };
                for block in blocks.iter_mut().rev() {
                    let maybe_cache = match block {
                        AnthropicContentBlock::Text { cache_control, .. }
                        | AnthropicContentBlock::ToolResult { cache_control, .. } => {
                            Some(cache_control)
                        }
                        AnthropicContentBlock::ToolUse { .. }
                        | AnthropicContentBlock::Image { .. }
                        | AnthropicContentBlock::Thinking { .. }
                        | AnthropicContentBlock::RedactedThinking { .. }
                        | AnthropicContentBlock::Compaction { .. } => None,
                    };
                    if let Some(cache_control) = maybe_cache
                        && cache_control.is_some()
                    {
                        if seen < keep {
                            seen += 1;
                        } else {
                            *cache_control = None;
                        }
                    }
                }
            }
        }
    }

    fn build_request(&self, messages: &[Message], stream: bool) -> reqwest::RequestBuilder {
        let (thinking_param, mut temperature, effort) = self.build_thinking_param();
        if thinking_param.is_none()
            && let Some(Some(t)) = self.generation_overrides.as_ref().map(|ov| ov.temperature)
        {
            temperature = Some(t);
        }
        let output_config = effort.map(|e| OutputConfig { effort: e }); // lgtm[rust/cleartext-logging]

        let cap = thinking_capability(&self.model);
        // Opus 4.6 with thinking enabled does not support prefill: strip trailing assistant
        // messages so the conversation always ends with a user turn.
        let no_prefill = cap.prefers_effort && thinking_param.is_some();

        if Self::has_image_parts(messages) {
            let (system, mut chat_messages) =
                split_messages_structured(messages, self.cache_user_messages);
            let system_blocks = system.map(|s| split_system_into_blocks(&s, &self.model));
            Self::cap_block_cache_controls(0, system_blocks.as_deref(), Some(&mut chat_messages));
            if no_prefill {
                while chat_messages.last().is_some_and(|m| m.role == "assistant") {
                    chat_messages.pop();
                }
            }
            let beta = self.beta_header(false);
            let body = VisionRequestBody {
                model: &self.model,
                max_tokens: self.max_tokens,
                system: system_blocks,
                messages: &chat_messages,
                stream,
                thinking: thinking_param,
                output_config,
                temperature,
                context_management: self.context_management(),
            };
            let mut req = self
                .client
                .post(API_URL)
                .header("x-api-key", &self.api_key)
                .header("anthropic-version", ANTHROPIC_VERSION);
            if let Some(b) = beta {
                req = req.header("anthropic-beta", b);
            }
            return req.header("content-type", "application/json").json(&body);
        }

        let (system, mut chat_messages) = split_messages(messages);
        if no_prefill {
            while chat_messages.last().is_some_and(|m| m.role == "assistant") {
                chat_messages.pop();
            }
        }
        let system_blocks = system.map(|s| split_system_into_blocks(&s, &self.model));
        let beta = self.beta_header(false);
        let body = RequestBody {
            model: &self.model,
            max_tokens: self.max_tokens,
            system: system_blocks,
            messages: &chat_messages,
            stream,
            thinking: thinking_param,
            output_config,
            temperature,
            context_management: self.context_management(),
        };

        let mut req = self
            .client
            .post(API_URL)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", ANTHROPIC_VERSION);
        if let Some(b) = beta {
            req = req.header("anthropic-beta", b);
        }
        req.header("content-type", "application/json").json(&body)
    }

    async fn send_request(&self, messages: &[Message]) -> Result<String, LlmError> {
        let response = send_with_retry("Claude", MAX_RETRIES, self.status_tx.as_ref(), || {
            self.build_request(messages, false).send()
        })
        .await?;

        let status = response.status();
        let text = response.text().await.map_err(LlmError::Http)?;

        if !status.is_success() {
            if Self::is_compact_beta_rejection(status, &text) {
                self.server_compaction_rejected
                    .store(true, Ordering::Relaxed);
                tracing::warn!(
                    "compact-2026-01-12 beta header rejected by Claude API; \
                    disabling server-side compaction for this session. \
                    Update your config to set `server_compaction = false`."
                );
                return Err(LlmError::BetaHeaderRejected {
                    header: ANTHROPIC_BETA_COMPACT.into(),
                });
            }
            tracing::error!("Claude API error {status}: {text}");
            return Err(LlmError::Other(format!(
                "Claude API request failed (status {status})"
            )));
        }

        if Self::has_image_parts(messages) {
            let resp: ToolApiResponse = serde_json::from_str(&text)?;
            if let Some(ref usage) = resp.usage {
                log_cache_usage(usage);
                self.store_cache_usage(usage);
            }
            let extracted = resp.content.into_iter().find_map(|b| {
                if let AnthropicContentBlock::Text { text, .. } = b {
                    Some(text)
                } else {
                    None
                }
            });
            return extracted.ok_or(LlmError::EmptyResponse {
                provider: "claude".into(),
            });
        }

        let resp: types::ApiResponse = serde_json::from_str(&text)?;

        if let Some(ref usage) = resp.usage {
            log_cache_usage(usage);
            self.store_cache_usage(usage);
        }

        resp.content
            .first()
            .map(|c| c.text.clone())
            .ok_or(LlmError::EmptyResponse {
                provider: "claude".into(),
            })
    }

    async fn send_stream_request(
        &self,
        messages: &[Message],
    ) -> Result<reqwest::Response, LlmError> {
        let response = send_with_retry("Claude", MAX_RETRIES, self.status_tx.as_ref(), || {
            self.build_request(messages, true).send()
        })
        .await?;

        let status = response.status();
        if !status.is_success() {
            let text = response.text().await.map_err(LlmError::Http)?;
            if Self::is_compact_beta_rejection(status, &text) {
                self.server_compaction_rejected
                    .store(true, Ordering::Relaxed);
                tracing::warn!(
                    "compact-2026-01-12 beta header rejected by Claude API (streaming); \
                    disabling server-side compaction for this session. \
                    Update your config to set `server_compaction = false`."
                );
                return Err(LlmError::BetaHeaderRejected {
                    header: ANTHROPIC_BETA_COMPACT.into(),
                });
            }
            tracing::error!("Claude API streaming request error {status}: {text}");
            return Err(LlmError::Other(format!(
                "Claude API streaming request failed (status {status})"
            )));
        }

        Ok(response)
    }
}

impl LlmProvider for ClaudeProvider {
    fn context_window(&self) -> Option<usize> {
        if self.model.contains("opus")
            || self.model.contains("sonnet")
            || self.model.contains("haiku")
        {
            // Only Opus 4.6 and Sonnet 4.6 support the 1M context window.
            // Haiku does not support extended context even when the flag is set.
            let supports_1m = self.enable_extended_context && !self.model.contains("haiku");
            if supports_1m {
                Some(1_000_000)
            } else {
                if self.enable_extended_context && self.model.contains("haiku") {
                    tracing::warn!(
                        model = %self.model,
                        "enable_extended_context has no effect for Haiku models; \
                        extended context (1M) is only supported by Opus 4.6 and Sonnet 4.6"
                    );
                }
                Some(200_000)
            }
        } else {
            None
        }
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(
            name = "llm.chat",
            skip_all,
            fields(provider = self.name(), model = self.model_identifier())
        )
    )]
    async fn chat(&self, messages: &[Message]) -> Result<String, LlmError> {
        self.send_request(messages).await
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(
            name = "llm.chat_stream",
            skip_all,
            fields(provider = self.name(), model = self.model_identifier())
        )
    )]
    async fn chat_stream(&self, messages: &[Message]) -> Result<ChatStream, LlmError> {
        let response = self.send_stream_request(messages).await?;
        Ok(claude_sse_to_stream(response))
    }

    fn supports_streaming(&self) -> bool {
        true
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(
            name = "llm.embed",
            skip_all,
            fields(provider = self.name(), model = self.model_identifier())
        )
    )]
    async fn embed(&self, _text: &str) -> Result<Vec<f32>, LlmError> {
        Err(LlmError::EmbedUnsupported {
            provider: "claude".into(),
        })
    }

    fn supports_embeddings(&self) -> bool {
        false
    }

    #[allow(clippy::unnecessary_literal_bound)]
    fn name(&self) -> &str {
        "claude"
    }

    fn model_identifier(&self) -> &str {
        &self.model
    }

    fn supports_structured_output(&self) -> bool {
        true
    }

    async fn chat_typed<T>(&self, messages: &[Message]) -> Result<T, LlmError>
    where
        T: serde::de::DeserializeOwned + schemars::JsonSchema + 'static,
        Self: Sized,
    {
        let (schema_value, _) = crate::provider::cached_schema::<T>()?;
        let type_name = crate::provider::short_type_name::<T>();

        let tool_name = format!("submit_{type_name}");
        let tool = ToolDefinition {
            name: tool_name.clone().into(),
            description: format!("Submit the structured {type_name} result"),
            parameters: schema_value,
        };

        let (system, mut chat_messages) =
            split_messages_structured(messages, self.cache_user_messages);
        let api_tool = AnthropicTool {
            name: tool.name.as_str(),
            description: &tool.description,
            input_schema: &tool.parameters,
        };

        let (thinking_param, mut temperature, effort) = self.build_thinking_param();
        if thinking_param.is_none()
            && let Some(Some(t)) = self.generation_overrides.as_ref().map(|ov| ov.temperature)
        {
            temperature = Some(t);
        }
        let output_config = effort.map(|e| OutputConfig { effort: e });
        let system_blocks = system.map(|s| split_system_into_blocks(&s, &self.model));
        Self::cap_block_cache_controls(0, system_blocks.as_deref(), Some(&mut chat_messages));
        let beta = self.beta_header(true);
        let body = TypedToolRequestBody {
            model: &self.model,
            max_tokens: self.max_tokens,
            system: system_blocks,
            messages: &chat_messages,
            tools: &[api_tool],
            tool_choice: ToolChoice {
                r#type: "tool",
                name: &tool_name,
            },
            thinking: thinking_param,
            output_config,
            temperature,
            context_management: self.context_management(),
        };

        let mut req = self
            .client
            .post(API_URL)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", ANTHROPIC_VERSION);
        if let Some(b) = beta {
            req = req.header("anthropic-beta", b);
        }
        let response = req
            .header("content-type", "application/json")
            .json(&body)
            .send()
            .await?;

        let status = response.status();
        let text = response.text().await.map_err(LlmError::Http)?;

        if !status.is_success() {
            if Self::is_compact_beta_rejection(status, &text) {
                self.server_compaction_rejected
                    .store(true, Ordering::Relaxed);
                tracing::warn!(
                    "compact-2026-01-12 beta header rejected by Claude API (typed); \
                    disabling server-side compaction for this session. \
                    Update your config to set `server_compaction = false`."
                );
                return Err(LlmError::BetaHeaderRejected {
                    header: ANTHROPIC_BETA_COMPACT.into(),
                });
            }
            return Err(LlmError::Other(format!(
                "Claude API request failed (status {status})"
            )));
        }

        let resp: ToolApiResponse = serde_json::from_str(&text)?;

        if let Some(ref usage) = resp.usage {
            log_cache_usage(usage);
            self.store_cache_usage(usage);
        }

        for block in resp.content {
            if let AnthropicContentBlock::ToolUse { input, .. } = block {
                return serde_json::from_value::<T>(input)
                    .map_err(|e| LlmError::StructuredParse(e.to_string()));
            }
        }

        Err(LlmError::StructuredParse(
            "no tool_use block in response".into(),
        ))
    }

    fn supports_vision(&self) -> bool {
        true
    }

    fn last_cache_usage(&self) -> Option<(u64, u64)> {
        self.usage.last_cache_usage()
    }

    fn last_usage(&self) -> Option<(u64, u64)> {
        self.usage.last_usage()
    }

    fn take_compaction_summary(&self) -> Option<String> {
        ClaudeProvider::take_compaction_summary(self)
    }

    fn debug_request_json(
        &self,
        messages: &[Message],
        tools: &[ToolDefinition],
        stream: bool,
    ) -> serde_json::Value {
        let (thinking_param, mut temperature, effort) = self.build_thinking_param();
        if thinking_param.is_none()
            && let Some(Some(t)) = self.generation_overrides.as_ref().map(|ov| ov.temperature)
        {
            temperature = Some(t);
        }
        let output_config = effort.map(|e| OutputConfig { effort: e });

        if !tools.is_empty() {
            let (system, mut chat_messages) =
                split_messages_structured(messages, self.cache_user_messages);
            let system_blocks = system.map(|s| split_system_into_blocks(&s, &self.model));
            Self::cap_block_cache_controls(1, system_blocks.as_deref(), Some(&mut chat_messages));
            let api_tools = self.get_or_build_api_tools(tools);
            let body = ToolRequestBody {
                model: &self.model,
                max_tokens: self.max_tokens,
                system: system_blocks,
                messages: &chat_messages,
                tools: &api_tools,
                thinking: thinking_param,
                output_config,
                temperature,
                context_management: self.context_management(),
            };
            return serde_json::to_value(&body)
                .unwrap_or_else(|e| serde_json::json!({ "serialization_error": e.to_string() }));
        }

        if Self::has_image_parts(messages) {
            let (system, mut chat_messages) =
                split_messages_structured(messages, self.cache_user_messages);
            let system_blocks = system.map(|s| split_system_into_blocks(&s, &self.model));
            Self::cap_block_cache_controls(0, system_blocks.as_deref(), Some(&mut chat_messages));
            let body = VisionRequestBody {
                model: &self.model,
                max_tokens: self.max_tokens,
                system: system_blocks,
                messages: &chat_messages,
                stream,
                thinking: thinking_param,
                output_config,
                temperature,
                context_management: self.context_management(),
            };
            return serde_json::to_value(&body)
                .unwrap_or_else(|e| serde_json::json!({ "serialization_error": e.to_string() }));
        }

        let (system, chat_messages) = split_messages(messages);
        let system_blocks = system.map(|s| split_system_into_blocks(&s, &self.model));
        let body = RequestBody {
            model: &self.model,
            max_tokens: self.max_tokens,
            system: system_blocks,
            messages: &chat_messages,
            stream,
            thinking: thinking_param,
            output_config,
            temperature,
            context_management: self.context_management(),
        };
        serde_json::to_value(&body)
            .unwrap_or_else(|e| serde_json::json!({ "serialization_error": e.to_string() }))
    }

    async fn chat_with_tools(
        &self,
        messages: &[Message],
        tools: &[ToolDefinition],
    ) -> Result<ChatResponse, LlmError> {
        let (system, mut chat_messages) =
            split_messages_structured(messages, self.cache_user_messages);
        let api_tools = self.get_or_build_api_tools(tools);

        let (thinking_param, mut temperature, effort) = self.build_thinking_param();
        if thinking_param.is_none()
            && let Some(Some(t)) = self.generation_overrides.as_ref().map(|ov| ov.temperature)
        {
            temperature = Some(t);
        }
        let output_config = effort.map(|e| OutputConfig { effort: e });
        let system_blocks = system.map(|s| split_system_into_blocks(&s, &self.model));
        Self::cap_block_cache_controls(1, system_blocks.as_deref(), Some(&mut chat_messages));
        let beta = self.beta_header(!tools.is_empty());
        let body = ToolRequestBody {
            model: &self.model,
            max_tokens: self.max_tokens,
            system: system_blocks,
            messages: &chat_messages,
            tools: &api_tools,
            thinking: thinking_param,
            output_config,
            temperature,
            context_management: self.context_management(),
        };

        let response = send_with_retry("Claude", MAX_RETRIES, self.status_tx.as_ref(), || {
            let mut req = self
                .client
                .post(API_URL)
                .header("x-api-key", &self.api_key)
                .header("anthropic-version", ANTHROPIC_VERSION);
            if let Some(ref b) = beta {
                req = req.header("anthropic-beta", b);
            }
            req.header("content-type", "application/json")
                .json(&body)
                .send()
        })
        .await?;

        let status = response.status();
        let text = response.text().await.map_err(LlmError::Http)?;

        if !status.is_success() {
            if Self::is_compact_beta_rejection(status, &text) {
                self.server_compaction_rejected
                    .store(true, Ordering::Relaxed);
                tracing::warn!(
                    "compact-2026-01-12 beta header rejected by Claude API (tool use); \
                    disabling server-side compaction for this session. \
                    Update your config to set `server_compaction = false`."
                );
                return Err(LlmError::BetaHeaderRejected {
                    header: ANTHROPIC_BETA_COMPACT.into(),
                });
            }
            tracing::error!("Claude API error {status}: {text}");
            return Err(LlmError::Other(format!(
                "Claude API request failed (status {status})"
            )));
        }

        let resp: ToolApiResponse = serde_json::from_str(&text)?;
        tracing::debug!(
            stop_reason = ?resp.stop_reason,
            content_blocks = resp.content.len(),
            "Claude chat_with_tools response"
        );
        if let Some(ref usage) = resp.usage {
            log_cache_usage(usage);
            self.store_cache_usage(usage);
        }
        let (parsed, compaction_summary) = parse_tool_response(resp);
        if let Some(ref summary) = compaction_summary {
            tracing::info!(
                summary_len = summary.len(),
                "storing server compaction summary"
            );
            *self.last_compaction.lock() = compaction_summary;
        }
        tracing::debug!(?parsed, "parsed ChatResponse");
        Ok(parsed)
    }
}