For the complete documentation index, see llms.txt. This page is also available as Markdown.

Batch

Process multiple requests asynchronously at lower cost

The Batch API lets you submit multiple requests as a single job for asynchronous processing, typically at lower cost than real-time requests.

How It Works

  1. Prepare a JSONL file where each line is a batch request object.

  2. Call create_batch() with the file path and target endpoint.

  3. any-llm uploads the file to the provider and creates the batch job.

  4. Poll with retrieve_batch() to check status.

  5. When complete, download results from the provider.

Input File Format

The input file must be a JSONL file where each line follows this structure:

{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-4.1-mini", "messages": [{"role": "user", "content": "Hello"}]}}
{"custom_id": "request-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-4.1-mini", "messages": [{"role": "user", "content": "World"}]}}

any_llm.create_batch()

Create a batch job by uploading a local JSONL file.

def create_batch(
    provider: str | LLMProvider,
    input_file_path: str,
    endpoint: str,
    *,
    completion_window: str = "24h",
    metadata: dict[str, str] | None = None,
    api_key: str | None = None,
    api_base: str | None = None,
    client_args: dict[str, Any] | None = None,
    **kwargs: Any,
) -> Batch

any_llm.acreate_batch()

Async variant with the same parameters.

Parameters (create)

Parameter
Type
Default
Description

provider

str | LLMProvider

required

Provider name to use for the request (e.g., 'openai', 'mistral')

input_file_path

str

required

Path to a local file containing batch requests in JSONL format.

endpoint

str

required

The endpoint to be used for all requests (e.g., '/v1/chat/completions')

completion_window

str

"24h"

The time frame within which the batch should be processed (default: '24h')

metadata

dict[str, str] | None

None

Optional custom metadata for the batch

api_key

str | None

None

API key for the provider

api_base

str | None

None

Base URL for the provider API

client_args

dict[str, Any] | None

None

Additional provider-specific arguments for client instantiation

**kwargs

Any

required

Additional provider-specific arguments

Returns: A Batch object.

any_llm.retrieve_batch()

Retrieve the current status and details of a batch job.

any_llm.aretrieve_batch()

Async variant with the same parameters.

Parameters (retrieve)

Parameter
Type
Default
Description

provider

str | LLMProvider

required

Provider name to use for the request (e.g., 'openai', 'mistral')

batch_id

str

required

The ID of the batch to retrieve

api_key

str | None

None

API key for the provider

api_base

str | None

None

Base URL for the provider API

client_args

dict[str, Any] | None

None

Additional provider-specific arguments for client instantiation

**kwargs

Any

required

Additional provider-specific arguments

Returns: A Batch object.

any_llm.cancel_batch()

Cancel an in-progress batch job.

any_llm.acancel_batch()

Async variant with the same parameters.

Returns: The cancelled Batch object.

any_llm.list_batches()

List batch jobs for a provider.

any_llm.alist_batches()

Async variant with the same parameters.

Parameters (list)

Parameter
Type
Default
Description

provider

str | LLMProvider

required

Provider name to use for the request (e.g., 'openai', 'mistral')

after

str | None

None

A cursor for pagination. Returns batches after this batch ID.

limit

int | None

None

Maximum number of batches to return (default: 20)

api_key

str | None

None

API key for the provider

api_base

str | None

None

Base URL for the provider API

client_args

dict[str, Any] | None

None

Additional provider-specific arguments for client instantiation

**kwargs

Any

required

Additional provider-specific arguments

Returns: A Sequence of Batch objects.

Usage

Last updated