liter-llm 1.7.4

Universal LLM API client — 142+ providers, streaming, tool calling. Rust-powered, type-safe, compiled.
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
//! Type-state builder for [`DefaultClient`].
//!
//! Use [`ClientBuilder`] when you want compile-time enforcement that both an
//! API key and a provider name have been supplied before the client is
//! constructed.  Calling [`build`](ClientBuilder::build) without setting both
//! fields is a **compile error**, not a runtime error.
//!
//! [`ClientConfigBuilder`](super::config::ClientConfigBuilder) remains
//! available for cases where the key or provider are not known until runtime
//! (e.g. when loading from environment variables without an explicit provider
//! hint).
//!
//! # Examples
//!
//! ```rust,no_run
//! # #[cfg(any(feature = "native-http", feature = "wasm-http"))]
//! # {
//! use liter_llm::ClientBuilder;
//!
//! # async fn run() -> liter_llm::Result<()> {
//! let client = ClientBuilder::new()
//!     .api_key("sk-…")
//!     .provider("openai")
//!     .build()?;
//! # Ok(())
//! # }
//! # }
//! ```

use std::sync::Arc;
use std::time::Duration;

use secrecy::SecretString;

use crate::auth::CredentialProvider;
#[cfg(any(feature = "native-http", feature = "wasm-http"))]
use crate::error::Result;
use crate::http::transport::TransportConfig;
#[cfg(feature = "tower")]
use crate::tower::{BudgetConfig, CacheConfig, CacheStore, LlmHook, RateLimitConfig};

// ── Type-state markers ───────────────────────────────────────────────────────

/// Marker: no API key has been set on this builder.
pub struct NoApiKey;
/// Marker: an API key has been set on this builder.
pub struct WithApiKey;
/// Marker: no provider has been set on this builder.
pub struct NoProvider;
/// Marker: a provider name (model hint) has been set on this builder.
pub struct WithProvider;

// ── Builder ──────────────────────────────────────────────────────────────────

/// Type-state builder for [`DefaultClient`](super::DefaultClient).
///
/// The two type parameters `K` and `P` track whether the required fields have
/// been supplied:
///
/// - `K` is [`NoApiKey`] until [`api_key`](Self::api_key) is called, then
///   [`WithApiKey`].
/// - `P` is [`NoProvider`] until [`provider`](Self::provider) is called, then
///   [`WithProvider`].
///
/// [`build`](Self::build) is only available when both are [`WithApiKey`] and
/// [`WithProvider`].  Attempting to call it in any other state is a **compile
/// error**.
///
/// All optional knobs (`base_url`, `timeout`, `transport`, …) are available
/// at any point in the chain and do not affect the type-state.
///
/// # Examples
///
/// ```rust,no_run
/// # #[cfg(any(feature = "native-http", feature = "wasm-http"))]
/// # {
/// use liter_llm::ClientBuilder;
///
/// # async fn run() -> liter_llm::Result<()> {
/// let client = ClientBuilder::new()
///     .api_key("sk-…")
///     .provider("openai")
///     .timeout(std::time::Duration::from_secs(30))
///     .build()?;
/// # Ok(())
/// # }
/// # }
/// ```
#[must_use = "call .build() to construct the client"]
pub struct ClientBuilder<K = NoApiKey, P = NoProvider> {
    api_key: SecretString,
    provider_hint: String,
    base_url: Option<String>,
    timeout: Duration,
    max_retries: u32,
    transport: TransportConfig,
    load_env: bool,
    credential_provider: Option<Arc<dyn CredentialProvider>>,
    #[cfg(feature = "tower")]
    cache_config: Option<CacheConfig>,
    #[cfg(feature = "tower")]
    cache_store: Option<Arc<dyn CacheStore>>,
    #[cfg(feature = "tower")]
    budget_config: Option<BudgetConfig>,
    #[cfg(feature = "tower")]
    hooks: Vec<Arc<dyn LlmHook>>,
    #[cfg(feature = "tower")]
    cooldown_duration: Option<Duration>,
    #[cfg(feature = "tower")]
    rate_limit_config: Option<RateLimitConfig>,
    #[cfg(feature = "tower")]
    health_check_interval: Option<Duration>,
    #[cfg(feature = "tower")]
    enable_cost_tracking: bool,
    #[cfg(feature = "tower")]
    enable_tracing: bool,
    _key_state: std::marker::PhantomData<K>,
    _provider_state: std::marker::PhantomData<P>,
}

