claude-api 0.5.1

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
//! The Messages namespace: [`Messages::create`], [`Messages::count_tokens`],
//! and their `_with_beta` siblings.
//!
//! Obtain via [`Client::messages`](crate::Client::messages).
//!
//! Streaming (`create_stream`) lands in task #12.

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

use crate::client::Client;
use crate::dry_run::DryRun;
use crate::error::Result;
use crate::messages::request::{CountTokensRequest, CreateMessageRequest};
use crate::messages::response::{CountTokensResponse, Message};

#[cfg(feature = "streaming")]
use crate::messages::stream::EventStream;

/// Namespace handle for the Messages API.
///
/// Obtained via [`Client::messages`](crate::Client::messages); cheap to
/// construct (just borrows the client).
pub struct Messages<'a> {
    client: &'a Client,
}

impl<'a> Messages<'a> {
    pub(crate) fn new(client: &'a Client) -> Self {
        Self { client }
    }

    /// Send a request to `POST /v1/messages` and return the full response.
    ///
    /// Retries are governed by the [`RetryPolicy`](crate::retry::RetryPolicy)
    /// configured on the client.
    pub async fn create(&self, request: CreateMessageRequest) -> Result<Message> {
        self.create_with_beta(request, &[]).await
    }

    /// Like [`Self::create`] but with additional per-request beta headers
    /// merged into `anthropic-beta`.
    pub async fn create_with_beta(
        &self,
        request: CreateMessageRequest,
        betas: &[&str],
    ) -> Result<Message> {
        let request_ref = &request;
        self.client
            .execute_with_retry(
                || {
                    self.client
                        .request_builder(reqwest::Method::POST, "/v1/messages")
                        .json(request_ref)
                },
                betas,
            )
            .await
    }

    /// Count the input tokens that would be billed for the given request,
    /// without invoking the model.
    pub async fn count_tokens(&self, request: CountTokensRequest) -> Result<CountTokensResponse> {
        self.count_tokens_with_beta(request, &[]).await
    }

    /// Like [`Self::count_tokens`] but with per-request beta headers.
    pub async fn count_tokens_with_beta(
        &self,
        request: CountTokensRequest,
        betas: &[&str],
    ) -> Result<CountTokensResponse> {
        let request_ref = &request;
        self.client
            .execute_with_retry(
                || {
                    self.client
                        .request_builder(reqwest::Method::POST, "/v1/messages/count_tokens")
                        .json(request_ref)
                },
                betas,
            )
            .await
    }

    /// Render the HTTP request that [`Self::create`] would send, without
    /// firing it. Returns a [`DryRun`] containing the rendered method, URL,
    /// headers, and JSON body. Useful for tests, debugging, and reproducing
    /// requests as curl commands.
    pub fn dry_run(&self, request: &CreateMessageRequest) -> Result<DryRun> {
        self.dry_run_with_beta(request, &[])
    }

    /// Like [`Self::dry_run`] but with per-request beta headers merged in,
    /// matching [`Self::create_with_beta`].
    pub fn dry_run_with_beta(
        &self,
        request: &CreateMessageRequest,
        betas: &[&str],
    ) -> Result<DryRun> {
        let builder = self
            .client
            .request_builder(reqwest::Method::POST, "/v1/messages")
            .json(request);
        self.client.render_dry_run(builder, betas)
    }

