claude-sdk 2.1.0

Native Rust SDK for the Claude API with streaming support and tool execution
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
//! Message Batches API for async bulk processing.
//!
//! Process large volumes of Messages requests asynchronously with 50% cost reduction.
//! Ideal for bulk content generation, data processing, and offline workloads.
//!
//! # Key Benefits
//!
//! | Feature | Description |
//! |---------|-------------|
//! | **50% Cost Savings** | All batch requests are billed at half price |
//! | **High Volume** | Up to 100,000 requests per batch |
//! | **Large Batches** | Up to 256 MB total request size |
//! | **Long Results** | Results available for 29 days |
//! | **Concurrent Processing** | Requests processed in parallel |
//!
//! # Typical Workflow
//!
//! 1. Create a batch with [`BatchClient::create`]
//! 2. Monitor progress with [`BatchClient::retrieve`] or [`BatchClient::wait_for_completion`]
//! 3. Fetch results with [`BatchClient::results`]
//!
//! # Quick Example
//!
//! ```rust,no_run
//! use claude_sdk::batch::{BatchClient, BatchRequest};
//! use claude_sdk::{MessagesRequest, Message};
//! use futures::StreamExt;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = BatchClient::new("your-api-key");
//!
//! // Create batch with multiple requests
//! let requests = vec![
//!     BatchRequest {
//!         custom_id: "summarize-doc-1".into(),
//!         params: MessagesRequest::new(
//!             "claude-sonnet-4-5-20250929",
//!             1024,
//!             vec![Message::user("Summarize: [document 1 text]")],
//!         ),
//!     },
//!     BatchRequest {
//!         custom_id: "summarize-doc-2".into(),
//!         params: MessagesRequest::new(
//!             "claude-sonnet-4-5-20250929",
//!             1024,
//!             vec![Message::user("Summarize: [document 2 text]")],
//!         ),
//!     },
//! ];
//!
//! // Submit batch
//! let batch = client.create(requests).await?;
//! println!("Batch {} created", batch.id);
//!
//! // Wait for completion (polls every 60s)
//! let completed = client.wait_for_completion(&batch.id).await?;
//! println!("Done! {} succeeded, {} failed",
//!     completed.request_counts.succeeded,
//!     completed.request_counts.errored);
//!
//! // Stream results
//! let mut results = client.results(&batch.id).await?;
//! while let Some(result) = results.next().await {
//!     let result = result?;
//!     println!("Result for {}: {:?}", result.custom_id, result.result);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Processing Large Datasets
//!
//! For large datasets, create batches in chunks and process results as they complete:
//!
//! ```rust,no_run
//! use claude_sdk::batch::{BatchClient, BatchRequest, BatchResultType};
//! use claude_sdk::{MessagesRequest, Message};
//! use futures::StreamExt;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = BatchClient::new("your-api-key");
//!
//! // Example: Process 1000 items in batches of 100
//! let items: Vec<String> = (0..1000).map(|i| format!("Item {}", i)).collect();
//!
//! for (batch_num, chunk) in items.chunks(100).enumerate() {
//!     let requests: Vec<BatchRequest> = chunk
//!         .iter()
//!         .enumerate()
//!         .map(|(i, item)| BatchRequest {
//!             custom_id: format!("batch-{}-item-{}", batch_num, i),
//!             params: MessagesRequest::new(
//!                 "claude-sonnet-4-5-20250929",
//!                 256,
//!                 vec![Message::user(format!("Process: {}", item))],
//!             ),
//!         })
//!         .collect();
//!
//!     let batch = client.create(requests).await?;
//!     println!("Batch {} submitted: {}", batch_num, batch.id);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Handling Results
//!
//! Each result can be one of several types:
//!
//! ```rust,no_run
//! use claude_sdk::batch::{BatchClient, BatchResultType};
//! use futures::StreamExt;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = BatchClient::new("your-api-key");
//! let mut results = client.results("msgbatch_123").await?;
//!
//! while let Some(result) = results.next().await {
//!     let result = result?;
//!     match result.result {
//!         BatchResultType::Succeeded { message } => {
//!             println!("{}: Success - {} tokens used",
//!                 result.custom_id,
//!                 message.usage.output_tokens);
//!         }
//!         BatchResultType::Errored { error } => {
//!             println!("{}: Error - {}", result.custom_id, error.message);
//!         }
//!         BatchResultType::Canceled => {
//!             println!("{}: Canceled", result.custom_id);
//!         }
//!         BatchResultType::Expired => {
//!             println!("{}: Expired", result.custom_id);
//!         }
//!     }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Batch Lifecycle
//!
//! ```text
//! ┌──────────┐     ┌─────────────┐     ┌───────┐
//! │  create  │────▶│ in_progress │────▶│ ended │
//! └──────────┘     └─────────────┘     └───────┘
//!//!//!                  ┌───────────┐
//!                  │ canceling │────▶ ended
//!                  └───────────┘
//! ```
//!
//! # Cancellation
//!
//! Cancel a batch to stop processing remaining requests:
//!
//! ```rust,no_run
//! use claude_sdk::batch::BatchClient;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = BatchClient::new("your-api-key");
//!
//! // Cancel a running batch
//! let batch = client.cancel("msgbatch_123").await?;
//! println!("Batch canceled. Completed: {}, Canceled: {}",
//!     batch.request_counts.succeeded,
//!     batch.request_counts.canceled);
//! # Ok(())
//! # }
//! ```

