claude-agent 0.2.25

Rust SDK for building AI agents with Anthropic's Claude - Direct API, no CLI dependency
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
//! Anthropic API client with multi-provider support.

pub mod adapter;
pub mod batch;
pub mod fallback;
pub mod files;
pub mod gateway;
pub mod messages;
pub mod network;
pub mod recovery;
pub mod resilience;
pub mod schema;
mod streaming;

pub use adapter::{
    AnthropicAdapter, BetaConfig, BetaFeature, CloudProvider, DEFAULT_MODEL,
    DEFAULT_REASONING_MODEL, DEFAULT_SMALL_MODEL, FRONTIER_MODEL, ModelConfig, ModelType,
    ProviderAdapter, ProviderConfig,
};
pub use batch::{
    BatchClient, BatchRequest, BatchResult, BatchStatus, CreateBatchRequest, MessageBatch,
};
pub use fallback::{FallbackConfig, FallbackTrigger};
pub use files::{File, FileData, FileDownload, FileListResponse, FilesClient, UploadFileRequest};
pub use gateway::GatewayConfig;
pub use messages::{
    ClearConfig, ClearTrigger, ContextEdit, ContextManagement, CountTokensContextManagement,
    CountTokensRequest, CountTokensResponse, CreateMessageRequest, DEFAULT_MAX_TOKENS, EffortLevel,
    KeepConfig, KeepThinkingConfig, MAX_TOKENS_128K, MIN_MAX_TOKENS, MIN_THINKING_BUDGET,
    OutputConfig, OutputFormat, ThinkingConfig, ThinkingType, TokenValidationError, ToolChoice,
};
pub use network::{ClientCertConfig, HttpNetworkConfig, PoolConfig, ProxyConfig};
pub use recovery::StreamRecoveryState;
pub use resilience::{
    CircuitBreaker, CircuitConfig, CircuitState, ExponentialBackoff, Resilience, ResilienceConfig,
    RetryConfig,
};
pub use schema::{strict_schema, transform_for_strict};
pub use streaming::{RecoverableStream, StreamItem, StreamParser};

#[cfg(feature = "aws")]
pub use adapter::BedrockAdapter;
#[cfg(feature = "azure")]
pub use adapter::FoundryAdapter;
#[cfg(feature = "gcp")]
pub use adapter::VertexAdapter;

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

use crate::auth::{Auth, Credential, OAuthConfig};
use crate::{Error, Result};

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(300);

#[derive(Clone)]
pub struct Client {
    adapter: Arc<dyn ProviderAdapter>,
    http: reqwest::Client,
    fallback_config: Option<FallbackConfig>,
    resilience: Option<Arc<Resilience>>,
}

impl Client {
    pub fn new(adapter: impl ProviderAdapter + 'static) -> Result<Self> {
        let timeout = DEFAULT_TIMEOUT;
        let http = reqwest::Client::builder()
            .timeout(timeout)
            .build()
            .map_err(Error::Network)?;

        Ok(Self {
            adapter: Arc::new(adapter),
            http,
            fallback_config: None,
            resilience: None,
        })
    }

    pub fn from_http(adapter: impl ProviderAdapter + 'static, http: reqwest::Client) -> Self {
        Self {
            adapter: Arc::new(adapter),
            http,
            fallback_config: None,
            resilience: None,
        }
    }

    pub fn fallback(mut self, config: FallbackConfig) -> Self {
        self.fallback_config = Some(config);
        self
    }

    pub fn resilience(mut self, config: ResilienceConfig) -> Self {
        self.resilience = Some(Arc::new(Resilience::new(config)));
        self
    }

    pub fn resilience_ref(&self) -> Option<&Arc<Resilience>> {
        self.resilience.as_ref()
    }

    pub fn builder() -> ClientBuilder {
        ClientBuilder::default()
    }

    pub async fn query(&self, prompt: &str) -> Result<String> {
        self.query_with_model(prompt, ModelType::Primary).await
    }

    pub async fn query_with_model(&self, prompt: &str, model_type: ModelType) -> Result<String> {
        let model = self.adapter.model(model_type).to_string();
        let request = CreateMessageRequest::new(&model, vec![crate::types::Message::user(prompt)])
            .max_tokens(self.adapter.config().max_tokens);
        request.validate()?;

        let response = self.adapter.send(&self.http, request).await?;
        Ok(response.text())
    }

    fn check_circuit_breaker(&self) -> Result<Option<Arc<CircuitBreaker>>> {
        let cb = self.resilience.as_ref().and_then(|r| r.circuit().cloned());
        if let Some(ref cb) = cb
            && !cb.allow_request()
        {
            return Err(Error::CircuitOpen);
        }
        Ok(cb)
    }