    /// Estimate the USD cost of a request before sending it.
    ///
    /// Hits `/v1/messages/count_tokens` to get the exact server-counted
    /// input-token total, then uses `request.max_tokens` as the output upper
    /// bound. The returned [`CostPreview`](crate::cost_preview::CostPreview)
    /// exposes input / max-output / max-total cost components.
    ///
    /// Pre-flight charge from this call: one count-tokens request
    /// (significantly cheaper than a full generation, but not free).
    #[cfg(feature = "pricing")]
    #[cfg_attr(docsrs, doc(cfg(feature = "pricing")))]
    pub async fn cost_preview(
        &self,
        request: &CreateMessageRequest,
        pricing: &crate::pricing::PricingTable,
    ) -> Result<crate::cost_preview::CostPreview> {
        use crate::types::Usage;
        let count = self.count_tokens(CountTokensRequest::from(request)).await?;
        let input_tokens = count.input_tokens;
        let max_output_tokens = request.max_tokens;
        let input_cost_usd = pricing.cost(
            &request.model,
            &Usage {
                input_tokens,
                output_tokens: 0,
                ..Usage::default()
            },
        );
        let max_total_usd = pricing.cost(
            &request.model,
            &Usage {
                input_tokens,
                output_tokens: max_output_tokens,
                ..Usage::default()
            },
        );
        let max_output_cost_usd = max_total_usd - input_cost_usd;
        Ok(crate::cost_preview::CostPreview {
            model: request.model.clone(),
            input_tokens,
            max_output_tokens,
            input_cost_usd,
            max_output_cost_usd,
            max_total_usd,
        })
    }

    /// Like [`Self::cost_preview`] but consults a
    /// [`CountTokensCache`](crate::cost_preview::CountTokensCache)
    /// before issuing the network call. Cache hits avoid the
    /// `count_tokens` round-trip entirely. Useful for IDE integrations
    /// and long-running agents that preview the same prompt many times.
    #[cfg(feature = "pricing")]
    #[cfg_attr(docsrs, doc(cfg(feature = "pricing")))]
    pub async fn cost_preview_cached(
        &self,
        request: &CreateMessageRequest,
        pricing: &crate::pricing::PricingTable,
        cache: &crate::cost_preview::CountTokensCache,
    ) -> Result<crate::cost_preview::CostPreview> {
        use crate::types::Usage;
        let count_req = CountTokensRequest::from(request);
        let key = crate::cost_preview::hash_request(&count_req);

        let input_tokens = if let Some(cached) = cache.get(key) {
            cached
        } else {
            let count = self.count_tokens(count_req).await?;
            cache.put(key, count.input_tokens);
            count.input_tokens
        };

        let max_output_tokens = request.max_tokens;
        let input_cost_usd = pricing.cost(
            &request.model,
            &Usage {
                input_tokens,
                output_tokens: 0,
                ..Usage::default()
            },
        );
        let max_total_usd = pricing.cost(
            &request.model,
            &Usage {
                input_tokens,
                output_tokens: max_output_tokens,
                ..Usage::default()
            },
        );
        let max_output_cost_usd = max_total_usd - input_cost_usd;
        Ok(crate::cost_preview::CostPreview {
            model: request.model.clone(),
            input_tokens,
            max_output_tokens,
            input_cost_usd,
            max_output_cost_usd,
            max_total_usd,
        })
    }

    /// Render the HTTP request that [`Self::count_tokens`] would send.
    pub fn dry_run_count_tokens(&self, request: &CountTokensRequest) -> Result<DryRun> {
        self.dry_run_count_tokens_with_beta(request, &[])
    }

    /// Like [`Self::dry_run_count_tokens`] but with per-request beta headers.
    pub fn dry_run_count_tokens_with_beta(
        &self,
        request: &CountTokensRequest,
        betas: &[&str],
    ) -> Result<DryRun> {
        let builder = self
            .client
            .request_builder(reqwest::Method::POST, "/v1/messages/count_tokens")
            .json(request);
        self.client.render_dry_run(builder, betas)
    }

    /// Open a streaming connection to `POST /v1/messages` and return an
    /// [`EventStream`].
    ///
    /// The returned stream yields [`StreamEvent`](crate::messages::stream::StreamEvent)s
    /// as they arrive; call [`EventStream::aggregate`] to drive it to
    /// completion and reconstruct the final [`Message`].
    ///
    /// Streaming requests are *not* retried.
    #[cfg(feature = "streaming")]
    #[cfg_attr(docsrs, doc(cfg(feature = "streaming")))]
    pub async fn create_stream(&self, request: CreateMessageRequest) -> Result<EventStream> {
        self.create_stream_with_beta(request, &[]).await
    }

