Drop-in AsyncOpenAI replacement that transparently batches requests using the batch API
Project description
autobatcher
Drop-in replacement for AsyncOpenAI that transparently batches requests using
OpenAI's (or compatible) Batch
API.
Why?
Batch LLM APIs (like OpenAI's) offers 50% cost savings, but requires you to restructure your code around file uploads and polling. autobatcher lets you keep your existing async code while getting batch pricing automatically.
# Before: regular async calls (full price)
from openai import AsyncOpenAI
client = AsyncOpenAI()
# After: batched calls (50% off)
from autobatcher import BatchOpenAI
client = BatchOpenAI()
# Same interface, same code
response = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
How it works
- Requests are collected over a configurable time window (default: 1 second)
- When the window closes or batch size is reached, requests are submitted as a batch
- Results are polled and returned to waiting callers as they complete
- Your code sees normal
ChatCompletionresponses
Installation
pip install autobatcher
Usage
import asyncio
from autobatcher import BatchOpenAI
async def main():
client = BatchOpenAI(
api_key="sk-...", # or set OPENAI_API_KEY env var
batch_size=100, # submit batch when this many requests queued
batch_window_seconds=1.0, # or after this many seconds
poll_interval_seconds=5.0, # how often to check for results
)
# Use exactly like AsyncOpenAI
response = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What is 2+2?"}],
)
print(response.choices[0].message.content)
await client.close()
asyncio.run(main())
Parallel requests
The real power comes when you have many requests:
async def process_many(prompts: list[str]) -> list[str]:
client = BatchOpenAI(batch_size=50, batch_window_seconds=2.0)
async def get_response(prompt: str) -> str:
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content
# All requests are batched together automatically
results = await asyncio.gather(*[get_response(p) for p in prompts])
await client.close()
return results
Context manager
async with BatchOpenAI() as client:
response = await client.chat.completions.create(...)
Configuration
| Parameter | Default | Description |
|---|---|---|
api_key |
None |
OpenAI API key (falls back to OPENAI_API_KEY env var) |
base_url |
None |
API base URL (for proxies or compatible APIs) |
batch_size |
100 |
Submit batch when this many requests are queued |
batch_window_seconds |
1.0 |
Submit batch after this many seconds |
poll_interval_seconds |
5.0 |
How often to poll for batch completion |
completion_window |
"24h" |
Batch completion window ("24h" or "1h") |
Limitations
- Only
chat.completions.createis supported for now - Batch API has a 24-hour completion window by default
- No escalations when the completion window elapses
- Not suitable for real-time/interactive use cases
- This library is designed or use with the Doubleword batched API. Support for OpenAI's batch API or other compatible APIs is best effort. If you experience any issues, please open an issue.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autobatcher-0.1.1.tar.gz.
File metadata
- Download URL: autobatcher-0.1.1.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bae1bbdfe7ce9f229a91697263fc7fce3f157c2a767af945c2743f09ef5e3262
|
|
| MD5 |
6110ffa806a2f85e879bd6012e7a3ecf
|
|
| BLAKE2b-256 |
2cec2f03090cd5504496514fe696210368a06c574bf87fc3a5c3f7c4d052b1dd
|
Provenance
The following attestation bundles were made for autobatcher-0.1.1.tar.gz:
Publisher:
publish.yml on doublewordai/autobatcher
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autobatcher-0.1.1.tar.gz -
Subject digest:
bae1bbdfe7ce9f229a91697263fc7fce3f157c2a767af945c2743f09ef5e3262 - Sigstore transparency entry: 793256308
- Sigstore integration time:
-
Permalink:
doublewordai/autobatcher@46abebf5552ccab00a6478a78985dd23b59bde50 -
Branch / Tag:
refs/tags/autobatcher-v0.1.1 - Owner: https://github.com/doublewordai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@46abebf5552ccab00a6478a78985dd23b59bde50 -
Trigger Event:
release
-
Statement type:
File details
Details for the file autobatcher-0.1.1-py3-none-any.whl.
File metadata
- Download URL: autobatcher-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09037e87256d434412eeed6839fd8e4ca3f0185378614c29739e5650123034cf
|
|
| MD5 |
baeef6671f59fb7f36ffc33a2b89cb43
|
|
| BLAKE2b-256 |
adafea254ac19b35820d0f4a14647d01caf17bafa03eede43901b508a08b0c05
|
Provenance
The following attestation bundles were made for autobatcher-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on doublewordai/autobatcher
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autobatcher-0.1.1-py3-none-any.whl -
Subject digest:
09037e87256d434412eeed6839fd8e4ca3f0185378614c29739e5650123034cf - Sigstore transparency entry: 793256353
- Sigstore integration time:
-
Permalink:
doublewordai/autobatcher@46abebf5552ccab00a6478a78985dd23b59bde50 -
Branch / Tag:
refs/tags/autobatcher-v0.1.1 - Owner: https://github.com/doublewordai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@46abebf5552ccab00a6478a78985dd23b59bde50 -
Trigger Event:
release
-
Statement type: