Skip to main content

BatchCall

A lightweight Python library for concurrent batch calls, with independent adaptive concurrency, round-based retries, JSONL persistence and resume support. No runtime dependencies. Python 3.9+.

Designed for LLM APIs, agents, workflows and other I/O-bound calls. Bring your own callable; BatchCall handles scheduling, not business logic.

中文文档

Installation

From PyPI (once the first release is published):

python -m pip install batchcall

Or from a local checkout:

python -m pip install .

Quick start

import json
from batchcall import run, StorageConfig

class MyCaller:
    concurrency = 10
    adaptive = True
    max_concurrency = 50

    def __call__(self, input_data: str) -> dict:
        # Replace this example with your thread-safe API call.
        return {"status": "success", "output": input_data}

caller = MyCaller()
data = {str(i): json.dumps({"query": f"Question {i}"}) for i in range(100)}
summary = run(data, caller, storage_config=StorageConfig(output_path="experiment.jsonl"))
print(summary)

Multiple callers

Each object represents an independent rate-limit unit. Callers must be interchangeable for the same workload. Store credentials in your own caller; do not commit secrets.

caller1 = MyCaller()
caller1.concurrency = 10
caller1.max_concurrency = 30
caller2 = MyCaller()
caller2.concurrency = 20
caller2.max_concurrency = 100

from batchcall import LogConfig, RetryConfig

run(data, [caller1, caller2],
    storage_config=StorageConfig(output_path="experiment.jsonl", save_every_n=50),
    retry_config=RetryConfig(max_retries=3),
    log_config=LogConfig(log_path="runtime.log"))

Caller protocol

A callable accepts one JSON string and returns a dict. The library does not parse or validate the business JSON inside the string.

Field Required Meaning
status Yes success, rate_limited (HTTP 429), or any other failure string
output Yes JSON string; may be None on failure
duration_s No Finite, nonnegative seconds; measured by BatchCall if omitted
error No Human-readable error or None

Regular exceptions and invalid return contracts become exception failures. All failures are retried. The caller must manage API timeouts, credentials, thread safety and classify HTTP 429 as rate_limited. Hidden SDK retries are not separately observed by BatchCall.

Optional caller attributes (read at the start of each run):

Attribute Default Meaning
name caller_0, etc. Unique name for logs and records
concurrency 10 Fixed or initial concurrency
adaptive False Enable adaptive concurrency
max_concurrency 50 Upper bound in adaptive mode
adjust_every_n 100 Completed attempts per adjustment window

Each caller independently adjusts its concurrency after a full window of completed attempts (including errors and retries). At most 1% rate-limited: increase by 1; over 5%: halve, rounding down; otherwise hold. The minimum is 1. Running calls are not cancelled. Configuration attributes are never modified. RPM is measured over the last 60 seconds of actual callable starts, not actively limited. One callable invocation counts as one attempt regardless of internal HTTP requests.

Configuration

run(data, callers, *, storage_config=None, retry_config=None, log_config=None) accepts one caller or a list/tuple. All configuration objects are optional.

Class Parameters (defaults)
StorageConfig output_path="results.jsonl", save_every_n=50, resume=True
RetryConfig max_retries=3 (additional rounds, after initial round 0)
LogConfig log_every_n=100, console=True, log_path=None

Logs include total and per-caller RPM, target concurrency, in-flight requests, progress, success/failure counts, 429s, average elapsed and persisted records. Event logs also cover round boundaries, adjustments and tail repair. Console logs go to stderr. No raw input/output or credentials are logged by the framework.

Persistence and resume

Each completed attempt is appended to JSONL, including failures and retries. A record has this shape:

{"id":"case_001","input":"{\"query\":\"hello\"}","output":"{\"answer\":\"hi\"}","status":"success","elapsed":0.123,"error_msg":null,"caller_name":"caller_0","round":0}

output is the caller's business response string (or null on failure), status is the attempt status, and elapsed is its duration in seconds. All three are top-level fields. The caller protocol is unchanged.

Format change: Older JSONL records with nested output.response, output.status and output.elapsed are not supported. Use a new output file or migrate the old records before reading or resuming. This applies even with resume=False.

The main thread flushes every save_every_n completions and at round boundaries and exit. The initial round runs all pending IDs; later rounds run only failures from the previous round. Each run gets its own retry budget. Caller concurrency state persists across rounds, not across runs.

On startup, BatchCall scans the history line by line, keeping only successful IDs. Any ID that succeeded at least once is skipped when resume=True. A valid last record without a newline is preserved and given a newline; an invalid, unterminated last line is truncated. Other invalid lines or structurally invalid records raise an error. resume=False still appends and checks the file.

Use a new file or new IDs whenever input, model, or experiment settings change. Do not write to the same result file from multiple processes/runs. Flush is not fsync: a crash can lose unflushed or OS-buffered records. Calls may be repeated after a crash; this is not exactly-once execution. Side-effecting calls should be idempotent. Ordinary interruption flushes collected results where possible; running threads cannot be forcibly stopped, so set caller timeouts. Results can contain sensitive data: choose an appropriate storage location.

from batchcall import load_success_ids, iter_results

success_ids = load_success_ids("experiment.jsonl")
for record in iter_results("experiment.jsonl"):
    if record["status"] == "success":
        pass  # process a single successful record

The readers never modify files; iter_results yields every attempt, not deduplicated final results. load_success_ids returns an empty set for a missing file. Both reject corrupt records. Memory is bounded by input data, task IDs, active futures, the recent RPM window and the write buffer, not by the full historical result payloads.

run returns a dict with total, skipped, executed, success, failed, attempts, rate_limited, elapsed and output_path. Success/failure counts refer only to the current run's pending IDs.

Development

python -m pip install -e .
python -m unittest discover -s tests -v

MIT license.

Metadata

Release files for batchcall 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for batchcall 0.1.0
File Size Uploaded Provenance Actions
batchcall-0.1.0.tar.gz 15.8 kB With provenance Details for batchcall-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for batchcall 0.1.0
File Interpreter ABI Platform Actions
batchcall-0.1.0-py3-none-any.whl Python 3 none any Details for batchcall-0.1.0-py3-none-any.whl

Total release size: 27.6 kB

Release files / batchcall-0.1.0.tar.gz

Download URL batchcall-0.1.0.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8a4d548a3ca1c43de8487b81ca77a535e96900566896525acb336b84c6515fa1
BLAKE2b-256 checksum
How to use checksums
837dc21ec8fb8076a409d264fb75ae4b1fbe44fbc9df32ff6b9e80683007f870
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / batchcall-0.1.0-py3-none-any.whl

Download URL batchcall-0.1.0-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a87a4ed3a4172a67b75ec098bee4b92bf5714ccc8fd6d373d1a0f3c44694d9c
BLAKE2b-256 checksum
How to use checksums
ba43c1689e7b1a1fe60e54cf460266b43d914dbc10a4f179647e8c6c77f6b434
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for batchcall 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for batchcall-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page