edgequake-llm 0.5.1

Multi-provider LLM abstraction library with caching, rate limiting, 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
//! LLM response caching for reducing API costs and latency.
//!
//! This module provides a caching layer for LLM completions and embeddings,
//! significantly reducing costs for repeated queries and improving response times.
//!
//! ## Implements
//!
//! - **FEAT0019**: LLM Response Caching
//! - **FEAT0772**: LRU eviction policy
//! - **FEAT0773**: TTL-based expiration
//!
//! ## Enforces
//!
//! - **BR0772**: Cache hit does not modify original response
//! - **BR0773**: Expired entries evicted on next access
//!
//! Based on LightRAG's caching approach with an in-memory LRU cache.

use crate::error::Result;
use crate::traits::{ChatMessage, CompletionOptions, EmbeddingProvider, LLMProvider, LLMResponse};
use async_trait::async_trait;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Configuration for the LLM cache.
#[derive(Debug, Clone)]
pub struct CacheConfig {
    /// Maximum number of entries in the cache.
    pub max_entries: usize,
    /// Time-to-live for cache entries.
    pub ttl: Duration,
    /// Whether to cache completions.
    pub cache_completions: bool,
    /// Whether to cache embeddings.
    pub cache_embeddings: bool,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            max_entries: 1000,
            ttl: Duration::from_secs(3600), // 1 hour
            cache_completions: true,
            cache_embeddings: true,
        }
    }
}

impl CacheConfig {
    /// Create a new cache config with specified max entries.
    pub fn new(max_entries: usize) -> Self {
        Self {
            max_entries,
            ..Default::default()
        }
    }

    /// Set the TTL for cache entries.
    pub fn with_ttl(mut self, ttl: Duration) -> Self {
        self.ttl = ttl;
        self
    }

    /// Enable or disable completion caching.
    pub fn with_completion_caching(mut self, enabled: bool) -> Self {
        self.cache_completions = enabled;
        self
    }

    /// Enable or disable embedding caching.
    pub fn with_embedding_caching(mut self, enabled: bool) -> Self {
        self.cache_embeddings = enabled;
        self
    }
}

/// A cached entry with metadata.
#[derive(Debug, Clone)]
struct CacheEntry<T> {
    value: T,
    created_at: Instant,
    access_count: usize,
}

impl<T: Clone> CacheEntry<T> {
    fn new(value: T) -> Self {
        Self {
            value,
            created_at: Instant::now(),
            access_count: 0,
        }
    }

    fn is_expired(&self, ttl: Duration) -> bool {
        self.created_at.elapsed() > ttl
    }

    fn access(&mut self) -> T {
        self.access_count += 1;
        self.value.clone()
    }
}

/// Cache key derived from prompt/input.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct CacheKey {
    hash: u64,
}

impl CacheKey {
    fn from_prompt(prompt: &str) -> Self {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        prompt.hash(&mut hasher);
        Self {
            hash: hasher.finish(),
        }
    }

    fn from_texts(texts: &[&str]) -> Self {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        for text in texts {
            text.hash(&mut hasher);
        }
        Self {
            hash: hasher.finish(),
        }
    }
}

/// LLM cache statistics.
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Number of cache hits.
    pub hits: usize,
    /// Number of cache misses.
    pub misses: usize,
    /// Current number of entries.
    pub entries: usize,
    /// Number of evictions.
    pub evictions: usize,
}

impl CacheStats {
    /// Get the cache hit rate.
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        }
    }
}

/// In-memory LLM cache.
/// @implements FEAT0014
pub struct LLMCache {
    config: CacheConfig,
    completions: RwLock<HashMap<CacheKey, CacheEntry<LLMResponse>>>,
    embeddings: RwLock<HashMap<CacheKey, CacheEntry<Vec<Vec<f32>>>>>,
    stats: RwLock<CacheStats>,
}

impl LLMCache {
    /// Create a new LLM cache with the given configuration.
    pub fn new(config: CacheConfig) -> Self {
        Self {
            config,
            completions: RwLock::new(HashMap::new()),
            embeddings: RwLock::new(HashMap::new()),
            stats: RwLock::new(CacheStats::default()),
        }
    }

    /// Get cache statistics.
    pub async fn stats(&self) -> CacheStats {
        let stats = self.stats.read().await;
        let completions = self.completions.read().await;
        let embeddings = self.embeddings.read().await;

        CacheStats {
            entries: completions.len() + embeddings.len(),
            ..*stats
        }
    }