    fn record_circuit_result<T>(cb: &Option<Arc<CircuitBreaker>>, result: &Result<T>) {
        if let Some(cb) = cb {
            match result {
                Ok(_) => cb.record_success(),
                Err(_) => cb.record_failure(),
            }
        }
    }

    pub async fn send(&self, request: CreateMessageRequest) -> Result<crate::types::ApiResponse> {
        let cb = self.check_circuit_breaker()?;
        let result = self.send_inner(request).await;
        Self::record_circuit_result(&cb, &result);
        result
    }

    async fn send_inner(&self, request: CreateMessageRequest) -> Result<crate::types::ApiResponse> {
        request.validate()?;

        let fallback = match &self.fallback_config {
            Some(f) => f,
            None => return self.adapter.send(&self.http, request).await,
        };

        let mut current_request = request;
        let mut attempt = 0;
        let mut using_fallback = false;

        loop {
            match self.adapter.send(&self.http, current_request.clone()).await {
                Ok(response) => return Ok(response),
                Err(e) => {
                    if !fallback.should_fallback(&e) {
                        return Err(e);
                    }

                    attempt += 1;
                    if attempt > fallback.max_retries {
                        return Err(e);
                    }

                    if !using_fallback {
                        tracing::warn!(
                            error = %e,
                            fallback_model = %fallback.fallback_model,
                            attempt,
                            max_retries = fallback.max_retries,
                            "Primary model failed, falling back"
                        );
                        current_request = current_request.model(&fallback.fallback_model);
                        using_fallback = true;
                    } else {
                        tracing::warn!(
                            error = %e,
                            attempt,
                            max_retries = fallback.max_retries,
                            "Fallback model failed, retrying"
                        );
                    }
                }
            }
        }
    }

    pub async fn send_no_fallback(
        &self,
        request: CreateMessageRequest,
    ) -> Result<crate::types::ApiResponse> {
        request.validate()?;
        self.adapter.send(&self.http, request).await
    }

    pub fn fallback_config(&self) -> Option<&FallbackConfig> {
        self.fallback_config.as_ref()
    }

    pub async fn stream(
        &self,
        prompt: &str,
    ) -> Result<impl futures::Stream<Item = Result<String>> + Send + 'static + use<>> {
        let model = self.adapter.model(ModelType::Primary).to_string();
        let request = CreateMessageRequest::new(&model, vec![crate::types::Message::user(prompt)])
            .max_tokens(self.adapter.config().max_tokens);
        request.validate()?;

        let response = self.adapter.send_stream(&self.http, request).await?;
        let stream = StreamParser::new(response.bytes_stream());

        Ok(futures::StreamExt::filter_map(stream, |item| async move {
            match item {
                Ok(StreamItem::Text(text)) => Some(Ok(text)),
                Ok(StreamItem::Thinking(text)) => Some(Ok(text)),
                Ok(
                    StreamItem::Event(_) | StreamItem::Citation(_) | StreamItem::ToolUseComplete(_),
                ) => None,
                Err(e) => Some(Err(e)),
            }
        }))
    }

    pub async fn stream_request(
        &self,
        request: CreateMessageRequest,
    ) -> Result<impl futures::Stream<Item = Result<StreamItem>> + Send + 'static + use<>> {
        let cb = self.check_circuit_breaker()?;
        let result = self.stream_request_inner(request).await;
        Self::record_circuit_result(&cb, &result);
        result
    }

    async fn stream_request_inner(
        &self,
        request: CreateMessageRequest,
    ) -> Result<impl futures::Stream<Item = Result<StreamItem>> + Send + 'static + use<>> {
        request.validate()?;
        let response = self.adapter.send_stream(&self.http, request).await?;
        Ok(StreamParser::new(response.bytes_stream()))
    }

    pub async fn stream_recoverable(
        &self,
        request: CreateMessageRequest,
    ) -> Result<
        RecoverableStream<
            impl futures::Stream<Item = std::result::Result<bytes::Bytes, reqwest::Error>>
            + Send
            + 'static
            + use<>,
        >,
    > {
        request.validate()?;
        let response = self.adapter.send_stream(&self.http, request).await?;
        Ok(RecoverableStream::new(response.bytes_stream()))
    }

    pub async fn stream_with_recovery(
        &self,
        request: CreateMessageRequest,
        recovery_state: Option<StreamRecoveryState>,
    ) -> Result<
        RecoverableStream<
            impl futures::Stream<Item = std::result::Result<bytes::Bytes, reqwest::Error>>
            + Send
            + 'static
            + use<>,
        >,
    > {
        let request = match recovery_state {
            Some(state) if state.is_recoverable() => {
                let mut req = request;
                req.messages = state.build_continuation_messages(&req.messages);
                req
            }
            _ => request,
        };
        self.stream_recoverable(request).await
    }

    pub fn batch(&self) -> BatchClient<'_> {
        BatchClient::new(self)
    }

    pub fn files(&self) -> FilesClient<'_> {
        FilesClient::new(self)
    }

    pub fn adapter(&self) -> &dyn ProviderAdapter {
        self.adapter.as_ref()
    }

    pub fn config(&self) -> &ProviderConfig {
        self.adapter.config()
    }

    pub(crate) fn http(&self) -> &reqwest::Client {
        &self.http
    }

    pub async fn refresh_credentials(&self) -> Result<()> {
        self.adapter.refresh_credentials().await
    }

    async fn with_auth_retry<T, F, Fut>(&self, op: F) -> Result<T>
    where
        F: Fn() -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        self.adapter.ensure_fresh_credentials().await?;

        match op().await {
            Ok(resp) => Ok(resp),
            Err(e) if e.is_unauthorized() && self.adapter.supports_credential_refresh() => {
                tracing::debug!("Received 401, refreshing credentials");
                self.refresh_credentials().await?;
                op().await
            }
            Err(e) => Err(e),
        }
    }

    pub async fn send_with_auth_retry(
        &self,
        request: CreateMessageRequest,
    ) -> Result<crate::types::ApiResponse> {
        self.with_auth_retry(|| self.send(request.clone())).await
    }

    pub async fn send_stream_with_auth_retry(
        &self,
        request: CreateMessageRequest,
    ) -> Result<reqwest::Response> {
        request.validate()?;
        self.with_auth_retry(|| self.adapter.send_stream(&self.http, request.clone()))
            .await
    }

    pub async fn count_tokens(
        &self,
        request: messages::CountTokensRequest,
    ) -> Result<messages::CountTokensResponse> {
        self.with_auth_retry(|| self.adapter.count_tokens(&self.http, request.clone()))
            .await
    }

    pub async fn count_tokens_for_request(
        &self,
        request: &CreateMessageRequest,
    ) -> Result<messages::CountTokensResponse> {
        let count_request = messages::CountTokensRequest::from_message_request(request);
        self.count_tokens(count_request).await
    }
}

