claude-api 0.5.3

Type-safe Rust client for the Anthropic API
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
//! HTTP client and builder.
//!
//! [`Client`] is the entry point to the SDK. It is cheap to [`Clone`] (an
//! `Arc<Inner>` under the hood) and `Send + Sync`, so a single instance can
//! be shared across tasks.

#![cfg(feature = "async")]

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

use serde::de::DeserializeOwned;

use crate::auth::{ApiKey, ApiKeySigner, RequestSigner};
use crate::error::{Error, Result};
use crate::retry::RetryPolicy;

/// HTTP client for the Anthropic API.
#[derive(Debug, Clone)]
pub struct Client {
    inner: Arc<Inner>,
}

#[derive(Debug)]
struct Inner {
    base_url: String,
    http: reqwest::Client,
    user_agent: String,
    betas: Vec<String>,
    retry: RetryPolicy,
    signer: Arc<dyn RequestSigner>,
}

impl Client {
    /// Construct a [`Client`] with default settings and the given API key.
    ///
    /// # Panics
    ///
    /// Panics if reqwest fails to build its default HTTP client (extremely
    /// unusual; would indicate a broken TLS stack). Use [`Client::builder`]
    /// for a fallible alternative.
    pub fn new(api_key: impl Into<String>) -> Self {
        Self::builder()
            .api_key(api_key)
            .build()
            .expect("default builder should succeed when an api key is provided")
    }

    /// Begin configuring a [`Client`].
    pub fn builder() -> ClientBuilder {
        ClientBuilder::default()
    }

