octolib 0.4.2

Self-sufficient AI provider library with multi-provider support, embedding models, model validation, and cost tracking
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
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Anthropic provider implementation

use crate::llm::retry;
use crate::llm::traits::AiProvider;
use crate::llm::types::{
    ChatCompletionParams, Message, ProviderExchange, ProviderResponse, TokenUsage, ToolCall,
};
use crate::llm::utils::normalize_model_name;
use anyhow::Result;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::env;

/// Anthropic pricing constants (per 1M tokens in USD)
/// Source: https://docs.anthropic.com/en/docs/about-claude/pricing (as of Jan 2026)
const PRICING: &[(&str, f64, f64)] = &[
    // Model, Input price per 1M tokens, Output price per 1M tokens
    // Claude 4.5 (Latest - Jan 2026)
    ("claude-opus-4-5", 5.00, 25.00), // 67% cheaper than Opus 4.1, double max output tokens
    ("claude-haiku-4-5", 1.00, 5.00),
    ("claude-sonnet-4-5", 3.00, 15.00), // Extended context: $6/$15 for >200K, $22.50 for >200K output
    // Claude 4 (Released May 22, 2025)
    ("claude-opus-4-1", 15.00, 75.00),
    ("claude-opus-4-0", 15.00, 75.00),
    ("claude-opus-4", 15.00, 75.00),
    ("claude-sonnet-4-0", 3.00, 15.00),
    ("claude-sonnet-4", 3.00, 15.00),
    // Claude 3.7
    ("claude-3-7-sonnet", 3.00, 15.00),
    // Claude 3.5
    ("claude-3-5-sonnet", 3.00, 15.00),
    ("claude-3-5-haiku", 0.80, 4.00),
    // Claude 3
    ("claude-3-opus", 15.00, 75.00),
    ("claude-3-sonnet", 3.00, 15.00),
    ("claude-3-haiku", 0.25, 1.25),
];

/// Token usage breakdown for cache-aware pricing
struct CacheTokenUsage {
    regular_input_tokens: u64,
    cache_creation_tokens: u64,
    cache_creation_tokens_1h: u64, // 1h TTL cache creation tokens (2x price)
    cache_read_tokens: u64,
    output_tokens: u64,
}

/// Check if a model supports temperature parameter
/// All Claude models support temperature except opus-4-1
fn supports_temperature_and_top_p(model: &str) -> bool {
    let unsupported_prefixes = [
        "opus-4-1",
        "sonnet-4-5",
        "claude-haiku-4-5",
        "claude-opus-4-5",
    ];

    !unsupported_prefixes
        .iter()
        .any(|prefix| model.contains(prefix))
}

/// Calculate cost for Anthropic models with cache-aware pricing
/// - cache_creation_tokens: charged at 1.25x normal price (5m cache)
/// - cache_creation_tokens_1h: charged at 2x normal price (1h cache)
/// - cache_read_tokens: charged at 0.1x normal price
/// - regular_input_tokens: charged at normal price
/// - output_tokens: charged at normal price
fn calculate_cost_with_cache(model: &str, usage: CacheTokenUsage) -> Option<f64> {
    for (pricing_model, input_price, output_price) in PRICING {
        if model.contains(pricing_model) {
            // Regular input tokens at normal price
            let regular_input_cost =
                (usage.regular_input_tokens as f64 / 1_000_000.0) * input_price;

            // Cache creation tokens at 1.25x price (25% more expensive) for 5m cache
            let cache_creation_cost =
                (usage.cache_creation_tokens as f64 / 1_000_000.0) * input_price * 1.25;

            // Cache creation tokens at 2x price (100% more expensive) for 1h cache
            let cache_creation_cost_1h =
                (usage.cache_creation_tokens_1h as f64 / 1_000_000.0) * input_price * 2.0;

            // Cache read tokens at 0.1x price (90% cheaper)
            let cache_read_cost =
                (usage.cache_read_tokens as f64 / 1_000_000.0) * input_price * 0.1;

            // Output tokens at normal price (never cached)
            let output_cost = (usage.output_tokens as f64 / 1_000_000.0) * output_price;

            let total_cost = regular_input_cost
                + cache_creation_cost
                + cache_creation_cost_1h
                + cache_read_cost
                + output_cost;

            return Some(total_cost);
        }
    }
    None
}

/// Simplified cost calculation for Anthropic models with cache support
/// This is used by the helper function for individual token counts
fn calculate_anthropic_cost(
    model: &str,
    input_tokens: u32,
    output_tokens: u32,
    cache_creation_input_tokens: u32,
    cache_read_input_tokens: u32,
) -> Option<f64> {
    // Calculate cache creation tokens for 1h (these are charged at 2x)
    let cache_creation_1h_tokens = if cache_creation_input_tokens > 0 {
        // Assume all cache creation is for 1h TTL (more expensive)
        cache_creation_input_tokens
    } else {
        0
    };

    // Calculate regular input tokens (total input minus cache tokens)
    let regular_input_tokens =
        input_tokens.saturating_sub(cache_creation_input_tokens + cache_read_input_tokens);

    let usage = CacheTokenUsage {
        regular_input_tokens: regular_input_tokens as u64,
        cache_creation_tokens: 0, // Using 1h cache creation instead
        cache_creation_tokens_1h: cache_creation_1h_tokens as u64,
        cache_read_tokens: cache_read_input_tokens as u64,
        output_tokens: output_tokens as u64,
    };

    calculate_cost_with_cache(model, usage)
}

/// Anthropic provider
#[derive(Debug, Clone)]
pub struct AnthropicProvider;

impl Default for AnthropicProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl AnthropicProvider {
    pub fn new() -> Self {
        Self
    }
}

// Constants
const ANTHROPIC_API_KEY_ENV: &str = "ANTHROPIC_API_KEY";
const ANTHROPIC_OAUTH_TOKEN_ENV: &str = "ANTHROPIC_OAUTH_ACCESS_TOKEN";
const ANTHROPIC_API_URL_ENV: &str = "ANTHROPIC_API_URL";
const ANTHROPIC_API_URL: &str = "https://api.anthropic.com/v1/messages";

#[async_trait::async_trait]
impl AiProvider for AnthropicProvider {
    fn name(&self) -> &str {
        "anthropic"
    }

    fn supports_model(&self, model: &str) -> bool {
        // Anthropic Claude models (case-insensitive)
        let normalized = normalize_model_name(model);
        normalized.starts_with("claude-") || normalized.contains("claude")
    }

    fn get_api_key(&self) -> Result<String> {
        // Check for OAuth token first (priority)
        if env::var(ANTHROPIC_OAUTH_TOKEN_ENV).is_ok() {
            return Err(anyhow::anyhow!(
                "Using OAuth authentication. API key not available when {} is set.",
                ANTHROPIC_OAUTH_TOKEN_ENV
            ));
        }

        // Fall back to API key
        match env::var(ANTHROPIC_API_KEY_ENV) {
            Ok(key) => Ok(key),
            Err(_) => Err(anyhow::anyhow!(
                "Anthropic API key not found in environment variable: {}. Set either {} for API key auth or {} for OAuth.",
                ANTHROPIC_API_KEY_ENV,
                ANTHROPIC_API_KEY_ENV,
                ANTHROPIC_OAUTH_TOKEN_ENV
            )),
        }
    }

    fn supports_caching(&self, _model: &str) -> bool {
        true
    }

    fn supports_vision(&self, model: &str) -> bool {
        // Claude 3+ models support vision (case-insensitive)
        let model_lower = normalize_model_name(model);
        model_lower.contains("claude-3")
            || model_lower.contains("claude-4")
            || model_lower.contains("claude-3.5")
            || model_lower.contains("claude-3.7")
    }

    fn get_max_input_tokens(&self, model: &str) -> usize {
        // Anthropic model context window limits (case-insensitive)
        let model_lower = normalize_model_name(model);
        if model_lower.contains("claude-opus-4")
            || model_lower.contains("claude-sonnet-4")
            || model_lower.contains("claude-haiku-4")
        {
            // Claude 4 and 4.5 models have 200k context
            200_000
        } else if model_lower.contains("claude-3-7") {
            // Claude 3.7 has 200k context
            200_000
        } else if model_lower.contains("claude-3-5") {
            // Claude 3.5 models have 200k context
            200_000
        } else if model_lower.contains("claude-3") {
            // Claude 3 models have 200k context
            200_000
        } else {
            // Default fallback for older models
            100_000
        }
    }

