package anthropic
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Module Anthropic.BatchesSource
Batches API for asynchronous bulk message processing.
This module enables processing multiple message requests asynchronously, reducing costs and improving throughput for large-scale operations. Batches process in the background and results can be retrieved when complete.
status indicates the current state of batch processing.
Batches transition from `In_progress through optional `Canceling to `Ended.
type request_counts = {canceled : int;(*Requests canceled before processing.
*)errored : int;(*Requests that failed during processing.
*)expired : int;(*Requests that expired before processing.
*)processing : int;(*Requests currently being processed.
*)succeeded : int;(*Successfully completed requests.
*)
}request_counts tracks the status of individual requests in a batch.
The sum of all counts equals the total number of requests in the batch.
type t = {id : string;(*Unique batch identifier.
*)type_ : string;(*Batch type (always "messages_batch").
*)processing_status : status;(*Current processing state.
*)request_counts : request_counts;(*Request statistics.
*)created_at : string;(*ISO 8601 creation timestamp.
*)expires_at : string;(*ISO 8601 expiration timestamp.
*)ended_at : string option;(*ISO 8601 completion timestamp.
*)cancel_initiated_at : string option;(*ISO 8601 cancellation timestamp.
*)archived_at : string option;(*ISO 8601 archival timestamp.
*)results_url : string option;(*URL for downloading results.
*)
}t represents a batch job with its current state and statistics.
Batches have a 24-hour processing window before expiration. Results remain available for download after completion.
type request = {custom_id : string;(*User-defined identifier for tracking.
*)params : Yojson.Safe.t;(*Message API parameters as JSON.
*)
}request defines a single message request within a batch.
The params field should contain the same JSON structure as a regular Messages.create call, including model, messages, and other parameters.
type individual_response = {custom_id : string;(*The custom ID from the original request.
*)result : [ `Succeeded of Messages.response | `Errored of api_error ];(*Processing result.
*)
}individual_response contains the result of a single batch request.
Each response maps back to its original request via custom_id. Failed requests include structured error information.
submit client ~requests () submits a new batch for processing.
Example: Submits a batch of similar requests.
let make_request i =
{
custom_id = Printf.sprintf "req_%d" i;
params =
`Assoc
[
("model", `String "claude-3-5-haiku-20241022");
("max_tokens", `Int 100);
( "messages",
`List
[
`Assoc
[
("role", `String "user");
( "content",
`String (Printf.sprintf "Summarize article %d" i)
);
];
] );
];
}
in
let requests = List.init 100 make_request in
match Batches.submit client ~requests () with
| Ok batch -> Printf.printf "Submitted batch %s\n" batch.id
| Error e -> Printf.eprintf "Error: %s\n" (string_of_error e)get client ~batch_id () retrieves the current status of a batch.
Use this to poll for batch completion or check processing statistics.
Example: Polls until batch completes.
let rec wait_for_completion client batch_id =
match Batches.get client ~batch_id () with
| Ok batch when batch.processing_status = `Ended ->
Printf.printf "Batch completed: %d succeeded, %d failed\n"
batch.request_counts.succeeded batch.request_counts.errored
| Ok _ ->
Eio.Time.sleep env#clock 5.0;
wait_for_completion client batch_id
| Error e -> Printf.eprintf "Error: %s\n" (string_of_error e)list client ?limit ?after_id () retrieves a paginated list of batches.
Lists batches in reverse chronological order (newest first).
cancel client ~batch_id () cancels an in-progress batch.
Only batches with status `In_progress can be canceled. Already-processed requests are not affected.
val results :
client ->
batch_id:string ->
unit ->
(individual_response Eio.Stream.t, error) resultresults client ~batch_id () streams individual results from a completed batch.
Results are streamed as newline-delimited JSON. The batch must have status `Ended to retrieve results.
Example: Processes batch results.
match Batches.results client ~batch_id () with
| Ok stream ->
Eio.Stream.iter
(function
| { custom_id; result = `Succeeded response } ->
Printf.printf "Request %s succeeded\n" custom_id
| { custom_id; result = `Errored error } ->
Printf.printf "Request %s failed: %s\n" custom_id
error.message)
stream
| Error e -> Printf.eprintf "Error: %s\n" (string_of_error e)