    /// Clear all cache entries.
    pub async fn clear(&self) {
        let mut completions = self.completions.write().await;
        let mut embeddings = self.embeddings.write().await;
        let mut stats = self.stats.write().await;

        let evicted = completions.len() + embeddings.len();
        completions.clear();
        embeddings.clear();
        stats.evictions += evicted;
    }

    /// Get a cached completion response.
    pub async fn get_completion(&self, prompt: &str) -> Option<LLMResponse> {
        if !self.config.cache_completions {
            return None;
        }

        let key = CacheKey::from_prompt(prompt);
        let mut cache = self.completions.write().await;

        if let Some(entry) = cache.get_mut(&key) {
            if entry.is_expired(self.config.ttl) {
                cache.remove(&key);
                let mut stats = self.stats.write().await;
                stats.misses += 1;
                stats.evictions += 1;
                return None;
            }

            let mut stats = self.stats.write().await;
            stats.hits += 1;
            return Some(entry.access());
        }

        let mut stats = self.stats.write().await;
        stats.misses += 1;
        None
    }

    /// Store a completion response in cache.
    pub async fn put_completion(&self, prompt: &str, response: LLMResponse) {
        if !self.config.cache_completions {
            return;
        }

        let key = CacheKey::from_prompt(prompt);
        let mut cache = self.completions.write().await;

        // Evict if at capacity
        if cache.len() >= self.config.max_entries {
            self.evict_lru(&mut cache).await;
        }

        cache.insert(key, CacheEntry::new(response));
    }

    /// Get cached embeddings.
    pub async fn get_embeddings(&self, texts: &[&str]) -> Option<Vec<Vec<f32>>> {
        if !self.config.cache_embeddings {
            return None;
        }

        let key = CacheKey::from_texts(texts);
        let mut cache = self.embeddings.write().await;

        if let Some(entry) = cache.get_mut(&key) {
            if entry.is_expired(self.config.ttl) {
                cache.remove(&key);
                let mut stats = self.stats.write().await;
                stats.misses += 1;
                stats.evictions += 1;
                return None;
            }

            let mut stats = self.stats.write().await;
            stats.hits += 1;
            return Some(entry.access());
        }

        let mut stats = self.stats.write().await;
        stats.misses += 1;
        None
    }

    /// Store embeddings in cache.
    pub async fn put_embeddings(&self, texts: &[&str], embeddings: Vec<Vec<f32>>) {
        if !self.config.cache_embeddings {
            return;
        }

        let key = CacheKey::from_texts(texts);
        let mut cache = self.embeddings.write().await;

        // Evict if at capacity
        if cache.len() >= self.config.max_entries {
            self.evict_lru_embeddings(&mut cache).await;
        }

        cache.insert(key, CacheEntry::new(embeddings));
    }

    async fn evict_lru<T: Clone>(&self, cache: &mut HashMap<CacheKey, CacheEntry<T>>) {
        // Find the least recently used entry (oldest with fewest accesses)
        if let Some(key) = cache
            .iter()
            .min_by_key(|(_, entry)| (entry.access_count, entry.created_at))
            .map(|(k, _)| k.clone())
        {
            cache.remove(&key);
            let mut stats = self.stats.write().await;
            stats.evictions += 1;
        }
    }

    async fn evict_lru_embeddings(&self, cache: &mut HashMap<CacheKey, CacheEntry<Vec<Vec<f32>>>>) {
        if let Some(key) = cache
            .iter()
            .min_by_key(|(_, entry)| (entry.access_count, entry.created_at))
            .map(|(k, _)| k.clone())
        {
            cache.remove(&key);
            let mut stats = self.stats.write().await;
            stats.evictions += 1;
        }
    }
}

/// A cached LLM provider that wraps another provider with caching.
pub struct CachedProvider<P> {
    inner: P,
    cache: Arc<LLMCache>,
}

impl<P> CachedProvider<P> {
    /// Create a new cached provider.
    pub fn new(inner: P, cache: Arc<LLMCache>) -> Self {
        Self { inner, cache }
    }

    /// Create with default cache config.
    pub fn with_default_cache(inner: P) -> Self {
        Self {
            inner,
            cache: Arc::new(LLMCache::new(CacheConfig::default())),
        }
    }

    /// Get cache statistics.
    pub async fn cache_stats(&self) -> CacheStats {
        self.cache.stats().await
    }

    /// Clear the cache.
    pub async fn clear_cache(&self) {
        self.cache.clear().await;
    }
}

#[async_trait]
impl<P: LLMProvider> LLMProvider for CachedProvider<P> {
    fn name(&self) -> &str {
        self.inner.name()
    }

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

    fn max_context_length(&self) -> usize {
        self.inner.max_context_length()
    }

