rust-claude-sdk 0.2.0

A small typed client for Anthropic's Messages API: citations, prompt caching, refusal fallbacks, server tools, retries, streaming, batches, files and models.
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
//! The body of `POST /v1/messages`.
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

use crate::response::{Citation, ContentBlock};

/// Beta header for `fallbacks: "default"`.
pub(crate) const BETA_FALLBACK_DEFAULT: &str = "server-side-fallback-2026-07-01";
/// Beta header for the array form `fallbacks: [{"model": ...}]`.
pub(crate) const BETA_FALLBACK_LIST: &str = "server-side-fallback-2026-06-01";

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct MessagesRequest {
    pub model: String,
    pub max_tokens: u32,
    pub messages: Vec<MessageParam>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system: Option<System>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<ThinkingConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_config: Option<OutputConfig>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tools: Vec<ToolDefinition>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ToolChoice>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub stop_sequences: Vec<String>,
    /// Top-level automatic caching: the API places the breakpoint on the last cacheable block.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
    /// Server-side retry of a refused request on another model. The client adds the beta
    /// header this form needs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fallbacks: Option<Fallbacks>,
    /// Set by `Client::stream`; leave it `None`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
    /// Further `anthropic-beta` values for this request.
    #[serde(skip)]
    pub betas: Vec<String>,
    /// Any other top-level parameter, sent as is (e.g. `inference_geo`, `metadata`).
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

impl MessagesRequest {
    pub fn new(model: impl Into<String>, max_tokens: u32) -> MessagesRequest {
        MessagesRequest {
            model: model.into(),
            max_tokens,
            messages: Vec::new(),
            system: None,
            thinking: None,
            output_config: None,
            tools: Vec::new(),
            tool_choice: None,
            stop_sequences: Vec::new(),
            cache_control: None,
            fallbacks: None,
            stream: None,
            betas: Vec::new(),
            extra: Map::new(),
        }
    }

    pub fn system(mut self, text: impl Into<String>) -> Self {
        self.system = Some(System::Text(text.into()));
        self
    }

    /// A system prompt marked as a cache breakpoint: keep it byte-identical across requests
    /// (no dates or ids in it) so later requests read it from the cache.
    pub fn system_cached(mut self, text: impl Into<String>) -> Self {
        self.system = Some(System::Blocks(vec![SystemBlock { text: text.into(), cache_control: Some(CacheControl::ephemeral()) }]));
        self
    }

    pub fn message(mut self, message: MessageParam) -> Self {
        self.messages.push(message);
        self
    }

    pub fn user(self, text: impl Into<String>) -> Self {
        self.message(MessageParam::user_text(text))
    }

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

    /// Sets `output_config.effort`. Not every model accepts it: Claude Haiku 4.5 and
    /// Claude Sonnet 4.5 reject the parameter (use `ThinkingConfig::Enabled` there),
    /// Claude Opus 4.5 takes only `Low`/`Medium`/`High`, and Claude Opus 4.6 / Sonnet 4.6
    /// do not take `Xhigh`. The SDK does not check this; the API returns an error.
    pub fn effort(mut self, effort: Effort) -> Self {
        self.output_config.get_or_insert_with(OutputConfig::default).effort = Some(effort);
        self
    }

    pub fn tool(mut self, tool: impl Into<ToolDefinition>) -> Self {
        self.tools.push(tool.into());
        self
    }

    pub fn tool_choice(mut self, choice: ToolChoice) -> Self {
        self.tool_choice = Some(choice);
        self
    }

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

    pub fn beta(mut self, beta: impl Into<String>) -> Self {
        self.betas.push(beta.into());
        self
    }

    /// Any other top-level parameter.
    pub fn param(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
        self.extra.insert(key.into(), value.into());
        self
    }

    /// The `anthropic-beta` values this request needs: its own, then the one its
    /// `fallbacks` form requires (unless already present), without duplicates.
    pub fn beta_header_values(&self) -> Vec<String> {
        let mut out: Vec<String> = Vec::new();
        let needed = match &self.fallbacks {
            Some(Fallbacks::Default) => Some(BETA_FALLBACK_DEFAULT),
            Some(Fallbacks::Models(_)) => Some(BETA_FALLBACK_LIST),
            None => None,
        };
        for b in self.betas.iter().map(String::as_str).chain(needed) {
            if !out.iter().any(|o| o == b) {
                out.push(b.to_string());
            }
        }
        out
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    User,
    Assistant,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct MessageParam {
    pub role: Role,
    pub content: Content,
}

impl MessageParam {
    pub fn user_text(text: impl Into<String>) -> MessageParam {
        MessageParam { role: Role::User, content: Content::Text(text.into()) }
    }

    pub fn user(blocks: Vec<ContentBlockParam>) -> MessageParam {
        MessageParam { role: Role::User, content: Content::Blocks(blocks) }
    }

    pub fn assistant(blocks: Vec<ContentBlockParam>) -> MessageParam {
        MessageParam { role: Role::Assistant, content: Content::Blocks(blocks) }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(untagged)]
pub enum Content {
    Text(String),
    Blocks(Vec<ContentBlockParam>),
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentBlockParam {
    Text {
        text: String,
        /// Only on echoed assistant text that carried citations.
        #[serde(skip_serializing_if = "Option::is_none")]
        citations: Option<Vec<Citation>>,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },
    Image {
        source: ImageSource,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },
    Document {
        source: DocumentSource,
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Shown to the model but never cited from.
        #[serde(skip_serializing_if = "Option::is_none")]
        context: Option<String>,
        /// Citations must be enabled on all documents of a request or on none.
        #[serde(skip_serializing_if = "Option::is_none")]
        citations: Option<CitationsConfig>,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },
    ToolUse {
        id: String,
        name: String,
        input: Value,
    },
    ToolResult {
        tool_use_id: String,
        content: ToolResultContent,
        #[serde(skip_serializing_if = "Option::is_none")]
        is_error: Option<bool>,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },
    /// Echoed back unchanged on the same model.
    Thinking {
        thinking: String,
        signature: String,
    },
    RedactedThinking {
        data: String,
    },
    /// Any other block, sent verbatim (e.g. an echoed block type this crate does not model).
    #[serde(untagged)]
    Raw(Value),
}

impl ContentBlockParam {
    pub fn text(text: impl Into<String>) -> ContentBlockParam {
        ContentBlockParam::Text { text: text.into(), citations: None, cache_control: None }
    }

    /// A plain-text document; the API splits it into sentences for citations.
    pub fn text_document(data: impl Into<String>, title: impl Into<String>) -> ContentBlockParam {
        ContentBlockParam::Document {
            source: DocumentSource::Text { media_type: "text/plain".into(), data: data.into() },
            title: Some(title.into()),
            context: None,
            citations: None,
            cache_control: None,
        }
    }

    /// A document made of chunks you choose; citations point at chunk indices.
    pub fn chunked_document(chunks: impl IntoIterator<Item = impl Into<String>>, title: impl Into<String>) -> ContentBlockParam {
        let content = chunks.into_iter().map(|c| ContentBlockParam::text(c)).collect();
        ContentBlockParam::Document {
            source: DocumentSource::Content { content },
            title: Some(title.into()),
            context: None,
            citations: None,
            cache_control: None,
        }
    }

    pub fn base64_pdf(data: impl Into<String>) -> ContentBlockParam {
        ContentBlockParam::Document {
            source: DocumentSource::Base64 { media_type: "application/pdf".into(), data: data.into() },
            title: None,
            context: None,
            citations: None,
            cache_control: None,
        }
    }

    /// A document uploaded with the Files API (PDF or plain text).
    pub fn file_document(file_id: impl Into<String>) -> ContentBlockParam {
        ContentBlockParam::Document {
            source: DocumentSource::File { file_id: file_id.into() },
            title: None,
            context: None,
            citations: None,
            cache_control: None,
        }
    }

    /// An image uploaded with the Files API.
    pub fn file_image(file_id: impl Into<String>) -> ContentBlockParam {
        ContentBlockParam::Image { source: ImageSource::File { file_id: file_id.into() }, cache_control: None }
    }

    pub fn tool_result(tool_use_id: impl Into<String>, content: impl Into<String>, is_error: bool) -> ContentBlockParam {
        ContentBlockParam::ToolResult {
            tool_use_id: tool_use_id.into(),
            content: ToolResultContent::Text(content.into()),
            is_error: is_error.then_some(true),
            cache_control: None,
        }
    }

    /// Enables citations on a document; no effect on other blocks.
    pub fn with_citations(mut self) -> Self {
        if let ContentBlockParam::Document { citations, .. } = &mut self {
            *citations = Some(CitationsConfig { enabled: true });
        }
        self
    }

    /// Sets the document's non-citable context; no effect on other blocks.
    pub fn with_context(mut self, text: impl Into<String>) -> Self {
        if let ContentBlockParam::Document { context, .. } = &mut self {
            *context = Some(text.into());
        }
        self
    }

    /// Marks this block as a cache breakpoint; no effect on blocks that take none.
    pub fn with_cache_control(mut self, cc: CacheControl) -> Self {
        match &mut self {
            ContentBlockParam::Text { cache_control, .. }
            | ContentBlockParam::Image { cache_control, .. }
            | ContentBlockParam::Document { cache_control, .. }
            | ContentBlockParam::ToolResult { cache_control, .. } => *cache_control = Some(cc),
            _ => {}
        }
        self
    }
}

/// Echoing an assistant turn: every block goes back as received.
impl From<ContentBlock> for ContentBlockParam {
    fn from(block: ContentBlock) -> ContentBlockParam {
        match block {
            ContentBlock::Text { text, citations } => {
                ContentBlockParam::Text { text, citations: (!citations.is_empty()).then_some(citations), cache_control: None }
            }
            ContentBlock::Thinking { thinking, signature } => ContentBlockParam::Thinking { thinking, signature },
            ContentBlock::RedactedThinking { data } => ContentBlockParam::RedactedThinking { data },
            ContentBlock::ToolUse { id, name, input } => ContentBlockParam::ToolUse { id, name, input },
            other => ContentBlockParam::Raw(serde_json::to_value(&other).unwrap_or(Value::Null)),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DocumentSource {
    Text { media_type: String, data: String },
    Base64 { media_type: String, data: String },
    Content { content: Vec<ContentBlockParam> },
    Url { url: String },
    File { file_id: String },
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ImageSource {
    Base64 { media_type: String, data: String },
    Url { url: String },
    File { file_id: String },
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(untagged)]
pub enum ToolResultContent {
    Text(String),
    Blocks(Vec<ContentBlockParam>),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct CitationsConfig {
    pub enabled: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CacheControl {
    #[serde(rename = "type")]
    pub kind: String,
    /// `"5m"` (default) or `"1h"`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ttl: Option<String>,
}

impl CacheControl {
    pub fn ephemeral() -> CacheControl {
        CacheControl { kind: "ephemeral".into(), ttl: None }
    }

    pub fn one_hour() -> CacheControl {
        CacheControl { kind: "ephemeral".into(), ttl: Some("1h".into()) }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(untagged)]
pub enum System {
    Text(String),
    Blocks(Vec<SystemBlock>),
}

/// A system prompt block; serialised with `"type": "text"`.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type", rename = "text")]
pub struct SystemBlock {
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ThinkingConfig {
    /// The model decides when and how much to think; `effort` sets the depth.
    Adaptive {
        #[serde(skip_serializing_if = "Option::is_none")]
        display: Option<ThinkingDisplay>,
    },
    /// Rejected by several current models (see the model's documentation).
    Disabled,
    /// Older models only.
    Enabled { budget_tokens: u32 },
    /// Claude Sonnet 5.5's way to turn thinking off.
    BetweenTools,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ThinkingDisplay {
    Summarized,
    Omitted,
    Updates,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct OutputConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effort: Option<Effort>,
    /// Structured output format (cannot be combined with citations).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<Value>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Effort {
    Low,
    Medium,
    High,
    Xhigh,
    Max,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Tool {
    pub name: String,
    pub description: String,
    pub input_schema: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strict: Option<bool>,
    /// Loaded only when a tool search finds it (see `ToolSearchTool`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub defer_loading: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
}

impl Tool {
    pub fn new(name: impl Into<String>, description: impl Into<String>, input_schema: Value) -> Tool {
        Tool { name: name.into(), description: description.into(), input_schema, strict: None, defer_loading: None, cache_control: None }
    }

    /// Inputs always validate against the schema (which needs `additionalProperties: false`).
    pub fn strict(mut self) -> Self {
        self.strict = Some(true);
        self
    }

    pub fn deferred(mut self) -> Self {
        self.defer_loading = Some(true);
        self
    }

    pub fn with_cache_control(mut self, cc: CacheControl) -> Self {
        self.cache_control = Some(cc);
        self
    }
}

/// A tool definition: a custom tool, a server tool Anthropic runs, or anything else as raw JSON.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(untagged)]
pub enum ToolDefinition {
    Custom(Tool),
    WebSearch(WebSearchTool),
    WebFetch(WebFetchTool),
    CodeExecution(CodeExecutionTool),
    ToolSearch(ToolSearchTool),
    Raw(Value),
}

impl From<Tool> for ToolDefinition {
    fn from(t: Tool) -> ToolDefinition {
        ToolDefinition::Custom(t)
    }
}

impl From<WebSearchTool> for ToolDefinition {
    fn from(t: WebSearchTool) -> ToolDefinition {
        ToolDefinition::WebSearch(t)
    }
}

impl From<WebFetchTool> for ToolDefinition {
    fn from(t: WebFetchTool) -> ToolDefinition {
        ToolDefinition::WebFetch(t)
    }
}

impl From<CodeExecutionTool> for ToolDefinition {
    fn from(t: CodeExecutionTool) -> ToolDefinition {
        ToolDefinition::CodeExecution(t)
    }
}

impl From<ToolSearchTool> for ToolDefinition {
    fn from(t: ToolSearchTool) -> ToolDefinition {
        ToolDefinition::ToolSearch(t)
    }
}

impl From<Value> for ToolDefinition {
    fn from(v: Value) -> ToolDefinition {
        ToolDefinition::Raw(v)
    }
}

/// Server-side web search. Results come back as `ContentBlock::WebSearchToolResult` blocks
/// and cited text carries `Citation::WebSearchResultLocation`. A failed search is an error
/// object inside the result block, not an API error.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct WebSearchTool {
    /// The tool version, e.g. `web_search_20260209`.
    #[serde(rename = "type")]
    pub kind: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_uses: Option<u32>,
    /// Use `allowed_domains` or `blocked_domains`, not both.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_domains: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blocked_domains: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_location: Option<UserLocation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
}

impl WebSearchTool {
    /// The version with dynamic filtering (Claude Opus 4.6, Claude Sonnet 4.6 and later).
    /// Do not also declare `CodeExecutionTool`: it runs code on its own.
    pub fn new() -> WebSearchTool {
        WebSearchTool::version("web_search_20260209")
    }

    /// The basic version, for older models and Vertex AI.
    pub fn basic() -> WebSearchTool {
        WebSearchTool::version("web_search_20250305")
    }

    pub fn version(kind: impl Into<String>) -> WebSearchTool {
        WebSearchTool {
            kind: kind.into(),
            name: "web_search".into(),
            max_uses: None,
            allowed_domains: None,
            blocked_domains: None,
            user_location: None,
            cache_control: None,
        }
    }

    pub fn max_uses(mut self, n: u32) -> Self {
        self.max_uses = Some(n);
        self
    }

    pub fn allowed_domains(mut self, domains: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.allowed_domains = Some(domains.into_iter().map(Into::into).collect());
        self
    }

    pub fn blocked_domains(mut self, domains: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.blocked_domains = Some(domains.into_iter().map(Into::into).collect());
        self
    }

    pub fn user_location(mut self, location: UserLocation) -> Self {
        self.user_location = Some(location);
        self
    }
}

impl Default for WebSearchTool {
    fn default() -> Self {
        WebSearchTool::new()
    }
}

/// Where the user roughly is, to localise search results; serialised with `"type": "approximate"`.
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
#[serde(tag = "type", rename = "approximate")]
pub struct UserLocation {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub city: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub region: Option<String>,
    /// ISO 3166-1 alpha-2, e.g. `RO`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub country: Option<String>,
    /// IANA time zone, e.g. `Europe/Bucharest`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timezone: Option<String>,
}

/// Server-side fetch of a URL already present in the conversation. The page comes back as
/// a `ContentBlock::WebFetchToolResult` block.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct WebFetchTool {
    /// The tool version, e.g. `web_fetch_20260209`.
    #[serde(rename = "type")]
    pub kind: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_uses: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_domains: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blocked_domains: Option<Vec<String>>,
    /// Lets the answer cite the fetched pages.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub citations: Option<CitationsConfig>,
    /// Truncates long pages to about this many tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_content_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
}

impl WebFetchTool {
    /// The version with dynamic filtering (Claude Opus 4.6, Claude Sonnet 4.6 and later).
    pub fn new() -> WebFetchTool {
        WebFetchTool::version("web_fetch_20260209")
    }

    /// The basic version, for older models.
    pub fn basic() -> WebFetchTool {
        WebFetchTool::version("web_fetch_20250910")
    }

    pub fn version(kind: impl Into<String>) -> WebFetchTool {
        WebFetchTool {
            kind: kind.into(),
            name: "web_fetch".into(),
            max_uses: None,
            allowed_domains: None,
            blocked_domains: None,
            citations: None,
            max_content_tokens: None,
            cache_control: None,
        }
    }

    pub fn max_uses(mut self, n: u32) -> Self {
        self.max_uses = Some(n);
        self
    }

    pub fn allowed_domains(mut self, domains: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.allowed_domains = Some(domains.into_iter().map(Into::into).collect());
        self
    }

    pub fn blocked_domains(mut self, domains: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.blocked_domains = Some(domains.into_iter().map(Into::into).collect());
        self
    }

    pub fn with_citations(mut self) -> Self {
        self.citations = Some(CitationsConfig { enabled: true });
        self
    }

    pub fn max_content_tokens(mut self, n: u32) -> Self {
        self.max_content_tokens = Some(n);
        self
    }
}

impl Default for WebFetchTool {
    fn default() -> Self {
        WebFetchTool::new()
    }
}

/// Server-side code execution in a sandbox. Output comes back as
/// `ContentBlock::BashCodeExecutionToolResult` and `TextEditorCodeExecutionToolResult` blocks;
/// files it writes can be downloaded with `Client::download_file`.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct CodeExecutionTool {
    /// The tool version, e.g. `code_execution_20260521`.
    #[serde(rename = "type")]
    pub kind: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
}

impl CodeExecutionTool {
    pub fn new() -> CodeExecutionTool {
        CodeExecutionTool::version("code_execution_20260521")
    }

    /// Another version, e.g. `code_execution_20260120` for programmatic tool calling.
    pub fn version(kind: impl Into<String>) -> CodeExecutionTool {
        CodeExecutionTool { kind: kind.into(), name: "code_execution".into(), cache_control: None }
    }
}

impl Default for CodeExecutionTool {
    fn default() -> Self {
        CodeExecutionTool::new()
    }
}

/// Server-side search over the request's deferred tools (`Tool::deferred`): only the tools
/// it finds are loaded. Keep at least one tool, this one included, not deferred.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ToolSearchTool {
    /// `tool_search_tool_regex_20251119` or `tool_search_tool_bm25_20251119`.
    #[serde(rename = "type")]
    pub kind: String,
    pub name: String,
}

impl ToolSearchTool {
    /// The model searches with regular expressions.
    pub fn regex() -> ToolSearchTool {
        ToolSearchTool { kind: "tool_search_tool_regex_20251119".into(), name: "tool_search_tool_regex".into() }
    }

    /// The model searches with natural-language queries.
    pub fn bm25() -> ToolSearchTool {
        ToolSearchTool { kind: "tool_search_tool_bm25_20251119".into(), name: "tool_search_tool_bm25".into() }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ToolChoice {
    Auto {
        #[serde(skip_serializing_if = "Option::is_none")]
        disable_parallel_tool_use: Option<bool>,
    },
    None,
    /// Rejected by several current models; prefer `Auto` and say which tool to use.
    Any,
    /// Rejected by several current models; prefer `Auto` and say which tool to use.
    Tool {
        name: String,
    },
}

/// Which model the API retries a refused request on.
#[derive(Debug, Clone, PartialEq)]
pub enum Fallbacks {
    /// `"default"`: the API picks the fallback by refusal category.
    Default,
    /// Named models, tried in order.
    Models(Vec<FallbackModel>),
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct FallbackModel {
    pub model: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,
}

impl Serialize for Fallbacks {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        match self {
            Fallbacks::Default => s.serialize_str("default"),
            Fallbacks::Models(models) => models.serialize(s),
        }
    }
}