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
mod private
{
//! Response caching functionality using LRU cache.
//!
//! This module provides application-level caching for XAI API responses,
//! which helps reduce API calls, lower costs, and improve response times
//! for repeated queries.
//!
//! # Design Decisions
//!
//! ## Why Application-Level Caching?
//!
//! The XAI Grok API does not provide server-side prompt caching like some
//! other LLM providers. This makes client-side caching essential for:
//!
//! 1. **Cost Reduction**: Avoid redundant API calls for identical requests
//! 2. **Performance**: Instant responses for cached queries
//! 3. **Rate Limiting**: Reduce pressure on API rate limits
//! 4. **Offline Capability**: Serve cached responses when API is unavailable
//!
//! ## Why LRU (Least Recently Used)?
//!
//! LRU eviction policy is ideal for chat applications because:
//!
//! 1. **Temporal Locality**: Recent queries are more likely to repeat
//! 2. **Bounded Memory**: Automatic eviction prevents unbounded growth
//! 3. **Simplicity**: No complex TTL management needed
//! 4. **Performance**: O(1) get/put operations
//!
//! ## Alternatives Considered
//!
//! - **TTL-based cache**: Adds complexity, doesn't align with usage patterns
//! - **Unlimited cache**: Memory leak risk for long-running applications
//! - **Disk-backed cache**: Violates "Thin Client" principle (no persistence)
//!
//! ## Cache Key Strategy
//!
//! Cache keys are computed from the serialized JSON request. This ensures:
//!
//! 1. **Correctness**: Different requests never collide
//! 2. **Completeness**: All request parameters affect caching
//! 3. **Determinism**: Same request always produces same key
//!
//! **Note**: Streaming requests are NOT cached (responses are incremental).
use crate::{ ChatCompletionRequest, ChatCompletionResponse, Client, XaiEnvironment, ClientApiAccessors };
use crate::error::Result;
use std::sync::{ Arc, Mutex };
use std::num::NonZeroUsize;
#[ cfg( feature = "caching" ) ]
use lru::LruCache;
#[ cfg( feature = "caching" ) ]
use std::hash::{ Hash, Hasher };
#[ cfg( feature = "caching" ) ]
use std::collections::hash_map::DefaultHasher;
/// A client wrapper that caches chat completion responses.
///
/// Wraps a standard `Client` and adds LRU caching for non-streaming
/// chat completion requests. Streaming requests bypass the cache.
///
/// # Cache Key
///
/// Cache keys are computed from the serialized JSON of the request.
/// This ensures that any change to the request (model, messages,
/// temperature, etc.) produces a different cache key.
///
/// # Thread Safety
///
/// The cache is protected by a `Mutex` and can be safely shared
/// across threads via `Arc`.
///
/// # Examples
///
/// ```no_run
/// # #[ cfg( feature = "caching") ]
/// # {
/// use api_xai::{ CachedClient, Client, Secret, XaiEnvironmentImpl, ChatCompletionRequest, Message };
///
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// let secret = Secret::new( "xai-key".to_string() )?;
/// let env = XaiEnvironmentImpl::new( secret )?;
/// let client = Client::build( env )?;
///
/// // Wrap with cache (capacity : 100 responses)
/// let cached_client = CachedClient::new( client, 100 );
///
/// let request = ChatCompletionRequest::former()
/// .model( "grok-2-1212".to_string() )
/// .messages( vec![ Message::user( "What is Rust?" ) ] )
/// .form();
///
/// // First call : hits API
/// let response1 = cached_client.cached_create( request.clone() ).await?;
///
/// // Second call : hits cache (instant, no API call)
/// let response2 = cached_client.cached_create( request ).await?;
///
/// assert_eq!( response1.id, response2.id ); // Same response
/// # Ok( () )
/// # }
/// # }
/// ```
#[ cfg( feature = "caching" ) ]
pub struct CachedClient< E >
where
E : XaiEnvironment,
{
client : Client< E >,
cache : Arc< Mutex< LruCache< String, ChatCompletionResponse > > >,
}
#[ cfg( feature = "caching" ) ]
impl< E > std::fmt::Debug for CachedClient< E >
where
E : XaiEnvironment + std::fmt::Debug,
{
fn fmt( &self, f : &mut std::fmt::Formatter< '_ > ) -> std::fmt::Result
{
f.debug_struct( "CachedClient" )
.field( "client", &self.client )
.field( "cache", &"< LruCache >" )
.finish()
}
}
#[ cfg( feature = "caching" ) ]
impl< E > CachedClient< E >
where
E : XaiEnvironment,
{
/// Creates a new cached client with specified capacity.
///
/// # Arguments
///
/// * `client` - The underlying XAI client
/// * `capacity` - Maximum number of responses to cache
///
/// # Examples
///
/// ```no_run
/// # #[ cfg( feature = "caching") ]
/// # {
/// use api_xai::{ CachedClient, Client, Secret, XaiEnvironmentImpl };
///
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// let secret = Secret::new( "xai-key".to_string() )?;
/// let env = XaiEnvironmentImpl::new( secret )?;
/// let client = Client::build( env )?;
///
/// // Cache up to 100 responses
/// let cached_client = CachedClient::new( client, 100 );
/// # Ok( () )
/// # }
/// # }
/// ```
///
/// # Panics
///
/// Panics if capacity is zero.
pub fn new( client : Client< E >, capacity : usize ) -> Self
{
Self
{
client,
cache : Arc::new
(
Mutex::new
(
LruCache::new
(
NonZeroUsize::new( capacity ).expect( "Capacity must be > 0" )
)
)
),
}
}
/// Creates a chat completion request, using cache when possible.
///
/// # Caching Behavior
///
/// - **Cache hit**: Returns cached response instantly (no API call)
/// - **Cache miss**: Makes API call, stores response, returns it
/// - **Streaming requests**: Always bypass cache (no caching)
///
/// # Arguments
///
/// * `request` - The chat completion request
///
/// # Returns
///
/// The chat completion response (cached or fresh).
///
/// # Errors
///
/// Returns errors from the underlying API client.
///
/// # Examples
///
/// ```no_run
/// # #[ cfg( feature = "caching") ]
/// # {
/// use api_xai::{ CachedClient, Client, Secret, XaiEnvironmentImpl, ChatCompletionRequest, Message };
///
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// let secret = Secret::new( "xai-key".to_string() )?;
/// let env = XaiEnvironmentImpl::new( secret )?;
/// let client = Client::build( env )?;
/// let cached_client = CachedClient::new( client, 100 );
///
/// let request = ChatCompletionRequest::former()
/// .model( "grok-2-1212".to_string() )
/// .messages( vec![ Message::user( "Hello!" ) ] )
/// .form();
///
/// let response = cached_client.cached_create( request ).await?;
/// println!( "Response : {:?}", response.choices[ 0 ].message.content );
/// # Ok( () )
/// # }
/// # }
/// ```
///
/// # Panics
///
/// Panics if the internal mutex is poisoned.
pub async fn cached_create
(
&self,
request : ChatCompletionRequest
)
-> Result< ChatCompletionResponse >
{
// Streaming requests cannot be cached (responses are incremental)
if request.stream.unwrap_or( false )
{
return self.client.chat().create( request ).await;
}
let cache_key = Self::compute_cache_key( &request );
// Check cache (with explicit scope to release lock before await)
{
let mut cache = self.cache.lock().unwrap();
if let Some( cached ) = cache.get( &cache_key )
{
return Ok( cached.clone() );
}
}
// Cache miss - make API request
let response = self.client.chat().create( request ).await?;
// Store in cache
{
let mut cache = self.cache.lock().unwrap();
cache.put( cache_key, response.clone() );
}
Ok( response )
}
/// Clears all cached responses.
///
/// # Examples
///
/// ```no_run
/// # #[ cfg( feature = "caching") ]
/// # {
/// use api_xai::{ CachedClient, Client, Secret, XaiEnvironmentImpl };
///
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// let secret = Secret::new( "xai-key".to_string() )?;
/// let env = XaiEnvironmentImpl::new( secret )?;
/// let client = Client::build( env )?;
/// let cached_client = CachedClient::new( client, 100 );
///
/// // ... use cache ...
///
/// // Clear cache
/// cached_client.clear();
/// # Ok( () )
/// # }
/// # }
/// ```
///
/// # Panics
///
/// Panics if the internal mutex is poisoned.
pub fn clear( &self )
{
let mut cache = self.cache.lock().unwrap();
cache.clear();
}
/// Returns the number of cached responses.
///
/// # Examples
///
/// ```no_run
/// # #[ cfg( feature = "caching") ]
/// # {
/// use api_xai::{ CachedClient, Client, Secret, XaiEnvironmentImpl };
///
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// let secret = Secret::new( "xai-key".to_string() )?;
/// let env = XaiEnvironmentImpl::new( secret )?;
/// let client = Client::build( env )?;
/// let cached_client = CachedClient::new( client, 100 );
///
/// println!( "Cache size : {}", cached_client.len() );
/// # Ok( () )
/// # }
/// # }
/// ```
///
/// # Panics
///
/// Panics if the internal mutex is poisoned.
pub fn len( &self ) -> usize
{
let cache = self.cache.lock().unwrap();
cache.len()
}
/// Returns true if the cache is empty.
///
/// # Examples
///
/// ```no_run
/// # #[ cfg( feature = "caching") ]
/// # {
/// use api_xai::{ CachedClient, Client, Secret, XaiEnvironmentImpl };
///
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// let secret = Secret::new( "xai-key".to_string() )?;
/// let env = XaiEnvironmentImpl::new( secret )?;
/// let client = Client::build( env )?;
/// let cached_client = CachedClient::new( client, 100 );
///
/// assert!( cached_client.is_empty() );
/// # Ok( () )
/// # }
/// # }
/// ```
///
/// # Panics
///
/// Panics if the internal mutex is poisoned.
pub fn is_empty( &self ) -> bool
{
let cache = self.cache.lock().unwrap();
cache.is_empty()
}
/// Computes a cache key for a request.
///
/// Uses the serialized JSON representation of the request as input
/// to a hash function. This ensures that any change to the request
/// produces a different cache key.
fn compute_cache_key( request : &ChatCompletionRequest ) -> String
{
let json = serde_json::to_string( request ).unwrap_or_default();
let mut hasher = DefaultHasher::new();
json.hash( &mut hasher );
format!( "{:x}", hasher.finish() )
}
}
}
#[ cfg( feature = "caching" ) ]
crate::mod_interface!
{
exposed use
{
CachedClient,
};
}