    async fn chat_completion(&self, params: ChatCompletionParams) -> Result<ProviderResponse> {
        // Check for OAuth token first (priority), otherwise use API key
        let (auth_header_name, auth_header_value) =
            if let Ok(oauth_token) = env::var(ANTHROPIC_OAUTH_TOKEN_ENV) {
                (
                    "Authorization".to_string(),
                    format!("Bearer {}", oauth_token),
                )
            } else {
                let api_key = self.get_api_key()?;
                ("x-api-key".to_string(), api_key)
            };

        // Convert messages to Anthropic format
        let anthropic_messages = convert_messages(&params.messages);

        // Extract system message if present
        let system_message = params
            .messages
            .iter()
            .find(|m| m.role == "system")
            .map(|m| m.content.clone())
            .unwrap_or_else(|| "You are a helpful assistant.".to_string());

        let system_cached = params
            .messages
            .iter()
            .any(|m| m.role == "system" && m.cached);

        // Create the request body
        let mut request_body = serde_json::json!({
            "model": params.model,
            "messages": anthropic_messages,
        });
        request_body["temperature"] = serde_json::json!(params.temperature);

        // Opus 4.1 doesn't support using temperature and top_p together, so we do this instead
        if supports_temperature_and_top_p(&params.model) {
            request_body["top_p"] = serde_json::json!(params.top_p);
        }

        request_body["top_k"] = serde_json::json!(params.top_k);

        // Add max_tokens if specified (0 means don't include it in request)
        if params.max_tokens > 0 {
            request_body["max_tokens"] = serde_json::json!(params.max_tokens);
        }

        // Add system message with cache control if needed
        if system_cached {
            let cache_ttl = crate::llm::config::CacheTTL::short();
            request_body["system"] = serde_json::json!([{
                "type": "text",
                "text": system_message,
                "cache_control": {
                    "type": "ephemeral",
                    "ttl": cache_ttl.to_string()
                }
            }]);
        } else {
            request_body["system"] = serde_json::json!(system_message);
        }

        // Add tools if available (Anthropic format)
        if let Some(tools) = &params.tools {
            if !tools.is_empty() {
                // Sort tools by name for consistent ordering
                let mut sorted_tools = tools.clone();
                sorted_tools.sort_by(|a, b| a.name.cmp(&b.name));

                let anthropic_tools = sorted_tools
                    .iter()
                    .map(|f| {
                        let mut tool = serde_json::json!({
                            "name": f.name,
                            "description": f.description,
                            "input_schema": f.parameters
                        });

                        // Add cache control if present
                        if let Some(ref cache_control) = f.cache_control {
                            tool["cache_control"] = cache_control.clone();
                        }

                        tool
                    })
                    .collect::<Vec<_>>();

                request_body["tools"] = serde_json::json!(anthropic_tools);
            }
        }

        // Execute the request with retry logic
        let api_url =
            env::var(ANTHROPIC_API_URL_ENV).unwrap_or_else(|_| ANTHROPIC_API_URL.to_string());

        let response = execute_anthropic_request(
            auth_header_name,
            auth_header_value,
            api_url,
            request_body,
            params.max_retries,
            params.retry_timeout,
            params.cancellation_token.as_ref(),
        )
        .await?;

        Ok(response)
    }
}