    async fn complete(&self, prompt: &str) -> Result<LLMResponse> {
        // Check cache first
        if let Some(cached) = self.cache.get_completion(prompt).await {
            tracing::debug!("Cache hit for completion");
            return Ok(cached);
        }

        // Call underlying provider
        let response = self.inner.complete(prompt).await?;

        // Store in cache
        self.cache.put_completion(prompt, response.clone()).await;

        Ok(response)
    }

    async fn complete_with_options(
        &self,
        prompt: &str,
        options: &CompletionOptions,
    ) -> Result<LLMResponse> {
        // For options-based completions, we skip caching since options affect output
        self.inner.complete_with_options(prompt, options).await
    }

    async fn chat(
        &self,
        messages: &[ChatMessage],
        options: Option<&CompletionOptions>,
    ) -> Result<LLMResponse> {
        // For chat completions, we skip caching since message history varies
        self.inner.chat(messages, options).await
    }
}

#[async_trait]
impl<P: EmbeddingProvider> EmbeddingProvider for CachedProvider<P> {
    fn name(&self) -> &str {
        self.inner.name()
    }

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

    fn dimension(&self) -> usize {
        self.inner.dimension()
    }

    fn max_tokens(&self) -> usize {
        self.inner.max_tokens()
    }

    async fn embed(&self, texts: &[String]) -> Result<Vec<Vec<f32>>> {
        // Convert to &str for cache lookup
        let text_refs: Vec<&str> = texts.iter().map(|s| s.as_str()).collect();

        // Check cache first
        if let Some(cached) = self.cache.get_embeddings(&text_refs).await {
            tracing::debug!("Cache hit for embeddings");
            return Ok(cached);
        }

        // Call underlying provider
        let embeddings = self.inner.embed(texts).await?;

        // Store in cache
        self.cache
            .put_embeddings(&text_refs, embeddings.clone())
            .await;

        Ok(embeddings)
    }
}

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

    #[test]
    fn test_cache_key_from_prompt() {
        let key1 = CacheKey::from_prompt("Hello world");
        let key2 = CacheKey::from_prompt("Hello world");
        let key3 = CacheKey::from_prompt("Different prompt");

        assert_eq!(key1, key2);
        assert_ne!(key1, key3);
    }

    #[test]
    fn test_cache_key_from_texts() {
        let key1 = CacheKey::from_texts(&["a", "b", "c"]);
        let key2 = CacheKey::from_texts(&["a", "b", "c"]);
        let key3 = CacheKey::from_texts(&["x", "y", "z"]);

        assert_eq!(key1, key2);
        assert_ne!(key1, key3);
    }

    #[test]
    fn test_cache_config_default() {
        let config = CacheConfig::default();
        assert_eq!(config.max_entries, 1000);
        assert!(config.cache_completions);
        assert!(config.cache_embeddings);
    }

    #[test]
    fn test_cache_config_builder() {
        let config = CacheConfig::new(500)
            .with_ttl(Duration::from_secs(600))
            .with_completion_caching(false);

        assert_eq!(config.max_entries, 500);
        assert_eq!(config.ttl, Duration::from_secs(600));
        assert!(!config.cache_completions);
    }

    #[tokio::test]
    async fn test_cache_stats() {
        let cache = LLMCache::new(CacheConfig::default());
        let stats = cache.stats().await;

        assert_eq!(stats.hits, 0);
        assert_eq!(stats.misses, 0);
        assert_eq!(stats.entries, 0);
    }

    #[tokio::test]
    async fn test_cache_miss() {
        let cache = LLMCache::new(CacheConfig::default());
        let result = cache.get_completion("test prompt").await;

        assert!(result.is_none());

        let stats = cache.stats().await;
        assert_eq!(stats.misses, 1);
    }

    #[tokio::test]
    async fn test_cache_hit() {
        let cache = LLMCache::new(CacheConfig::default());

        let response = LLMResponse::new("test response", "gpt-4").with_usage(10, 5);

        cache.put_completion("test prompt", response.clone()).await;
        let result = cache.get_completion("test prompt").await;

        assert!(result.is_some());
        assert_eq!(result.unwrap().content, "test response");

        let stats = cache.stats().await;
        assert_eq!(stats.hits, 1);
    }

    #[tokio::test]
    async fn test_cache_clear() {
        let cache = LLMCache::new(CacheConfig::default());

        let response = LLMResponse::new("test", "gpt-4").with_usage(1, 1);

        cache.put_completion("prompt", response).await;
        assert_eq!(cache.stats().await.entries, 1);

        cache.clear().await;
        assert_eq!(cache.stats().await.entries, 0);
    }

    #[test]
    fn test_hit_rate() {
        let mut stats = CacheStats::default();
        assert_eq!(stats.hit_rate(), 0.0);

        stats.hits = 3;
        stats.misses = 1;
        assert_eq!(stats.hit_rate(), 0.75);
    }

    #[tokio::test]
    async fn test_embedding_cache() {
        let cache = LLMCache::new(CacheConfig::default());

        let embeddings = vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]];
        let texts = ["text1", "text2"];

        cache.put_embeddings(&texts, embeddings.clone()).await;
        let result = cache.get_embeddings(&texts).await;

        assert!(result.is_some());
        assert_eq!(result.unwrap(), embeddings);
    }

    #[tokio::test]
    async fn test_disabled_caching() {
        let config = CacheConfig::default()
            .with_completion_caching(false)
            .with_embedding_caching(false);
        let cache = LLMCache::new(config);

        let response = LLMResponse::new("test", "gpt-4").with_usage(1, 1);

        cache.put_completion("prompt", response).await;
        assert!(cache.get_completion("prompt").await.is_none());

        cache.put_embeddings(&["text"], vec![vec![1.0]]).await;
        assert!(cache.get_embeddings(&["text"]).await.is_none());
    }

    #[tokio::test]
    async fn test_ttl_expiration_completion() {
        // TTL of 1ms ensures entries expire quickly
        let config = CacheConfig::new(100).with_ttl(Duration::from_millis(1));
        let cache = LLMCache::new(config);

        let response = LLMResponse::new("expires", "gpt-4").with_usage(5, 3);
        cache.put_completion("ephemeral", response).await;

        // Wait for TTL to elapse
        tokio::time::sleep(Duration::from_millis(10)).await;

        let result = cache.get_completion("ephemeral").await;
        assert!(result.is_none(), "Expired entry should return None");

        let stats = cache.stats().await;
        assert_eq!(stats.evictions, 1, "Expired entry should count as eviction");
        assert_eq!(stats.misses, 1);
    }

    #[tokio::test]
    async fn test_ttl_expiration_embeddings() {
        let config = CacheConfig::new(100).with_ttl(Duration::from_millis(1));
        let cache = LLMCache::new(config);

        cache.put_embeddings(&["txt"], vec![vec![1.0, 2.0]]).await;

        tokio::time::sleep(Duration::from_millis(10)).await;

        assert!(cache.get_embeddings(&["txt"]).await.is_none());
        let stats = cache.stats().await;
        assert_eq!(stats.evictions, 1);
    }

    #[tokio::test]
    async fn test_lru_eviction_completions() {
        // Cache with max 2 entries
        let config = CacheConfig::new(2);
        let cache = LLMCache::new(config);

        let r1 = LLMResponse::new("first", "gpt-4").with_usage(1, 1);
        let r2 = LLMResponse::new("second", "gpt-4").with_usage(1, 1);
        let r3 = LLMResponse::new("third", "gpt-4").with_usage(1, 1);

        cache.put_completion("p1", r1).await;
        cache.put_completion("p2", r2).await;

        // Access p2 to bump its access count
        let _ = cache.get_completion("p2").await;

        // Inserting p3 should evict p1 (least recently used)
        cache.put_completion("p3", r3).await;

        assert!(
            cache.get_completion("p1").await.is_none(),
            "p1 should have been evicted"
        );
        // p2 and p3 should still exist
        assert!(cache.get_completion("p2").await.is_some());
        assert!(cache.get_completion("p3").await.is_some());
    }

    #[tokio::test]
    async fn test_lru_eviction_embeddings() {
        let config = CacheConfig::new(1);
        let cache = LLMCache::new(config);

        cache.put_embeddings(&["a"], vec![vec![1.0]]).await;
        cache.put_embeddings(&["b"], vec![vec![2.0]]).await;

        // "a" should have been evicted
        assert!(cache.get_embeddings(&["a"]).await.is_none());
        assert!(cache.get_embeddings(&["b"]).await.is_some());
    }

    #[tokio::test]
    async fn test_access_count_increments() {
        let cache = LLMCache::new(CacheConfig::default());
        let response = LLMResponse::new("counter", "gpt-4").with_usage(1, 1);

        cache.put_completion("cnt", response).await;

        // Access 3 times
        for _ in 0..3 {
            let _ = cache.get_completion("cnt").await;
        }

        let stats = cache.stats().await;
        assert_eq!(stats.hits, 3);
    }

    #[tokio::test]
    async fn test_cache_entry_is_expired() {
        let entry = CacheEntry::new("value".to_string());
        // Just-created entry with large TTL should not be expired
        assert!(!entry.is_expired(Duration::from_secs(3600)));
        // Entry with zero TTL should be expired
        assert!(entry.is_expired(Duration::ZERO));
    }

    #[tokio::test]
    async fn test_cached_provider_complete_delegates() {
        use crate::providers::MockProvider;

        let mock = MockProvider::new();
        mock.add_response("cached answer").await;

        let cache = Arc::new(LLMCache::new(CacheConfig::default()));
        let provider = CachedProvider::new(mock, cache);

        // First call: cache miss, delegates to inner
        let r1 = provider.inner.complete("hello").await.unwrap();
        assert_eq!(r1.content, "cached answer");
    }

    #[tokio::test]
    async fn test_cached_provider_name_model_delegates() {
        use crate::providers::MockProvider;

        let mock = MockProvider::new();
        let cache = Arc::new(LLMCache::new(CacheConfig::default()));
        let provider = CachedProvider::new(mock, cache);

        assert_eq!(LLMProvider::name(&provider), "mock");
        assert_eq!(LLMProvider::model(&provider), "mock-model");
        assert_eq!(provider.max_context_length(), 4096);
    }

    #[tokio::test]
    async fn test_cached_provider_with_default_cache() {
        use crate::providers::MockProvider;

        let mock = MockProvider::new();
        let provider = CachedProvider::with_default_cache(mock);

        let stats = provider.cache_stats().await;
        assert_eq!(stats.entries, 0);
        assert_eq!(stats.hits, 0);
    }

    #[tokio::test]
    async fn test_cached_provider_clear_cache() {
        use crate::providers::MockProvider;

        let mock = MockProvider::new();
        let cache = Arc::new(LLMCache::new(CacheConfig::default()));
        let provider = CachedProvider::new(mock, cache);

        // Put something in cache directly
        provider
            .cache
            .put_completion("test", LLMResponse::new("v", "m").with_usage(1, 1))
            .await;
        assert_eq!(provider.cache_stats().await.entries, 1);

        provider.clear_cache().await;
        assert_eq!(provider.cache_stats().await.entries, 0);
    }

    #[test]
    fn test_cache_key_empty_prompt() {
        let k1 = CacheKey::from_prompt("");
        let k2 = CacheKey::from_prompt("");
        assert_eq!(k1, k2);
    }

    #[test]
    fn test_cache_key_empty_texts() {
        let k = CacheKey::from_texts(&[]);
        let k2 = CacheKey::from_texts(&[]);
        assert_eq!(k, k2);
    }

    #[tokio::test]
    async fn test_multiple_put_same_key_overwrites() {
        let cache = LLMCache::new(CacheConfig::default());
        let r1 = LLMResponse::new("first", "m").with_usage(1, 1);
        let r2 = LLMResponse::new("second", "m").with_usage(1, 1);

        cache.put_completion("key", r1).await;
        cache.put_completion("key", r2).await;

        let result = cache.get_completion("key").await.unwrap();
        assert_eq!(result.content, "second");
        // Only 1 entry despite 2 puts to same key
        assert_eq!(cache.stats().await.entries, 1);
    }

    #[test]
    fn test_cache_config_with_embedding_caching() {
        let config = CacheConfig::default().with_embedding_caching(false);
        assert!(!config.cache_embeddings);
        assert!(config.cache_completions); // default true
    }

    #[tokio::test]
    async fn test_clear_updates_eviction_count() {
        let cache = LLMCache::new(CacheConfig::default());
        let r = LLMResponse::new("a", "m").with_usage(1, 1);
        cache.put_completion("x", r).await;
        cache.put_embeddings(&["y"], vec![vec![1.0]]).await;

        cache.clear().await;
        let stats = cache.stats().await;
        assert_eq!(stats.evictions, 2, "Clear should count 2 evictions");
        assert_eq!(stats.entries, 0);
    }

    #[tokio::test]
    async fn test_cached_provider_embed_delegates() {
        use crate::providers::MockProvider;

        let mock = MockProvider::new();
        let cache = Arc::new(LLMCache::new(CacheConfig::default()));
        let provider = CachedProvider::new(mock, cache);

        // First call goes to inner
        let result = provider.embed(&["hello".to_string()]).await.unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].len(), 1536);

        // Second call should hit cache
        let result2 = provider.embed(&["hello".to_string()]).await.unwrap();
        assert_eq!(result2, result);

        let stats = provider.cache_stats().await;
        assert_eq!(stats.hits, 1);
    }
}