impl ClientBuilder<NoApiKey, NoProvider> {
    /// Create a new builder with no key and no provider set.
    ///
    /// Call [`api_key`](Self::api_key) and [`provider`](Self::provider) to
    /// advance the type state, then call [`build`] when both are set.
    pub fn new() -> Self {
        Self {
            api_key: SecretString::from(String::new()),
            provider_hint: String::new(),
            base_url: None,
            timeout: Duration::from_secs(60),
            max_retries: 3,
            transport: TransportConfig::default(),
            load_env: false,
            credential_provider: None,
            #[cfg(feature = "tower")]
            cache_config: None,
            #[cfg(feature = "tower")]
            cache_store: None,
            #[cfg(feature = "tower")]
            budget_config: None,
            #[cfg(feature = "tower")]
            hooks: Vec::new(),
            #[cfg(feature = "tower")]
            cooldown_duration: None,
            #[cfg(feature = "tower")]
            rate_limit_config: None,
            #[cfg(feature = "tower")]
            health_check_interval: None,
            #[cfg(feature = "tower")]
            enable_cost_tracking: false,
            #[cfg(feature = "tower")]
            enable_tracing: false,
            _key_state: std::marker::PhantomData,
            _provider_state: std::marker::PhantomData,
        }
    }
}

impl Default for ClientBuilder<NoApiKey, NoProvider> {
    fn default() -> Self {
        Self::new()
    }
}

impl<K, P> ClientBuilder<K, P> {
    /// Set the API key for authentication.
    ///
    /// Accepts any value that converts into a [`SecretString`] (including
    /// `&str` and `String`).  The secret is zeroed on drop.
    ///
    /// Calling this method transitions the `K` type parameter from
    /// [`NoApiKey`] to [`WithApiKey`].
    pub fn api_key(self, key: impl Into<String>) -> ClientBuilder<WithApiKey, P> {
        ClientBuilder {
            api_key: SecretString::from(key.into()),
            provider_hint: self.provider_hint,
            base_url: self.base_url,
            timeout: self.timeout,
            max_retries: self.max_retries,
            transport: self.transport,
            load_env: self.load_env,
            credential_provider: self.credential_provider,
            #[cfg(feature = "tower")]
            cache_config: self.cache_config,
            #[cfg(feature = "tower")]
            cache_store: self.cache_store,
            #[cfg(feature = "tower")]
            budget_config: self.budget_config,
            #[cfg(feature = "tower")]
            hooks: self.hooks,
            #[cfg(feature = "tower")]
            cooldown_duration: self.cooldown_duration,
            #[cfg(feature = "tower")]
            rate_limit_config: self.rate_limit_config,
            #[cfg(feature = "tower")]
            health_check_interval: self.health_check_interval,
            #[cfg(feature = "tower")]
            enable_cost_tracking: self.enable_cost_tracking,
            #[cfg(feature = "tower")]
            enable_tracing: self.enable_tracing,
            _key_state: std::marker::PhantomData,
            _provider_state: self._provider_state,
        }
    }