// Anthropic API structures
#[derive(Serialize, Deserialize, Debug)]
struct AnthropicMessage {
    role: String,
    content: Vec<AnthropicContent>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
enum AnthropicContent {
    #[serde(rename = "text")]
    Text {
        text: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<serde_json::Value>,
    },
    #[serde(rename = "image")]
    Image {
        source: ImageSource,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<serde_json::Value>,
    },
    #[serde(rename = "tool_result")]
    ToolResult {
        tool_use_id: String,
        content: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<serde_json::Value>,
    },
    #[serde(rename = "tool_use")]
    ToolUse {
        id: String,
        name: String,
        input: serde_json::Value,
    },
}

#[derive(Serialize, Deserialize, Debug)]
struct ImageSource {
    #[serde(rename = "type")]
    source_type: String,
    media_type: String,
    data: String,
}

#[derive(Deserialize, Debug)]
struct AnthropicResponse {
    content: Vec<AnthropicResponseContent>,
    usage: AnthropicUsage,
    #[serde(default)]
    stop_reason: Option<String>,
}

#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
enum AnthropicResponseContent {
    #[serde(rename = "text")]
    Text { text: String },
    #[serde(rename = "tool_use")]
    ToolUse {
        id: String,
        name: String,
        input: serde_json::Value,
    },
}

#[derive(Deserialize, Debug)]
struct AnthropicUsage {
    input_tokens: u64,
    output_tokens: u64,
    #[serde(default)]
    cache_creation_input_tokens: Option<u64>,
    #[serde(default)]
    cache_read_input_tokens: Option<u64>,
}

// Convert our session messages to Anthropic format
fn convert_messages(messages: &[Message]) -> Vec<AnthropicMessage> {
    let mut result = Vec::new();

    for message in messages {
        // Skip system messages - they're handled separately
        if message.role == "system" {
            continue;
        }

        match message.role.as_str() {
            "tool" => {
                // Tool messages must be converted to user role with tool_result content
                let tool_call_id = message.tool_call_id.as_deref().unwrap_or("");

                let content = vec![AnthropicContent::ToolResult {
                    tool_use_id: tool_call_id.to_string(),
                    content: message.content.clone(),
                    cache_control: if message.cached {
                        Some(serde_json::json!({"type": "ephemeral"}))
                    } else {
                        None
                    },
                }];

                result.push(AnthropicMessage {
                    role: "user".to_string(), // Tool messages become user messages
                    content,
                });
            }
            _ => {
                // Handle user and assistant messages
                if message.role == "assistant" && message.tool_calls.is_some() {
                    // Assistant message with tool calls - reconstruct tool_use blocks
                    let mut content = Vec::new();

                    // Add text content if not empty
                    if !message.content.trim().is_empty() {
                        content.push(AnthropicContent::Text {
                            text: message.content.clone(),
                            cache_control: if message.cached {
                                Some(serde_json::json!({"type": "ephemeral"}))
                            } else {
                                None
                            },
                        });
                    }

                    // Add tool_use blocks from stored tool_calls in unified GenericToolCall format
                    if let Some(ref tool_calls_data) = message.tool_calls {
                        // Parse as unified GenericToolCall format
                        if let Ok(generic_calls) = serde_json::from_value::<
                            Vec<crate::llm::tool_calls::GenericToolCall>,
                        >(tool_calls_data.clone())
                        {
                            // Convert GenericToolCall to Anthropic format
                            for call in generic_calls {
                                content.push(AnthropicContent::ToolUse {
                                    id: call.id,
                                    name: call.name,
                                    input: call.arguments,
                                });
                            }
                        }
                    }

                    result.push(AnthropicMessage {
                        role: message.role.clone(),
                        content,
                    });
                } else {
                    // Handle regular user and assistant messages
                    let mut content = vec![AnthropicContent::Text {
                        text: message.content.clone(),
                        cache_control: if message.cached {
                            Some(serde_json::json!({"type": "ephemeral"}))
                        } else {
                            None
                        },
                    }];

                    // Add images if present
                    if let Some(images) = &message.images {
                        for image in images {
                            if let crate::llm::types::ImageData::Base64(data) = &image.data {
                                content.push(AnthropicContent::Image {
                                    source: ImageSource {
                                        source_type: "base64".to_string(),
                                        media_type: image.media_type.clone(),
                                        data: data.clone(),
                                    },
                                    cache_control: None,
                                });
                            }
                        }
                    }

                    result.push(AnthropicMessage {
                        role: message.role.clone(),
                        content,
                    });
                }
            }
        }
    }

    result
}

// Execute a single Anthropic HTTP request with smart retry delay calculation
async fn execute_anthropic_request(
    auth_header_name: String,
    auth_header_value: String,
    api_url: String,
    request_body: serde_json::Value,
    max_retries: u32,
    base_timeout: std::time::Duration,
    cancellation_token: Option<&tokio::sync::watch::Receiver<bool>>,
) -> Result<ProviderResponse> {
    let client = Client::new();
    let start_time = std::time::Instant::now();

    let response = retry::retry_with_exponential_backoff(
        || {
            let client = client.clone();
            let auth_header_name = auth_header_name.clone();
            let auth_header_value = auth_header_value.clone();
            let api_url = api_url.clone();
            let request_body = request_body.clone();
            Box::pin(async move {
                client
                    .post(&api_url)
                    .header("Content-Type", "application/json")
                    .header(&auth_header_name, &auth_header_value)
                    .header("anthropic-version", "2023-06-01")
                    .header("anthropic-beta", "prompt-caching-2024-07-31")
                    .json(&request_body)
                    .send()
                    .await
            })
        },
        max_retries,
        base_timeout,
        cancellation_token,
    )
    .await?;

    let request_time_ms = start_time.elapsed().as_millis() as u64;

    // Extract rate limit headers before consuming response
    let mut rate_limit_headers = std::collections::HashMap::new();
    let headers = response.headers();

    // Anthropic rate limit headers
    if let Some(tokens_limit) = headers
        .get("anthropic-ratelimit-tokens-limit")
        .and_then(|h| h.to_str().ok())
    {
        rate_limit_headers.insert("tokens_limit".to_string(), tokens_limit.to_string());
    }
    if let Some(tokens_remaining) = headers
        .get("anthropic-ratelimit-tokens-remaining")
        .and_then(|h| h.to_str().ok())
    {
        rate_limit_headers.insert("tokens_remaining".to_string(), tokens_remaining.to_string());
    }
    if let Some(input_tokens_limit) = headers
        .get("anthropic-ratelimit-input-tokens-limit")
        .and_then(|h| h.to_str().ok())
    {
        rate_limit_headers.insert(
            "input_tokens_limit".to_string(),
            input_tokens_limit.to_string(),
        );
    }
    if let Some(input_tokens_remaining) = headers
        .get("anthropic-ratelimit-input-tokens-remaining")
        .and_then(|h| h.to_str().ok())
    {
        rate_limit_headers.insert(
            "input_tokens_remaining".to_string(),
            input_tokens_remaining.to_string(),
        );
    }
    if let Some(output_tokens_limit) = headers
        .get("anthropic-ratelimit-output-tokens-limit")
        .and_then(|h| h.to_str().ok())
    {
        rate_limit_headers.insert(
            "output_tokens_limit".to_string(),
            output_tokens_limit.to_string(),
        );
    }
    if let Some(output_tokens_remaining) = headers
        .get("anthropic-ratelimit-output-tokens-remaining")
        .and_then(|h| h.to_str().ok())
    {
        rate_limit_headers.insert(
            "output_tokens_remaining".to_string(),
            output_tokens_remaining.to_string(),
        );
    }

    if !response.status().is_success() {
        let status = response.status();
        let error_text = response.text().await.unwrap_or_default();
        return Err(anyhow::anyhow!(
            "Anthropic API error {}: {}",
            status,
            error_text
        ));
    }

    let response_text = response.text().await?;
    let anthropic_response: AnthropicResponse = serde_json::from_str(&response_text)?;

    // Extract content and tool calls
    let mut content_parts = Vec::new();
    let mut tool_calls = Vec::new();

    for content in anthropic_response.content {
        match content {
            AnthropicResponseContent::Text { text } => {
                content_parts.push(text);
            }
            AnthropicResponseContent::ToolUse { id, name, input } => {
                // Create generic ToolCall for processing
                tool_calls.push(ToolCall {
                    id: id.clone(),
                    name: name.clone(),
                    arguments: input,
                });
            }
        }
    }

    let content = content_parts.join("\n");

    // Calculate cost with proper cache pricing
    let cached_tokens = anthropic_response
        .usage
        .cache_read_input_tokens
        .unwrap_or(0);

    let cache_creation_tokens = anthropic_response
        .usage
        .cache_creation_input_tokens
        .unwrap_or(0);

    let cost = calculate_anthropic_cost(
        request_body["model"].as_str().unwrap_or(""),
        anthropic_response.usage.input_tokens as u32,
        anthropic_response.usage.output_tokens as u32,
        cache_creation_tokens as u32,
        cached_tokens as u32,
    );

    let usage = TokenUsage {
        prompt_tokens: anthropic_response.usage.input_tokens,
        output_tokens: anthropic_response.usage.output_tokens,
        reasoning_tokens: 0, // Anthropic doesn't provide reasoning token count
        total_tokens: anthropic_response.usage.input_tokens
            + anthropic_response.usage.output_tokens,
        cached_tokens,
        cost,
        request_time_ms: Some(request_time_ms),
    };

    // Create response JSON that stores tool_calls in unified GenericToolCall format
    let mut response_json: serde_json::Value = serde_json::from_str(&response_text)?;

    // Store tool_calls in unified GenericToolCall format for conversation history
    if !tool_calls.is_empty() {
        let generic_calls: Vec<crate::llm::tool_calls::GenericToolCall> = tool_calls
            .iter()
            .map(|tc| crate::llm::tool_calls::GenericToolCall {
                id: tc.id.clone(),
                name: tc.name.clone(),
                arguments: tc.arguments.clone(),
                meta: None, // Anthropic doesn't use meta fields
            })
            .collect();

        response_json["tool_calls"] = serde_json::to_value(&generic_calls).unwrap_or_default();
    }

    let exchange = if rate_limit_headers.is_empty() {
        ProviderExchange::new(request_body, response_json, Some(usage), "anthropic")
    } else {
        ProviderExchange::with_rate_limit_headers(
            request_body,
            response_json,
            Some(usage),
            "anthropic",
            rate_limit_headers,
        )
    };

    Ok(ProviderResponse {
        content,
        thinking: None, // Anthropic doesn't support thinking
        exchange,
        tool_calls: if tool_calls.is_empty() {
            None
        } else {
            Some(tool_calls)
        },
        finish_reason: anthropic_response.stop_reason,
        structured_output: None, // Anthropic doesn't support structured output
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;

    #[test]
    #[serial]
    fn test_oauth_token_priority() {
        let provider = AnthropicProvider::new();

        // Set OAuth token
        env::set_var(ANTHROPIC_OAUTH_TOKEN_ENV, "test-oauth-token");

        // get_api_key should return error when OAuth is set
        let result = provider.get_api_key();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("OAuth authentication"));

        // Clean up
        env::remove_var(ANTHROPIC_OAUTH_TOKEN_ENV);
    }

    #[test]
    #[serial]
    fn test_api_key_fallback() {
        let provider = AnthropicProvider::new();

        // Remove OAuth token if set
        env::remove_var(ANTHROPIC_OAUTH_TOKEN_ENV);

        // Set API key
        env::set_var(ANTHROPIC_API_KEY_ENV, "test-api-key");

        // get_api_key should return the API key
        let result = provider.get_api_key();
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "test-api-key");

        // Clean up
        env::remove_var(ANTHROPIC_API_KEY_ENV);
    }

    #[test]
    #[serial]
    fn test_no_auth_error() {
        let provider = AnthropicProvider::new();

        // Remove both OAuth and API key
        env::remove_var(ANTHROPIC_OAUTH_TOKEN_ENV);
        env::remove_var(ANTHROPIC_API_KEY_ENV);

        // get_api_key should return error
        let result = provider.get_api_key();
        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(
            error_msg.contains("ANTHROPIC_API_KEY") || error_msg.contains("ANTHROPIC_OAUTH_TOKEN")
        );
    }

    #[test]
    fn test_supports_model_case_insensitive() {
        let provider = AnthropicProvider::new();

        // Test lowercase (already working)
        assert!(provider.supports_model("claude-3-haiku"));
        assert!(provider.supports_model("claude-3-5-sonnet"));

        // Test uppercase
        assert!(provider.supports_model("CLAUDE-3-HAIKU"));
        assert!(provider.supports_model("CLAUDE-3-5-SONNET"));
        // Test mixed case
        assert!(provider.supports_model("ClaUde-3-Haiku"));
        assert!(provider.supports_model("CLAUDE-3-7-sonnet"));
    }

    #[test]
    fn test_supports_vision_case_insensitive() {
        let provider = AnthropicProvider::new();

        // Test lowercase
        assert!(provider.supports_vision("claude-3-haiku"));
        assert!(provider.supports_vision("claude-3-5-sonnet"));

        // Test uppercase
        assert!(provider.supports_vision("CLAUDE-3-HAIKU"));
        assert!(provider.supports_vision("CLAUDE-3-5-SONNET"));
        // Test mixed case
        assert!(provider.supports_vision("ClaUde-3-7"));
    }
}