impl std::fmt::Debug for Client {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Client")
            .field("provider", &self.adapter.name())
            .finish()
    }
}

#[derive(Default)]
pub struct ClientBuilder {
    provider: Option<CloudProvider>,
    credential: Option<Credential>,
    credential_provider: Option<Arc<dyn crate::auth::CredentialProvider>>,
    oauth_config: Option<OAuthConfig>,
    config: Option<ProviderConfig>,
    models: Option<ModelConfig>,
    network: Option<HttpNetworkConfig>,
    gateway: Option<GatewayConfig>,
    timeout: Option<Duration>,
    fallback_config: Option<FallbackConfig>,
    resilience_config: Option<ResilienceConfig>,

    #[cfg(feature = "aws")]
    aws_region: Option<String>,
    #[cfg(feature = "gcp")]
    gcp_project: Option<String>,
    #[cfg(feature = "gcp")]
    gcp_region: Option<String>,
    #[cfg(feature = "azure")]
    azure_resource: Option<String>,
}

impl ClientBuilder {
    /// Configure authentication for the client.
    ///
    /// Accepts `Auth` enum or any type that converts to it (e.g., API key string).
    /// For `Auth::ClaudeCli`, the credential provider is preserved for automatic token refresh.
    pub async fn auth(mut self, auth: impl Into<Auth>) -> Result<Self> {
        let auth = auth.into();

        #[allow(unreachable_patterns)]
        match &auth {
            #[cfg(feature = "aws")]
            Auth::Bedrock { region } => {
                self.provider = Some(CloudProvider::Bedrock);
                self.aws_region = Some(region.clone());
            }
            #[cfg(feature = "gcp")]
            Auth::Vertex { project, region } => {
                self.provider = Some(CloudProvider::Vertex);
                self.gcp_project = Some(project.clone());
                self.gcp_region = Some(region.clone());
            }
            #[cfg(feature = "azure")]
            Auth::Foundry { resource } => {
                self.provider = Some(CloudProvider::Foundry);
                self.azure_resource = Some(resource.clone());
            }
            _ => {
                self.provider = Some(CloudProvider::Anthropic);
            }
        }

        let (credential, provider) = auth.resolve_with_provider().await?;
        if !credential.is_placeholder() {
            self.credential = Some(credential);
        }
        self.credential_provider = provider;

        Ok(self)
    }

    pub fn anthropic(mut self) -> Self {
        self.provider = Some(CloudProvider::Anthropic);
        self
    }

    #[cfg(feature = "aws")]
    pub(crate) fn aws_region(mut self, region: String) -> Self {
        self.provider = Some(CloudProvider::Bedrock);
        self.aws_region = Some(region);
        self
    }

    #[cfg(feature = "gcp")]
    pub(crate) fn gcp(mut self, project: String, region: String) -> Self {
        self.provider = Some(CloudProvider::Vertex);
        self.gcp_project = Some(project);
        self.gcp_region = Some(region);
        self
    }