    /// Set the provider by name or model-hint string.
    ///
    /// The string is forwarded to provider auto-detection in the same way
    /// that the `model_hint` argument to `DefaultClient::new` works.  Use
    /// a slash-prefixed name (e.g. `"openai"`, `"groq/llama3-70b"`,
    /// `"anthropic"`) or a bare model name.
    ///
    /// Calling this method transitions the `P` type parameter from
    /// [`NoProvider`] to [`WithProvider`].
    pub fn provider(self, provider_name: impl Into<String>) -> ClientBuilder<K, WithProvider> {
        ClientBuilder {
            api_key: self.api_key,
            provider_hint: provider_name.into(),
            base_url: self.base_url,
            timeout: self.timeout,
            max_retries: self.max_retries,
            transport: self.transport,
            load_env: self.load_env,
            credential_provider: self.credential_provider,
            #[cfg(feature = "tower")]
            cache_config: self.cache_config,
            #[cfg(feature = "tower")]
            cache_store: self.cache_store,
            #[cfg(feature = "tower")]
            budget_config: self.budget_config,
            #[cfg(feature = "tower")]
            hooks: self.hooks,
            #[cfg(feature = "tower")]
            cooldown_duration: self.cooldown_duration,
            #[cfg(feature = "tower")]
            rate_limit_config: self.rate_limit_config,
            #[cfg(feature = "tower")]
            health_check_interval: self.health_check_interval,
            #[cfg(feature = "tower")]
            enable_cost_tracking: self.enable_cost_tracking,
            #[cfg(feature = "tower")]
            enable_tracing: self.enable_tracing,
            _key_state: self._key_state,
            _provider_state: std::marker::PhantomData,
        }
    }

    /// Override the provider base URL for all requests.
    ///
    /// Trailing slashes are trimmed automatically.  When set, provider
    /// auto-detection is bypassed and all requests go to this URL.
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        let url = url.into();
        self.base_url = Some(url.trim_end_matches('/').to_string());
        self
    }

    /// Set the per-request timeout (default: 60 s).
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set the maximum number of retries on 429 / 5xx responses (default: 3).
    pub fn max_retries(mut self, retries: u32) -> Self {
        self.max_retries = retries;
        self
    }

    /// Set the HTTP transport configuration.
    ///
    /// Controls connection pooling, TCP keepalive, DNS caching, and HTTP
    /// version selection.
    pub fn transport(mut self, config: TransportConfig) -> Self {
        self.transport = config;
        self
    }

    /// Enable or disable automatic API key loading from environment variables.
    ///
    /// When `true`, the client will attempt to read the provider's designated
    /// environment variable (e.g. `OPENAI_API_KEY`) if no key was set via
    /// [`api_key`](Self::api_key).  Defaults to `false` in [`ClientBuilder`]
    /// (unlike [`ClientConfigBuilder`](super::config::ClientConfigBuilder)
    /// which defaults to `true`) because the type-state design signals that
    /// the key should be provided explicitly.
    pub fn load_env(mut self, enabled: bool) -> Self {
        self.load_env = enabled;
        self
    }

    /// Set a dynamic credential provider for token-based or refreshable auth.
    ///
    /// When configured, the client calls `resolve()` before each request
    /// instead of using the static API key for authentication.
    pub fn credential_provider(mut self, provider: Arc<dyn CredentialProvider>) -> Self {
        self.credential_provider = Some(provider);
        self
    }

    /// Set the response cache configuration for the Tower middleware stack.
    #[cfg(feature = "tower")]
    pub fn cache(mut self, config: CacheConfig) -> Self {
        self.cache_config = Some(config);
        self
    }

    /// Set a custom cache store backend for the Tower cache middleware.
    #[cfg(feature = "tower")]
    pub fn cache_store(mut self, store: Arc<dyn CacheStore>) -> Self {
        self.cache_store = Some(store);
        self
    }

    /// Set the budget enforcement configuration for the Tower middleware stack.
    #[cfg(feature = "tower")]
    pub fn budget(mut self, config: BudgetConfig) -> Self {
        self.budget_config = Some(config);
        self
    }

    /// Add a single hook to the Tower hooks middleware stack.
    #[cfg(feature = "tower")]
    pub fn hook(mut self, hook: Arc<dyn LlmHook>) -> Self {
        self.hooks.push(hook);
        self
    }

    /// Set the full list of hooks for the Tower hooks middleware stack.
    #[cfg(feature = "tower")]
    pub fn hooks(mut self, hooks: Vec<Arc<dyn LlmHook>>) -> Self {
        self.hooks = hooks;
        self
    }

    /// Set the cooldown duration after transient errors.
    #[cfg(feature = "tower")]
    pub fn cooldown(mut self, duration: Duration) -> Self {
        self.cooldown_duration = Some(duration);
        self
    }

    /// Set per-model rate limiting configuration.
    #[cfg(feature = "tower")]
    pub fn rate_limit(mut self, config: RateLimitConfig) -> Self {
        self.rate_limit_config = Some(config);
        self
    }

    /// Set the background health check interval.
    #[cfg(feature = "tower")]
    pub fn health_check(mut self, interval: Duration) -> Self {
        self.health_check_interval = Some(interval);
        self
    }

    /// Enable or disable per-request cost tracking.
    #[cfg(feature = "tower")]
    pub fn cost_tracking(mut self, enabled: bool) -> Self {
        self.enable_cost_tracking = enabled;
        self
    }

    /// Enable or disable OpenTelemetry-compatible tracing spans.
    #[cfg(feature = "tower")]
    pub fn tracing(mut self, enabled: bool) -> Self {
        self.enable_tracing = enabled;
        self
    }
}

