Skip to main content

claude_api/batches/
mod.rs

1//! The Batches API: submit a batch of message requests, poll for
2//! completion, stream per-request results.
3//!
4//! Anthropic's batch endpoint is the cheapest way to run large fan-out
5//! workloads (50% off vs. per-request pricing) at the cost of higher
6//! latency. This module wraps the full surface:
7//!
8//! - [`Batches::create`] -- submit
9//! - [`Batches::get`] -- status (polling-friendly)
10//! - [`Batches::list`] / [`Batches::list_all`] -- enumerate
11//! - [`Batches::cancel`], [`Batches::delete`]
12//! - [`Batches::wait_for`] -- poller that returns once `ended_at` is set
13//! - [`Batches::results`] / [`Batches::results_stream`] -- decode the JSONL
14//!   results body, eagerly into a `Vec` or lazily as a `Stream`
15//!
16//! The batch ID is the only state you need to durably persist; reattach
17//! later by calling [`Batches::get(id)`](Batches::get) or
18//! [`Batches::wait_for(id, _)`](Batches::wait_for).
19//!
20//! # Quick start
21//!
22//! ```no_run
23//! use claude_api::{Client, batches::{BatchRequest, BatchResultPayload, WaitOptions},
24//!     messages::CreateMessageRequest, types::ModelId};
25//! # async fn run() -> Result<(), claude_api::Error> {
26//! let client = Client::new(std::env::var("ANTHROPIC_API_KEY").unwrap());
27//! let requests = vec![
28//!     BatchRequest::new("q1",
29//!         CreateMessageRequest::builder()
30//!             .model(ModelId::HAIKU_4_5).max_tokens(32).user("2 + 2?").build()?),
31//! ];
32//! let batch = client.batches().create(requests).await?;
33//! let finished = client.batches()
34//!     .wait_for(&batch.id, WaitOptions::default()).await?;
35//! let items = client.batches().results(&finished.id).await?;
36//! for item in &items {
37//!     if let BatchResultPayload::Succeeded { message } = &item.result {
38//!         println!("{}: {} tokens", item.custom_id, message.usage.output_tokens);
39//!     }
40//! }
41//! # Ok(())
42//! # }
43//! ```
44
45pub mod types;
46
47#[cfg(feature = "async")]
48#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
49pub mod api;
50
51pub use types::{
52    BatchDeleted, BatchRequest, BatchResultItem, BatchResultPayload, ListBatchesParams,
53    MessageBatch, ProcessingStatus, RequestCounts, WaitOptions,
54};
55
56#[cfg(feature = "async")]
57pub use api::Batches;