    #[cfg(feature = "azure")]
    pub(crate) fn azure_resource(mut self, resource: String) -> Self {
        self.provider = Some(CloudProvider::Foundry);
        self.azure_resource = Some(resource);
        self
    }

    pub fn oauth_config(mut self, config: OAuthConfig) -> Self {
        self.oauth_config = Some(config);
        self
    }

    pub fn models(mut self, models: ModelConfig) -> Self {
        self.models = Some(models);
        self
    }

    pub fn config(mut self, config: ProviderConfig) -> Self {
        self.config = Some(config);
        self
    }

    pub fn network(mut self, network: HttpNetworkConfig) -> Self {
        self.network = Some(network);
        self
    }

    pub fn gateway(mut self, gateway: GatewayConfig) -> Self {
        self.gateway = Some(gateway);
        self
    }

    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    pub fn fallback(mut self, config: FallbackConfig) -> Self {
        self.fallback_config = Some(config);
        self
    }

    pub fn fallback_model(mut self, model: impl Into<String>) -> Self {
        self.fallback_config = Some(FallbackConfig::new(model));
        self
    }

    pub fn resilience(mut self, config: ResilienceConfig) -> Self {
        self.resilience_config = Some(config);
        self
    }

    pub fn default_resilience(mut self) -> Self {
        self.resilience_config = Some(ResilienceConfig::default());
        self
    }

    pub async fn build(self) -> Result<Client> {
        let provider = self.provider.unwrap_or_else(CloudProvider::from_env);

        let models = self.models.unwrap_or_else(|| provider.default_models());

        let config = self.config.unwrap_or_else(|| ProviderConfig::new(models));

        let adapter: Box<dyn ProviderAdapter> = match provider {
            CloudProvider::Anthropic => {
                let adapter = if let Some(ref cred) = self.credential {
                    let mut a = if let Some(cred_provider) = self.credential_provider {
                        AnthropicAdapter::from_credential_provider(
                            config,
                            cred,
                            self.oauth_config,
                            cred_provider,
                        )
                    } else {
                        AnthropicAdapter::from_credential(config, cred, self.oauth_config)
                    };
                    if let Some(ref gw) = self.gateway
                        && let Some(ref url) = gw.base_url
                    {
                        a = a.base_url(url);
                    }
                    a
                } else {
                    let mut a = AnthropicAdapter::new(config);
                    if let Some(ref gw) = self.gateway {
                        if let Some(ref url) = gw.base_url {
                            a = a.base_url(url);
                        }
                        if let Some(ref token) = gw.auth_token {
                            a = a.api_key(token);
                        }
                    }
                    a
                };
                Box::new(adapter)
            }
            #[cfg(feature = "aws")]
            CloudProvider::Bedrock => {
                let mut adapter = adapter::BedrockAdapter::from_env(config).await?;
                if let Some(region) = self.aws_region {
                    adapter = adapter.region(region);
                }
                Box::new(adapter)
            }
            #[cfg(feature = "gcp")]
            CloudProvider::Vertex => {
                let mut adapter = adapter::VertexAdapter::from_env(config).await?;
                if let Some(project) = self.gcp_project {
                    adapter = adapter.project(project);
                }
                if let Some(region) = self.gcp_region {
                    adapter = adapter.region(region);
                }
                Box::new(adapter)
            }
            #[cfg(feature = "azure")]
            CloudProvider::Foundry => {
                let mut adapter = adapter::FoundryAdapter::from_env(config).await?;
                if let Some(resource) = self.azure_resource {
                    adapter = adapter.resource(resource);
                }
                Box::new(adapter)
            }
        };

        let mut http_builder =
            reqwest::Client::builder().timeout(self.timeout.unwrap_or(DEFAULT_TIMEOUT));

        if let Some(ref network) = self.network {
            http_builder = network
                .apply_to_builder(http_builder)
                .await
                .map_err(|e| Error::Config(e.to_string()))?;
        }

        let http = http_builder.build().map_err(Error::Network)?;

        let resilience = self.resilience_config.map(|c| Arc::new(Resilience::new(c)));

        Ok(Client {
            adapter: Arc::from(adapter),
            http,
            fallback_config: self.fallback_config,
            resilience,
        })
    }
}

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

    #[test]
    fn test_client_builder() {
        let _builder = Client::builder().anthropic();
    }

    #[test]
    fn test_cloud_provider_from_env() {
        let provider = CloudProvider::from_env();
        assert_eq!(provider, CloudProvider::Anthropic);
    }

    #[tokio::test]
    async fn test_builder_with_auth_credential() {
        let _builder = Client::builder()
            .anthropic()
            .auth(Credential::api_key("test-key"))
            .await
            .unwrap();
    }
}