    /// Like [`Self::create_stream`] but with per-request beta headers.
    #[cfg(feature = "streaming")]
    #[cfg_attr(docsrs, doc(cfg(feature = "streaming")))]
    pub async fn create_stream_with_beta(
        &self,
        mut request: CreateMessageRequest,
        betas: &[&str],
    ) -> Result<EventStream> {
        request.stream = true;
        let response = self
            .client
            .execute_streaming(
                self.client
                    .request_builder(reqwest::Method::POST, "/v1/messages")
                    .json(&request),
                betas,
            )
            .await?;
        Ok(EventStream::from_response(response))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::messages::input::MessageInput;
    use crate::messages::response::Message;
    use crate::types::{ModelId, Role, StopReason};
    use pretty_assertions::assert_eq;
    use serde_json::json;
    use wiremock::matchers::{body_partial_json, header, header_exists, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

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

    fn fake_response_body() -> serde_json::Value {
        json!({
            "id": "msg_test",
            "type": "message",
            "role": "assistant",
            "content": [{"type": "text", "text": "Hi!"}],
            "model": "claude-sonnet-4-6",
            "stop_reason": "end_turn",
            "usage": {"input_tokens": 5, "output_tokens": 2}
        })
    }

    #[tokio::test]
    async fn create_posts_to_v1_messages_with_typed_request_body() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .and(header("x-api-key", "sk-ant-test"))
            .and(header("anthropic-version", crate::ANTHROPIC_VERSION))
            .and(body_partial_json(json!({
                "model": "claude-sonnet-4-6",
                "max_tokens": 64,
                "messages": [{"role": "user", "content": "hi"}]
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(fake_response_body()))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(64)
            .user("hi")
            .build()
            .unwrap();
        let resp = client.messages().create(req).await.unwrap();

        assert_eq!(resp.id, "msg_test");
        assert_eq!(resp.role, Role::Assistant);
        assert_eq!(resp.stop_reason, Some(StopReason::EndTurn));
        assert_eq!(resp.usage.input_tokens, 5);
    }

    #[tokio::test]
    async fn create_with_beta_attaches_per_request_beta_header() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .and(header_exists("anthropic-beta"))
            .respond_with(ResponseTemplate::new(200).set_body_json(fake_response_body()))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(8)
            .user("x")
            .build()
            .unwrap();

        let _: Message = client
            .messages()
            .create_with_beta(req, &["computer-use-2025-01-24"])
            .await
            .unwrap();

        let received = &mock.received_requests().await.unwrap()[0];
        let beta = received
            .headers
            .get("anthropic-beta")
            .unwrap()
            .to_str()
            .unwrap();
        assert_eq!(beta, "computer-use-2025-01-24");
    }

    #[tokio::test]
    async fn dry_run_renders_request_without_sending() {
        // No mock mounted: any actual HTTP call would 404 the test.
        let mock = MockServer::start().await;
        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(64)
            .user("hello")
            .build()
            .unwrap();

        let dr = client.messages().dry_run(&req).unwrap();

        assert_eq!(dr.method, reqwest::Method::POST);
        assert_eq!(dr.url, format!("{}/v1/messages", mock.uri()));
        assert_eq!(dr.headers.get("x-api-key").unwrap(), "sk-ant-test");
        assert_eq!(
            dr.headers.get("anthropic-version").unwrap(),
            crate::ANTHROPIC_VERSION
        );
        // Body round-trips the request.
        assert_eq!(dr.body["model"], "claude-sonnet-4-6");
        assert_eq!(dr.body["max_tokens"], 64);
        assert_eq!(dr.body["messages"][0]["role"], "user");

        // No request actually went out.
        assert_eq!(mock.received_requests().await.unwrap().len(), 0);
    }

    #[tokio::test]
    async fn dry_run_with_beta_includes_anthropic_beta_header() {
        let mock = MockServer::start().await;
        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(8)
            .user("x")
            .build()
            .unwrap();

        let dr = client
            .messages()
            .dry_run_with_beta(&req, &["computer-use-2025-01-24"])
            .unwrap();

        assert_eq!(
            dr.headers.get("anthropic-beta").unwrap(),
            "computer-use-2025-01-24"
        );
    }

    #[tokio::test]
    async fn dry_run_count_tokens_uses_count_tokens_path() {
        let mock = MockServer::start().await;
        let client = client_for(&mock);
        let req = CountTokensRequest::builder()
            .model(ModelId::HAIKU_4_5)
            .user("x")
            .build()
            .unwrap();

        let dr = client.messages().dry_run_count_tokens(&req).unwrap();
        assert!(dr.url.ends_with("/v1/messages/count_tokens"));
        assert_eq!(dr.body["model"], "claude-haiku-4-5-20251001");
    }

    #[tokio::test]
    async fn create_propagates_api_error_with_request_id() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .respond_with(
                ResponseTemplate::new(400)
                    .insert_header("request-id", "req_xyz")
                    .set_body_json(json!({
                        "type": "error",
                        "error": {"type": "invalid_request_error", "message": "bad input"}
                    })),
            )
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(8)
            .user("x")
            .build()
            .unwrap();

        let err = client.messages().create(req).await.unwrap_err();
        assert_eq!(err.request_id(), Some("req_xyz"));
        assert_eq!(err.status(), Some(http::StatusCode::BAD_REQUEST));
    }

    #[tokio::test]
    async fn count_tokens_posts_to_count_tokens_endpoint() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages/count_tokens"))
            .and(body_partial_json(json!({
                "model": "claude-haiku-4-5-20251001",
                "messages": [{"role": "user", "content": "x"}]
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"input_tokens": 7})))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CountTokensRequest::builder()
            .model(ModelId::HAIKU_4_5)
            .user("x")
            .build()
            .unwrap();
        let resp = client.messages().count_tokens(req).await.unwrap();
        assert_eq!(resp.input_tokens, 7);
    }

    #[cfg(feature = "pricing")]
    #[tokio::test]
    async fn cost_preview_calls_count_tokens_and_computes_bounds() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages/count_tokens"))
            .and(body_partial_json(json!({
                "model": "claude-sonnet-4-6",
                "messages": [{"role": "user", "content": "hi"}]
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"input_tokens": 1000})))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(2000)
            .user("hi")
            .build()
            .unwrap();

        let pricing = crate::pricing::PricingTable::default();
        let preview = client
            .messages()
            .cost_preview(&req, &pricing)
            .await
            .unwrap();

        // Sonnet 4.6 default rates: $3/Mtok input, $15/Mtok output
        assert_eq!(preview.input_tokens, 1000);
        assert_eq!(preview.max_output_tokens, 2000);
        // 1000 input * $3/Mtok = $0.003
        assert!((preview.input_cost_usd - 0.003).abs() < 1e-9);
        // 2000 output * $15/Mtok = $0.030
        assert!((preview.max_output_cost_usd - 0.030).abs() < 1e-9);
        // total
        assert!((preview.max_total_usd - 0.033).abs() < 1e-9);
    }