// ── Terminal: build() is only available with WithApiKey + WithProvider ────────

#[cfg(any(feature = "native-http", feature = "wasm-http"))]
impl ClientBuilder<WithApiKey, WithProvider> {
    /// Consume the builder and construct a [`DefaultClient`](super::DefaultClient).
    ///
    /// This method is only available when both [`api_key`](ClientBuilder::api_key)
    /// and [`provider`](ClientBuilder::provider) have been called.  The
    /// compiler enforces this at the type level.
    ///
    /// # Errors
    ///
    /// Returns `LiterLlmError::Authentication` if the provider requires an
    /// environment variable that is unset (only relevant when the API key
    /// supplied via [`api_key`] is an empty string and `load_env` is `true`).
    ///
    /// Returns `LiterLlmError::Http` if the underlying HTTP client cannot be
    /// constructed.
    pub fn build(self) -> Result<super::DefaultClient> {
        use super::config::ClientConfig;

        let config = ClientConfig {
            api_key: self.api_key,
            base_url: self.base_url,
            timeout: self.timeout,
            max_retries: self.max_retries,
            extra_headers: Vec::new(),
            credential_provider: self.credential_provider,
            load_env: self.load_env,
            transport: self.transport,
            #[cfg(feature = "tower")]
            cache_config: self.cache_config,
            #[cfg(feature = "tower")]
            cache_store: self.cache_store,
            #[cfg(feature = "tower")]
            budget_config: self.budget_config,
            #[cfg(feature = "tower")]
            hooks: self.hooks,
            #[cfg(feature = "tower")]
            cooldown_duration: self.cooldown_duration,
            #[cfg(feature = "tower")]
            rate_limit_config: self.rate_limit_config,
            #[cfg(feature = "tower")]
            health_check_interval: self.health_check_interval,
            #[cfg(feature = "tower")]
            enable_cost_tracking: self.enable_cost_tracking,
            #[cfg(feature = "tower")]
            enable_tracing: self.enable_tracing,
        };

        // Use the provider_hint for model detection; an empty hint defaults to
        // OpenAI (same semantics as DefaultClient::new with None hint).
        let hint = if self.provider_hint.is_empty() {
            None
        } else {
            Some(self.provider_hint.as_str())
        };

        super::DefaultClient::new(config, hint)
    }
}