    /// Namespace handle for the Messages API.
    pub fn messages(&self) -> crate::messages::Messages<'_> {
        crate::messages::Messages::new(self)
    }

    /// Namespace handle for the Models API.
    pub fn models(&self) -> crate::models::Models<'_> {
        crate::models::Models::new(self)
    }

    /// Namespace handle for the Batches API.
    pub fn batches(&self) -> crate::batches::Batches<'_> {
        crate::batches::Batches::new(self)
    }

    /// Namespace handle for the Files API (beta).
    pub fn files(&self) -> crate::files::Files<'_> {
        crate::files::Files::new(self)
    }

    /// Namespace handle for the Managed Agents API (preview).
    ///
    /// Gated on the `managed-agents-preview` feature.
    #[cfg(feature = "managed-agents-preview")]
    #[cfg_attr(docsrs, doc(cfg(feature = "managed-agents-preview")))]
    pub fn managed_agents(&self) -> crate::managed_agents::ManagedAgents<'_> {
        crate::managed_agents::ManagedAgents::new(self)
    }

    /// Namespace handle for the Admin API. Requires an admin API key.
    ///
    /// Gated on the `admin` feature.
    #[cfg(feature = "admin")]
    #[cfg_attr(docsrs, doc(cfg(feature = "admin")))]
    pub fn admin(&self) -> crate::admin::Admin<'_> {
        crate::admin::Admin::new(self)
    }

    /// Namespace handle for the Skills API (beta).
    ///
    /// Gated on the `skills` feature.
    #[cfg(feature = "skills")]
    #[cfg_attr(docsrs, doc(cfg(feature = "skills")))]
    pub fn skills(&self) -> crate::skills::Skills<'_> {
        crate::skills::Skills::new(self)
    }

    /// Namespace handle for the User Profiles API (beta).
    ///
    /// Gated on the `user-profiles` feature.
    #[cfg(feature = "user-profiles")]
    #[cfg_attr(docsrs, doc(cfg(feature = "user-profiles")))]
    pub fn user_profiles(&self) -> crate::user_profiles::UserProfiles<'_> {
        crate::user_profiles::UserProfiles::new(self)
    }

    /// Build a [`reqwest::RequestBuilder`] preloaded with the version
    /// and user-agent headers. Auth headers are added later by the
    /// configured [`RequestSigner`](crate::auth::RequestSigner). Endpoints
    /// add their body and any per-request beta headers, then call
    /// [`Self::execute`].
    pub(crate) fn request_builder(
        &self,
        method: reqwest::Method,
        path: &str,
    ) -> reqwest::RequestBuilder {
        let url = format!("{}{}", self.inner.base_url, path);
        self.inner
            .http
            .request(method, url)
            .header("anthropic-version", crate::ANTHROPIC_VERSION)
            .header(reqwest::header::USER_AGENT, &self.inner.user_agent)
    }

    /// Send a prepared request, merge in beta headers, and decode the response.
    ///
    /// Errors from the API (any non-2xx status) are mapped to [`Error::Api`]
    /// with `request-id` and `Retry-After` populated when the server sent
    /// them. The retry loop ([`Self::execute_with_retry`]) wraps this method.
    pub(crate) async fn execute<R: DeserializeOwned>(
        &self,
        mut builder: reqwest::RequestBuilder,
        per_request_betas: &[&str],
    ) -> Result<R> {
        if let Some(joined) = merge_betas(&self.inner.betas, per_request_betas) {
            builder = builder.header("anthropic-beta", joined);
        }

        let mut request = builder.build()?;
        self.inner
            .signer
            .sign(&mut request)
            .map_err(Error::Signing)?;
        let response = self.inner.http.execute(request).await?;
        let status = response.status();
        let request_id = response
            .headers()
            .get("request-id")
            .and_then(|v| v.to_str().ok())
            .map(String::from);
        let retry_after_header = response
            .headers()
            .get("retry-after")
            .and_then(|v| v.to_str().ok())
            .map(String::from);

        let bytes = response.bytes().await?;

        if !status.is_success() {
            tracing::warn!(
                status = status.as_u16(),
                request_id = ?request_id,
                "claude-api: error response"
            );
            return Err(Error::from_response(
                http::StatusCode::from_u16(status.as_u16())
                    .unwrap_or(http::StatusCode::INTERNAL_SERVER_ERROR),
                request_id,
                retry_after_header.as_deref(),
                &bytes,
            ));
        }

        Ok(serde_json::from_slice(&bytes)?)
    }

    /// Send a request with retries.
    ///
    /// `make_request` is called once per attempt to produce a fresh
    /// [`reqwest::RequestBuilder`]. Retries are gated by
    /// [`Error::is_retryable`] and spaced according to
    /// [`RetryPolicy::compute_backoff`]. Streaming endpoints intentionally do
    /// *not* go through this path -- a mid-stream retry would silently drop
    /// content.
    pub(crate) async fn execute_with_retry<R, F>(
        &self,
        mut make_request: F,
        per_request_betas: &[&str],
    ) -> Result<R>
    where
        R: DeserializeOwned,
        F: FnMut() -> reqwest::RequestBuilder,
    {
        let policy = &self.inner.retry;
        let mut attempt: u32 = 1;
        loop {
            let builder = make_request();
            match self.execute(builder, per_request_betas).await {
                Ok(r) => return Ok(r),
                Err(e) => {
                    if !e.is_retryable() || attempt >= policy.max_attempts {
                        return Err(e);
                    }
                    let backoff = policy.compute_backoff(attempt, e.retry_after());
                    tracing::warn!(
                        attempt,
                        retry_in_ms = u64::try_from(backoff.as_millis()).unwrap_or(u64::MAX),
                        request_id = ?e.request_id(),
                        status = ?e.status().map(|s| s.as_u16()),
                        "claude-api: retrying after error"
                    );
                    tokio::time::sleep(backoff).await;
                    attempt += 1;
                }
            }
        }
    }

    /// Send a request expected to return a streaming body.
    ///
    /// Returns the raw [`reqwest::Response`] on success so the caller can
    /// wrap its body in an SSE parser, JSONL parser, or other line-oriented
    /// reader. Non-2xx responses are mapped to [`Error::Api`] (with
    /// `request-id` and `Retry-After`) just like [`Self::execute`]; the
    /// body is consumed in that case.
    ///
    /// Streaming is *not* retried -- once the server starts emitting events,
    /// retrying mid-stream would silently drop content.
    pub(crate) async fn execute_streaming(
        &self,
        mut builder: reqwest::RequestBuilder,
        per_request_betas: &[&str],
    ) -> Result<reqwest::Response> {
        if let Some(joined) = merge_betas(&self.inner.betas, per_request_betas) {
            builder = builder.header("anthropic-beta", joined);
        }

        let mut request = builder.build()?;
        self.inner
            .signer
            .sign(&mut request)
            .map_err(Error::Signing)?;
        let response = self.inner.http.execute(request).await?;
        let status = response.status();

        if !status.is_success() {
            let request_id = response
                .headers()
                .get("request-id")
                .and_then(|v| v.to_str().ok())
                .map(String::from);
            let retry_after_header = response
                .headers()
                .get("retry-after")
                .and_then(|v| v.to_str().ok())
                .map(String::from);
            let bytes = response.bytes().await?;
            tracing::warn!(
                status = status.as_u16(),
                request_id = ?request_id,
                "claude-api: streaming connect failed"
            );
            return Err(Error::from_response(
                http::StatusCode::from_u16(status.as_u16())
                    .unwrap_or(http::StatusCode::INTERNAL_SERVER_ERROR),
                request_id,
                retry_after_header.as_deref(),
                &bytes,
            ));
        }

        Ok(response)
    }

    #[cfg(test)]
    pub(crate) fn betas(&self) -> &[String] {
        &self.inner.betas
    }

    /// Materialize a request without sending it, for use by namespace-level
    /// `dry_run` helpers. Mirrors the header logic in
    /// [`Self::execute`]/[`Self::execute_streaming`] so the rendered
    /// preview matches what would actually be transmitted.
    pub(crate) fn render_dry_run(
        &self,
        mut builder: reqwest::RequestBuilder,
        per_request_betas: &[&str],
    ) -> Result<crate::dry_run::DryRun> {
        if let Some(joined) = merge_betas(&self.inner.betas, per_request_betas) {
            builder = builder.header("anthropic-beta", joined);
        }
        let mut req = builder.build()?;
        // Run the signer through dry_run too so the rendered preview
        // matches the wire bytes the live client would actually send.
        self.inner.signer.sign(&mut req).map_err(Error::Signing)?;
        let method = req.method().clone();
        let url = req.url().to_string();
        let mut headers = http::HeaderMap::new();
        for (name, value) in req.headers() {
            // Convert reqwest::header::HeaderName/Value (re-exports of http
            // types) into the http crate's owned types.
            if let (Ok(name), Ok(value)) = (
                http::HeaderName::from_bytes(name.as_ref()),
                http::HeaderValue::from_bytes(value.as_bytes()),
            ) {
                headers.append(name, value);
            }
        }
        let body = if let Some(body) = req.body() {
            if let Some(bytes) = body.as_bytes() {
                serde_json::from_slice(bytes).unwrap_or(serde_json::Value::Null)
            } else {
                serde_json::Value::Null
            }
        } else {
            serde_json::Value::Null
        };
        Ok(crate::dry_run::DryRun {
            method,
            url,
            headers,
            body,
        })
    }
}