    #[cfg(feature = "pricing")]
    #[tokio::test]
    async fn cost_preview_cost_for_returns_point_estimate() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages/count_tokens"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"input_tokens": 1000})))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(5000)
            .user("hi")
            .build()
            .unwrap();

        let pricing = crate::pricing::PricingTable::default();
        let preview = client
            .messages()
            .cost_preview(&req, &pricing)
            .await
            .unwrap();
        // 1000 input + 500 output: $0.003 + $0.0075 = $0.0105
        let estimate = preview.cost_for(500, &pricing);
        assert!((estimate - 0.0105).abs() < 1e-9);
    }

    #[cfg(feature = "pricing")]
    #[tokio::test]
    async fn cost_preview_cached_skips_network_on_hit() {
        let mock = MockServer::start().await;
        // Mount with .expect(1): test fails if more than one count_tokens
        // call happens, which is exactly the regression we want to catch.
        Mock::given(method("POST"))
            .and(path("/v1/messages/count_tokens"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"input_tokens": 42})))
            .expect(1)
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(64)
            .user("hi")
            .build()
            .unwrap();
        let pricing = crate::pricing::PricingTable::default();
        let cache = crate::cost_preview::CountTokensCache::new(8);

        let p1 = client
            .messages()
            .cost_preview_cached(&req, &pricing, &cache)
            .await
            .unwrap();
        let p2 = client
            .messages()
            .cost_preview_cached(&req, &pricing, &cache)
            .await
            .unwrap();

        assert_eq!(p1, p2);
        assert_eq!(cache.len(), 1);
    }

    #[cfg(feature = "pricing")]
    #[tokio::test]
    async fn cost_preview_cached_distinguishes_different_requests() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages/count_tokens"))
            .and(body_partial_json(
                json!({"messages": [{"role": "user", "content": "alpha"}]}),
            ))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"input_tokens": 100})))
            .mount(&mock)
            .await;
        Mock::given(method("POST"))
            .and(path("/v1/messages/count_tokens"))
            .and(body_partial_json(
                json!({"messages": [{"role": "user", "content": "beta"}]}),
            ))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"input_tokens": 200})))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let pricing = crate::pricing::PricingTable::default();
        let cache = crate::cost_preview::CountTokensCache::new(8);

        let req_a = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(64)
            .user("alpha")
            .build()
            .unwrap();
        let req_b = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(64)
            .user("beta")
            .build()
            .unwrap();

        let pa = client
            .messages()
            .cost_preview_cached(&req_a, &pricing, &cache)
            .await
            .unwrap();
        let pb = client
            .messages()
            .cost_preview_cached(&req_b, &pricing, &cache)
            .await
            .unwrap();

        assert_eq!(pa.input_tokens, 100);
        assert_eq!(pb.input_tokens, 200);
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn count_tokens_request_from_create_drops_max_tokens_and_sampling() {
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(64)
            .temperature(0.7)
            .user("hello")
            .build()
            .unwrap();

        let count_req = CountTokensRequest::from(&req);
        assert_eq!(count_req.model, req.model);
        assert_eq!(count_req.messages.len(), 1);
        // Serialized form should not contain max_tokens or temperature.
        let body = serde_json::to_value(&count_req).unwrap();
        assert!(body.get("max_tokens").is_none());
        assert!(body.get("temperature").is_none());
    }

    #[tokio::test]
    async fn create_appends_assistant_prefill_in_history() {
        // Verifies that the .assistant() builder method works end-to-end.
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .and(body_partial_json(json!({
                "messages": [
                    {"role": "user", "content": "hi"},
                    {"role": "assistant", "content": "Sure, "}
                ]
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(fake_response_body()))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(8)
            .user("hi")
            .assistant("Sure, ")
            .build()
            .unwrap();
        let _ = client.messages().create(req).await.unwrap();
    }

    #[tokio::test]
    async fn create_retries_on_overloaded_then_succeeds() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .respond_with(ResponseTemplate::new(529))
            .up_to_n_times(1)
            .mount(&mock)
            .await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .respond_with(ResponseTemplate::new(200).set_body_json(fake_response_body()))
            .mount(&mock)
            .await;

        // Use a tiny retry policy so the test is fast.
        let client = Client::builder()
            .api_key("sk-ant-x")
            .base_url(mock.uri())
            .retry(crate::retry::RetryPolicy {
                max_attempts: 3,
                initial_backoff: std::time::Duration::from_millis(1),
                max_backoff: std::time::Duration::from_millis(5),
                jitter: crate::retry::Jitter::None,
                respect_retry_after: false,
            })
            .build()
            .unwrap();

        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(8)
            .user("x")
            .build()
            .unwrap();
        let resp = client.messages().create(req).await.unwrap();
        assert_eq!(resp.id, "msg_test");
        assert_eq!(mock.received_requests().await.unwrap().len(), 2);
    }

    #[test]
    fn messages_namespace_borrows_client() {
        // Sanity check the borrow shape: dropping the messages handle leaves
        // the client usable.
        let client = Client::new("sk-ant-x");
        {
            let _m = client.messages();
        }
        let _ = client.messages();

        // Suppress unused warning for MessageInput (it's used transitively via builder).
        let _: MessageInput = MessageInput::user("x");
    }

    // -----------------------------------------------------------------
    // Streaming end-to-end (gated on streaming feature)
    // -----------------------------------------------------------------

    #[cfg(feature = "streaming")]
    fn sse_corpus() -> &'static str {
        // A typical Hello-world streamed message, end to end.
        concat!(
            "event: message_start\n",
            "data: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_S\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"claude-sonnet-4-6\",\"usage\":{\"input_tokens\":3,\"output_tokens\":0}}}\n",
            "\n",
            "event: content_block_start\n",
            "data: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n",
            "\n",
            "event: ping\n",
            "data: {\"type\":\"ping\"}\n",
            "\n",
            "event: content_block_delta\n",
            "data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Hello\"}}\n",
            "\n",
            "event: content_block_delta\n",
            "data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\" world\"}}\n",
            "\n",
            "event: content_block_stop\n",
            "data: {\"type\":\"content_block_stop\",\"index\":0}\n",
            "\n",
            "event: message_delta\n",
            "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"input_tokens\":3,\"output_tokens\":2}}\n",
            "\n",
            "event: message_stop\n",
            "data: {\"type\":\"message_stop\"}\n",
            "\n",
        )
    }

    #[cfg(feature = "streaming")]
    #[tokio::test]
    async fn create_stream_aggregates_to_full_message() {
        use crate::messages::content::{ContentBlock, KnownBlock};
        use crate::messages::stream::EventStream;

        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .and(body_partial_json(json!({"stream": true})))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("content-type", "text/event-stream")
                    .set_body_string(sse_corpus()),
            )
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(8)
            .user("hi")
            .build()
            .unwrap();

        let stream: EventStream = client.messages().create_stream(req).await.unwrap();
        let msg = stream.aggregate().await.unwrap();

        assert_eq!(msg.id, "msg_S");
        assert_eq!(msg.stop_reason, Some(StopReason::EndTurn));
        assert_eq!(msg.usage.output_tokens, 2);
        assert_eq!(msg.content.len(), 1);
        match &msg.content[0] {
            ContentBlock::Known(KnownBlock::Text { text, .. }) => {
                assert_eq!(text, "Hello world");
            }
            _ => panic!("expected text block"),
        }
    }

    #[cfg(feature = "streaming")]
    #[tokio::test]
    async fn create_stream_yields_individual_events_for_iterator_use() {
        use futures_util::StreamExt;

        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("content-type", "text/event-stream")
                    .set_body_string(sse_corpus()),
            )
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(8)
            .user("hi")
            .build()
            .unwrap();

        let mut stream = client.messages().create_stream(req).await.unwrap();
        let mut count = 0;
        let mut saw_message_stop = false;
        while let Some(ev) = stream.next().await {
            let ev = ev.unwrap();
            count += 1;
            if ev.type_tag() == Some("message_stop") {
                saw_message_stop = true;
            }
        }
        assert!(saw_message_stop, "expected to see message_stop event");
        assert!(count >= 7, "expected at least 7 events, got {count}");
    }

    #[cfg(feature = "streaming")]
    #[tokio::test]
    async fn create_stream_propagates_connect_error() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .respond_with(
                ResponseTemplate::new(401)
                    .insert_header("request-id", "req_unauth")
                    .set_body_json(json!({
                        "type": "error",
                        "error": {"type": "authentication_error", "message": "bad key"}
                    })),
            )
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(8)
            .user("hi")
            .build()
            .unwrap();

        let err = client.messages().create_stream(req).await.unwrap_err();
        assert_eq!(err.status(), Some(http::StatusCode::UNAUTHORIZED));
        assert_eq!(err.request_id(), Some("req_unauth"));
    }

    #[cfg(feature = "streaming")]
    #[tokio::test]
    async fn create_stream_sets_stream_true_in_request_body() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .and(body_partial_json(json!({"stream": true})))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("content-type", "text/event-stream")
                    .set_body_string(sse_corpus()),
            )
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(8)
            .user("x")
            .build()
            .unwrap();
        // The body matcher above is the actual assertion; if `stream: true`
        // wasn't sent, the mock would 404.
        let _ = client.messages().create_stream(req).await.unwrap();
    }
}