use crate::error::{Error, Result};
use crate::types::{MessagesRequest, MessagesResponse};
use futures::Stream;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::pin::Pin;
use std::time::Duration;
use tracing::{debug, info};

/// Batch API endpoint
const BATCH_API_URL: &str = "https://api.anthropic.com/v1/messages/batches";

/// API version
const API_VERSION: &str = "2023-06-01";

/// A single request in a batch
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchRequest {
    /// Unique identifier for this request (for result matching)
    pub custom_id: String,

    /// The Messages API request parameters
    pub params: MessagesRequest,
}

/// Message Batch metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageBatch {
    pub id: String,

    #[serde(rename = "type")]
    pub batch_type: String,

    pub processing_status: BatchProcessingStatus,

    pub request_counts: RequestCounts,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub ended_at: Option<String>,

    pub created_at: String,

    pub expires_at: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub cancel_initiated_at: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub results_url: Option<String>,
}

/// Batch processing status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BatchProcessingStatus {
    InProgress,
    Canceling,
    Ended,
}

/// Request counts by status
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct RequestCounts {
    pub processing: u32,
    pub succeeded: u32,
    pub errored: u32,
    pub canceled: u32,
    pub expired: u32,
}

/// Individual batch result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchResult {
    pub custom_id: String,
    pub result: BatchResultType,
}

/// Type of batch result
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
#[allow(clippy::large_enum_variant)]
pub enum BatchResultType {
    Succeeded { message: MessagesResponse },
    Errored { error: BatchError },
    Canceled,
    Expired,
}

/// Error in a batch request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchError {
    #[serde(rename = "type")]
    pub error_type: String,
    pub message: String,
}

/// Client for Message Batches API
///
/// # Example
///
/// ```rust,no_run
/// use claude_sdk::batch::BatchClient;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = BatchClient::new("your-api-key");
///
/// // List all batches
/// let batches = client.list(None).await?;
/// println!("Found {} batches", batches.len());
/// # Ok(())
/// # }
/// ```
pub struct BatchClient {
    http: Client,
    api_key: String,
    api_version: String,
}