/// Merge client-level and per-request beta values into a single
/// comma-joined header value.
///
/// Order is preserved: client-level betas first, in insertion order, then
/// per-request betas. Empty or whitespace-only entries are dropped, and
/// each entry is trimmed. Returns `None` if no entries remain.
fn merge_betas(client_betas: &[String], per_request_betas: &[&str]) -> Option<String> {
    let trimmed: Vec<&str> = client_betas
        .iter()
        .map(String::as_str)
        .chain(per_request_betas.iter().copied())
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .collect();
    if trimmed.is_empty() {
        None
    } else {
        Some(trimmed.join(","))
    }
}

/// Builder for [`Client`].
#[derive(Default)]
pub struct ClientBuilder {
    api_key: Option<String>,
    base_url: Option<String>,
    user_agent: Option<String>,
    timeout: Option<Duration>,
    betas: Vec<String>,
    retry: Option<RetryPolicy>,
    http: Option<reqwest::Client>,
    signer: Option<Arc<dyn RequestSigner>>,
}

impl std::fmt::Debug for ClientBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClientBuilder")
            .field("api_key", &self.api_key.as_ref().map(|_| "<redacted>"))
            .field("base_url", &self.base_url)
            .field("user_agent", &self.user_agent)
            .field("timeout", &self.timeout)
            .field("betas", &self.betas)
            .field("retry", &self.retry)
            .field("http", &self.http.is_some())
            .field("signer", &self.signer.as_ref().map(|s| format!("{s:?}")))
            .finish()
    }
}