impl BatchClient {
    /// Create a new batch client
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            http: Client::new(),
            api_key: api_key.into(),
            api_version: API_VERSION.to_string(),
        }
    }

    /// Create a new message batch
    ///
    /// # Limits
    /// - Maximum 100,000 requests per batch
    /// - Maximum 256 MB total size
    /// - Results available for 29 days
    pub async fn create(&self, requests: Vec<BatchRequest>) -> Result<MessageBatch> {
        debug!("Creating batch with {} requests", requests.len());

        #[derive(Serialize)]
        struct CreateRequest {
            requests: Vec<BatchRequest>,
        }

        let response = self
            .http
            .post(BATCH_API_URL)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", &self.api_version)
            .header("content-type", "application/json")
            .json(&CreateRequest { requests })
            .send()
            .await?;

        let status = response.status();
        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(Error::Api {
                status: status.as_u16(),
                message: error_text,
                error_type: None,
            });
        }

        let batch: MessageBatch = response.json().await?;
        Ok(batch)
    }

    /// Retrieve a message batch by ID
    pub async fn retrieve(&self, batch_id: &str) -> Result<MessageBatch> {
        debug!("Retrieving batch: {}", batch_id);

        let url = format!("{}/{}", BATCH_API_URL, batch_id);

        let response = self
            .http
            .get(&url)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", &self.api_version)
            .send()
            .await?;

        let status = response.status();
        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(Error::Api {
                status: status.as_u16(),
                message: error_text,
                error_type: None,
            });
        }

        let batch: MessageBatch = response.json().await?;
        Ok(batch)
    }

    /// List message batches
    ///
    /// # Arguments
    /// * `limit` - Maximum number of batches to return (default: 20)
    pub async fn list(&self, limit: Option<u32>) -> Result<Vec<MessageBatch>> {
        debug!("Listing batches");

        let mut url = BATCH_API_URL.to_string();
        if let Some(lim) = limit {
            url.push_str(&format!("?limit={}", lim));
        }

        let response = self
            .http
            .get(&url)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", &self.api_version)
            .send()
            .await?;

        let status = response.status();
        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(Error::Api {
                status: status.as_u16(),
                message: error_text,
                error_type: None,
            });
        }

        #[derive(Deserialize)]
        struct ListResponse {
            data: Vec<MessageBatch>,
        }

        let list_response: ListResponse = response.json().await?;
        Ok(list_response.data)
    }

    /// Cancel a message batch
    pub async fn cancel(&self, batch_id: &str) -> Result<MessageBatch> {
        info!("Canceling batch: {}", batch_id);

        let url = format!("{}/{}/cancel", BATCH_API_URL, batch_id);

        let response = self
            .http
            .post(&url)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", &self.api_version)
            .send()
            .await?;

        let status = response.status();
        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(Error::Api {
                status: status.as_u16(),
                message: error_text,
                error_type: None,
            });
        }

        let batch: MessageBatch = response.json().await?;
        Ok(batch)
    }

    /// Wait for batch to complete processing
    ///
    /// Polls the batch status every 60 seconds until it reaches `ended` status.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use claude_sdk::batch::BatchClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = BatchClient::new("your-api-key");
    ///
    /// let batch = client.wait_for_completion("msgbatch_123").await?;
    /// println!("Batch complete! {} succeeded, {} failed",
    ///          batch.request_counts.succeeded,
    ///          batch.request_counts.errored);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn wait_for_completion(&self, batch_id: &str) -> Result<MessageBatch> {
        info!("Waiting for batch {} to complete", batch_id);

        loop {
            let batch = self.retrieve(batch_id).await?;

            if batch.processing_status == BatchProcessingStatus::Ended {
                return Ok(batch);
            }

            debug!(
                "Batch {} still processing (status: {:?})",
                batch_id, batch.processing_status
            );

            tokio::time::sleep(Duration::from_secs(60)).await;
        }
    }

    /// Stream batch results
    ///
    /// Results are returned as a stream of BatchResult items parsed from JSONL.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use claude_sdk::batch::BatchClient;
    /// use futures::StreamExt;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = BatchClient::new("your-api-key");
    ///
    /// let mut stream = client.results("msgbatch_123").await?;
    ///
    /// while let Some(result) = stream.next().await {
    ///     match result? {
    ///         result => println!("Result for {}: {:?}",
    ///                           result.custom_id, result.result),
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn results(
        &self,
        batch_id: &str,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<BatchResult>> + Send>>> {
        // First, get the batch to find the results_url
        let batch = self.retrieve(batch_id).await?;

        let results_url = batch
            .results_url
            .ok_or_else(|| Error::InvalidRequest("Batch has no results yet".into()))?;

        debug!("Streaming results from: {}", results_url);

        // Stream the JSONL results
        let response = self
            .http
            .get(&results_url)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", &self.api_version)
            .send()
            .await?;

        let status = response.status();
        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(Error::Api {
                status: status.as_u16(),
                message: error_text,
                error_type: None,
            });
        }

        // Convert bytes stream to lines stream
        let byte_stream = response.bytes_stream();

        let stream = async_stream::stream! {
            use futures::StreamExt;

            let mut byte_stream = byte_stream;
            let mut buffer = Vec::new();

            while let Some(chunk_result) = byte_stream.next().await {
                let chunk = chunk_result.map_err(|e| Error::Network(format!("Stream error: {}", e)))?;
                buffer.extend_from_slice(&chunk);

                // Process complete lines
                while let Some(newline_pos) = buffer.iter().position(|&b| b == b'\n') {
                    let line_bytes: Vec<u8> = buffer.drain(..=newline_pos).collect();
                    let line = String::from_utf8_lossy(&line_bytes).trim().to_string();

                    if !line.is_empty() {
                        let result: BatchResult = serde_json::from_str(&line)
                            .map_err(Error::Json)?;
                        yield Ok(result);
                    }
                }
            }

            // Process any remaining data
            if !buffer.is_empty() {
                let line = String::from_utf8_lossy(&buffer).trim().to_string();
                if !line.is_empty() {
                    let result: BatchResult = serde_json::from_str(&line)
                        .map_err(Error::Json)?;
                    yield Ok(result);
                }
            }
        };

        Ok(Box::pin(stream))
    }
}

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

    #[test]
    fn test_batch_client_creation() {
        let client = BatchClient::new("test-key");
        assert_eq!(client.api_key, "test-key");
    }

    #[test]
    fn test_batch_processing_status() {
        assert_eq!(
            serde_json::to_string(&BatchProcessingStatus::InProgress).unwrap(),
            r#""in_progress""#
        );
        assert_eq!(
            serde_json::to_string(&BatchProcessingStatus::Ended).unwrap(),
            r#""ended""#
        );
    }

    // Integration tests require API key
    #[tokio::test]
    #[ignore]
    async fn test_create_and_retrieve_batch() {
        let api_key = std::env::var("ANTHROPIC_API_KEY").expect("ANTHROPIC_API_KEY required");
        let client = BatchClient::new(api_key);

        let requests = vec![BatchRequest {
            custom_id: "test-1".into(),
            params: MessagesRequest::new(
                "claude-sonnet-4-5-20250929",
                100,
                vec![crate::types::Message::user("Hello!")],
            ),
        }];

        let batch = client.create(requests).await;

        match batch {
            Ok(b) => {
                println!("Created batch: {}", b.id);
                assert_eq!(b.processing_status, BatchProcessingStatus::InProgress);
            }
            Err(e) => println!("Test skipped (expected without real API): {}", e),
        }
    }
}