impl ClientBuilder {
    /// API key; required.
    #[must_use]
    pub fn api_key(mut self, k: impl Into<String>) -> Self {
        self.api_key = Some(k.into());
        self
    }

    /// Override the base URL. Useful for proxies and `wiremock`-based tests.
    /// Defaults to `https://api.anthropic.com`.
    #[must_use]
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = Some(url.into());
        self
    }

    /// Append an `anthropic-beta` value. May be called multiple times; values
    /// are comma-joined per Anthropic convention.
    #[must_use]
    pub fn beta(mut self, header_value: impl Into<String>) -> Self {
        self.betas.push(header_value.into());
        self
    }

    /// Per-request timeout applied to the underlying reqwest client.
    /// Ignored if a custom HTTP client is supplied via [`Self::http_client`].
    #[must_use]
    pub fn timeout(mut self, d: Duration) -> Self {
        self.timeout = Some(d);
        self
    }

    /// Override the default retry policy.
    #[must_use]
    pub fn retry(mut self, policy: RetryPolicy) -> Self {
        self.retry = Some(policy);
        self
    }

    /// Supply your own [`reqwest::Client`]. Lets callers reuse a connection
    /// pool, install custom middleware, or configure proxy / TLS settings
    /// outside the SDK.
    #[must_use]
    pub fn http_client(mut self, c: reqwest::Client) -> Self {
        self.http = Some(c);
        self
    }

    /// Override the `User-Agent` header. Defaults to `claude-api-rs/<version>`.
    #[must_use]
    pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
        self.user_agent = Some(ua.into());
        self
    }

    /// Install a custom [`RequestSigner`]. If unset, the builder
    /// defaults to [`ApiKeySigner`] from the configured `api_key`.
    /// Setting both is allowed: the explicit signer takes precedence
    /// (useful for tests that want a no-op signer with an unused
    /// placeholder key).
    #[must_use]
    pub fn signer(mut self, signer: Arc<dyn RequestSigner>) -> Self {
        self.signer = Some(signer);
        self
    }

    /// Construct the [`Client`]. Returns [`Error::InvalidConfig`] if
    /// neither an `api_key` nor a custom `signer` was provided.
    pub fn build(self) -> Result<Client> {
        let signer: Arc<dyn RequestSigner> = if let Some(s) = self.signer {
            s
        } else if let Some(key) = self.api_key {
            Arc::new(ApiKeySigner::new(ApiKey::new(key)))
        } else {
            return Err(Error::InvalidConfig(
                "either api_key or signer must be configured".into(),
            ));
        };

        let http = if let Some(c) = self.http {
            c
        } else {
            let mut builder = reqwest::Client::builder();
            if let Some(t) = self.timeout {
                builder = builder.timeout(t);
            }
            builder.build()?
        };

        let inner = Inner {
            base_url: self
                .base_url
                .unwrap_or_else(|| crate::DEFAULT_BASE_URL.to_owned()),
            http,
            user_agent: self
                .user_agent
                .unwrap_or_else(|| crate::USER_AGENT.to_owned()),
            betas: self.betas,
            retry: self.retry.unwrap_or_default(),
            signer,
        };

        Ok(Client {
            inner: Arc::new(inner),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use serde::Deserialize;
    use serde_json::json;
    use wiremock::matchers::{header, header_exists, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    #[derive(Deserialize, Debug, PartialEq)]
    struct Pong {
        ok: bool,
    }

    fn client_for(mock: &MockServer) -> Client {
        Client::builder()
            .api_key("sk-ant-test-key")
            .base_url(mock.uri())
            .build()
            .unwrap()
    }

    #[test]
    fn build_requires_api_key() {
        let err = Client::builder().build().unwrap_err();
        assert!(matches!(err, Error::InvalidConfig(_)), "{err:?}");
    }

    #[cfg(feature = "bedrock")]
    #[tokio::test]
    async fn bedrock_signer_replaces_x_api_key_with_sigv4_headers() {
        use crate::bedrock::{BedrockCredentials, BedrockSigner};
        use wiremock::matchers::header_regex;
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            // sigv4 always emits an Authorization header beginning with the algorithm prefix.
            .and(header_regex("authorization", "^AWS4-HMAC-SHA256 "))
            // x-amz-date is the canonical timestamp header.
            .and(header_exists("x-amz-date"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
            .mount(&mock)
            .await;

        let signer = std::sync::Arc::new(BedrockSigner::new(
            BedrockCredentials::new("AKIDEXAMPLE", "secret"),
            "us-east-1",
        ));
        let client = Client::builder()
            .api_key("sk-ant-unused")
            .base_url(mock.uri())
            .signer(signer)
            .build()
            .unwrap();

        let _: Pong = client
            .execute(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap();

        // Wiremock would 404 if the signer hadn't run; explicit
        // negative check on the live captured request:
        let received = &mock.received_requests().await.unwrap()[0];
        assert!(
            received.headers.get("x-api-key").is_none(),
            "x-api-key should not be set when a custom signer is installed",
        );
    }

    #[test]
    fn client_is_cheap_to_clone() {
        let c1 = Client::new("sk-ant-x");
        let c2 = c1.clone();
        // Both clones point at the same Arc<Inner>.
        assert!(Arc::ptr_eq(&c1.inner, &c2.inner));
    }

    #[tokio::test]
    async fn execute_sets_default_headers_and_decodes_response() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .and(header("x-api-key", "sk-ant-test-key"))
            .and(header("anthropic-version", crate::ANTHROPIC_VERSION))
            .and(header_exists("user-agent"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let resp: Pong = client
            .execute(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap();
        assert_eq!(resp, Pong { ok: true });
    }

    #[tokio::test]
    async fn beta_headers_from_builder_are_applied_and_comma_joined() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .and(header_exists("anthropic-beta"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
            .mount(&mock)
            .await;

        let client = Client::builder()
            .api_key("sk-ant-x")
            .base_url(mock.uri())
            .beta("feat-a")
            .beta("feat-b")
            .build()
            .unwrap();

        let _: Pong = client
            .execute(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap();

        let req = &mock.received_requests().await.unwrap()[0];
        let beta = req.headers.get("anthropic-beta").unwrap().to_str().unwrap();
        assert_eq!(beta, "feat-a,feat-b");
    }

    #[tokio::test]
    async fn per_request_betas_merge_with_builder_betas() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .and(header_exists("anthropic-beta"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
            .mount(&mock)
            .await;

        let client = Client::builder()
            .api_key("sk-ant-x")
            .base_url(mock.uri())
            .beta("client-level")
            .build()
            .unwrap();

        let _: Pong = client
            .execute(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &["per-req"],
            )
            .await
            .unwrap();

        let req = &mock.received_requests().await.unwrap()[0];
        let beta = req.headers.get("anthropic-beta").unwrap().to_str().unwrap();
        assert_eq!(beta, "client-level,per-req");
    }

    #[tokio::test]
    async fn no_beta_header_when_none_configured() {
        let mock = MockServer::start().await;
        // We can't easily assert "header NOT present" with wiremock matchers,
        // but if the request fails to match our (no-beta) mock, the call would
        // 404 and the assert below would fire.
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
            .expect(1)
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let _: Pong = client
            .execute(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn error_response_maps_to_api_error_with_request_id_and_retry_after() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(
                ResponseTemplate::new(429)
                    .insert_header("request-id", "req_abc123")
                    .insert_header("retry-after", "8")
                    .set_body_json(json!({
                        "type": "error",
                        "error": {
                            "type": "rate_limit_error",
                            "message": "slow down please"
                        }
                    })),
            )
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let err = client
            .execute::<Pong>(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap_err();

        match err {
            Error::Api {
                status,
                request_id,
                kind,
                message,
                retry_after,
            } => {
                assert_eq!(status, http::StatusCode::TOO_MANY_REQUESTS);
                assert_eq!(request_id.as_deref(), Some("req_abc123"));
                assert_eq!(kind, crate::error::ApiErrorKind::RateLimitError);
                assert_eq!(message, "slow down please");
                assert_eq!(retry_after, Some(Duration::from_secs(8)));
            }
            other => panic!("expected Api, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn non_json_error_body_falls_back_to_api_error() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(ResponseTemplate::new(502).set_body_string("<html>bad gateway</html>"))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let err = client
            .execute::<Pong>(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap_err();

        match err {
            Error::Api {
                status,
                message,
                kind,
                ..
            } => {
                assert_eq!(status, http::StatusCode::BAD_GATEWAY);
                assert_eq!(kind, crate::error::ApiErrorKind::ApiError);
                assert!(message.contains("bad gateway"), "{message}");
            }
            other => panic!("expected Api, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn malformed_success_body_maps_to_decode_error() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(ResponseTemplate::new(200).set_body_string("not json at all"))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let err = client
            .execute::<Pong>(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap_err();

        assert!(matches!(err, Error::Decode(_)), "{err:?}");
    }

    #[tokio::test]
    async fn custom_user_agent_overrides_default() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .and(header("user-agent", "my-app/1.0"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
            .mount(&mock)
            .await;

        let client = Client::builder()
            .api_key("sk-ant-x")
            .base_url(mock.uri())
            .user_agent("my-app/1.0")
            .build()
            .unwrap();

        let _: Pong = client
            .execute(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap();
    }

    fn fast_retry_policy() -> crate::retry::RetryPolicy {
        crate::retry::RetryPolicy {
            max_attempts: 3,
            initial_backoff: Duration::from_millis(1),
            max_backoff: Duration::from_millis(5),
            jitter: crate::retry::Jitter::None,
            respect_retry_after: false,
        }
    }

    #[tokio::test]
    async fn execute_with_retry_succeeds_after_transient_failure() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(ResponseTemplate::new(503))
            .up_to_n_times(1)
            .mount(&mock)
            .await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
            .mount(&mock)
            .await;

        let client = Client::builder()
            .api_key("sk-ant-x")
            .base_url(mock.uri())
            .retry(fast_retry_policy())
            .build()
            .unwrap();

        let resp: Pong = client
            .execute_with_retry(
                || client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap();
        assert!(resp.ok);
        // Two requests total: one 503 retry + one success.
        assert_eq!(mock.received_requests().await.unwrap().len(), 2);
    }

    #[tokio::test]
    async fn execute_with_retry_gives_up_after_max_attempts() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(ResponseTemplate::new(503))
            .mount(&mock)
            .await;

        let client = Client::builder()
            .api_key("sk-ant-x")
            .base_url(mock.uri())
            .retry(fast_retry_policy())
            .build()
            .unwrap();

        let err = client
            .execute_with_retry::<Pong, _>(
                || client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap_err();
        assert_eq!(err.status(), Some(http::StatusCode::SERVICE_UNAVAILABLE));
        // max_attempts = 3 -> 3 total requests.
        assert_eq!(mock.received_requests().await.unwrap().len(), 3);
    }

    #[tokio::test]
    async fn execute_with_retry_does_not_retry_non_retryable_errors() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(ResponseTemplate::new(400).set_body_json(json!({
                "type": "error",
                "error": {"type": "invalid_request_error", "message": "bad input"}
            })))
            .expect(1)
            .mount(&mock)
            .await;

        let client = Client::builder()
            .api_key("sk-ant-x")
            .base_url(mock.uri())
            .retry(fast_retry_policy())
            .build()
            .unwrap();

        let err = client
            .execute_with_retry::<Pong, _>(
                || client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap_err();
        assert_eq!(err.status(), Some(http::StatusCode::BAD_REQUEST));
        assert_eq!(mock.received_requests().await.unwrap().len(), 1);
    }

    #[tokio::test]
    async fn execute_with_retry_honors_retry_after_header() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(
                ResponseTemplate::new(429)
                    .insert_header("retry-after", "0")
                    .set_body_json(json!({
                        "type": "error",
                        "error": {"type": "rate_limit_error", "message": "slow down"}
                    })),
            )
            .up_to_n_times(1)
            .mount(&mock)
            .await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
            .mount(&mock)
            .await;

        let client = Client::builder()
            .api_key("sk-ant-x")
            .base_url(mock.uri())
            .retry(crate::retry::RetryPolicy {
                respect_retry_after: true,
                ..fast_retry_policy()
            })
            .build()
            .unwrap();

        let resp: Pong = client
            .execute_with_retry(
                || client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &[],
            )
            .await
            .unwrap();
        assert!(resp.ok);
    }

    #[test]
    fn builder_collects_betas_in_order() {
        let client = Client::builder()
            .api_key("sk-ant-x")
            .beta("a")
            .beta("b")
            .beta("c")
            .build()
            .unwrap();
        assert_eq!(
            client.betas(),
            &["a".to_owned(), "b".to_owned(), "c".to_owned()]
        );
    }

    #[test]
    fn merge_betas_returns_none_when_all_inputs_empty_or_whitespace() {
        assert_eq!(merge_betas(&[], &[]), None);
        assert_eq!(
            merge_betas(&[String::new(), "   ".into()], &["", "  "]),
            None
        );
    }

    #[test]
    fn merge_betas_filters_empties_and_trims() {
        let client_betas = vec!["  feat-a  ".to_owned(), String::new(), "feat-b".to_owned()];
        let per_request = ["", "feat-c\n", "  "];
        assert_eq!(
            merge_betas(&client_betas, &per_request).as_deref(),
            Some("feat-a,feat-b,feat-c")
        );
    }

    #[test]
    fn merge_betas_preserves_order_client_then_per_request() {
        assert_eq!(
            merge_betas(&["x".into(), "y".into()], &["z", "w"]).as_deref(),
            Some("x,y,z,w")
        );
    }

    #[test]
    fn merge_betas_keeps_duplicates_intact() {
        // Dedup is intentionally NOT performed; users manage their own set.
        assert_eq!(
            merge_betas(&["foo".into()], &["foo"]).as_deref(),
            Some("foo,foo")
        );
    }

    #[tokio::test]
    async fn beta_header_omits_when_only_whitespace_supplied() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/ping"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"ok": true})))
            .mount(&mock)
            .await;

        let client = Client::builder()
            .api_key("sk-ant-x")
            .base_url(mock.uri())
            .beta("   ")
            .beta("")
            .build()
            .unwrap();

        let _: Pong = client
            .execute(
                client.request_builder(reqwest::Method::GET, "/v1/ping"),
                &["  "],
            )
            .await
            .unwrap();

        let req = &mock.received_requests().await.unwrap()[0];
        assert!(
            req.headers.get("anthropic-beta").is_none(),
            "expected no anthropic-beta header when all values are whitespace"
        );
    }
}