# Batchata
<img alt="Batchata AI Batch Build Status" src="https://github.com/agamm/batchata/workflows/Tests/badge.svg" /><a href="https://pypi.org/project/batchata/"><img alt="Batchata AI Batch PyPI version" src="https://badge.fury.io/py/batchata.svg" /></a>
Unified Python API for AI Batch requests with cost tracking, Pydantic responses, citation mapping and parallel execution.
<img width="1328" height="598" alt="image" src="https://github.com/user-attachments/assets/b1b43070-f664-41a2-a85d-e2c589df556c" />
*This library is currently in beta - so there will be breaking changes*
## Why AI-batching?
AI providers offer batch APIs that process requests asynchronously at 50% reduced cost compared to real-time APIs. This is ideal for offline or batch processing tasks. However, managing batch jobs across providers, tracking costs, handling failures, and mapping citations back to source documents quickly becomes complex - that's where Batchata comes in.
## Batchata Features
- Native batch processing (50% cost savings via provider APIs)
- Set `max_cost_usd` limits for batch requests
- Time limit control with `.add_time_limit(seconds=, minutes=, hours=)`
- State persistence in case of network interruption
- Structured output `.json` format with Pydantic models
- Citation support and field mapping (Anthropic only)
- Multiple provider support (Anthropic, OpenAI, Google Gemini)
## Installation
### pip
```bash
pip install batchata
```
### uv
```bash
uv add batchata
```
## Quick Start
```python
from batchata import Batch
# Simple batch processing
batch = Batch(results_dir="./output")
.set_default_params(model="claude-sonnet-4-20250514") # or "gpt-4.1-2025-04-14" or "gemini-2.5-flash"
.add_cost_limit(usd=5.0)
for file in files:
batch.add_job(file=file, prompt="Summarize")
run = batch.run()
results = run.results() # {"completed": [JobResult], "failed": [JobResult], "cancelled": [JobResult]}
```
## Complete Example
```python
from batchata import Batch
from pydantic import BaseModel
from dotenv import load_dotenv
load_dotenv() # Load API keys from .env
# Define structured output
class InvoiceAnalysis(BaseModel):
invoice_number: str
total_amount: float
vendor: str
payment_status: str
# Create batch configuration
batch = Batch(
results_dir="./invoice_results",
max_parallel_batches=1,
items_per_batch=3
)
.set_state(file="./invoice_state.json", reuse_state=False)
.set_default_params(model="claude-sonnet-4-20250514", temperature=0.0)
.add_cost_limit(usd=5.0)
.add_time_limit(minutes=10) # Time limit of 10 minutes
.set_verbosity("warn")
# Add jobs with structured output and citations
invoice_files = ["path/to/invoice1.pdf", "path/to/invoice2.pdf", "path/to/invoice3.pdf"]
for invoice_file in invoice_files:
batch.add_job(
file=invoice_file,
prompt="Extract the invoice number, total amount, vendor name, and payment status.",
response_model=InvoiceAnalysis,
enable_citations=True
)
# Execute with rich progress display
print("Starting batch processing...")
run = batch.run(print_status=True)
# Or use custom progress callback
run = batch.run(print_status=True)
# Get results
results = run.results()
# Process successful results
for result in results["completed"]:
analysis = result.parsed_response
citations = result.citation_mappings
print(f"\nInvoice: {analysis.invoice_number} (page: {citations.get("invoice_number").page})")
print(f" Vendor: {analysis.vendor} (page: {citations.get("vendor").page})")
print(f" Total: ${analysis.total_amount:.2f} (page: {citations.get("total_amount").page})")
print(f" Status: {analysis.payment_status} (page: {citations.get("payment_status").page})")
# Process failed/cancelled results
for result in results["failed"]:
print(f"\nJob {result.job_id} failed: {result.error}")
for result in results["cancelled"]:
print(f"\nJob {result.job_id} was cancelled: {result.error}")
```
## Interactive Progress Display
Batchata provides an interactive real-time progress display when using `print_status=True`:
```python
run = batch.run(print_status=True)
```
<img width="2230" height="222" alt="image" src="https://github.com/user-attachments/assets/caf549a6-92a1-4ee0-8ac7-eda2d0f280a7" />
The interactive display shows:
- **Job Progress**: Completed/total jobs with progress bar
- **Batch Status**: Provider batch completion status
- **Real-time Cost**: Current spend vs limit (if set)
- **Elapsed Time**: Time since batch started
- **Live Updates**: Refreshes automatically as jobs complete
## File Structure
```
./results/
├── job-abc123.json
├── job-def456.json
├── job-ghi789.json
└── raw_files/
└── responses/
├── job-abc123_raw.json
├── job-def456_raw.json
└── job-ghi789_raw.json
./batch_state.json # Batch state
```
## Supported Providers
| Feature | Anthropic | OpenAI | Google Gemini |
|---------|-----------|--------|---------------|
| Models | [All Claude models](https://github.com/agamm/batchata/blob/main/batchata/providers/anthropic/models.py) | [All GPT models](https://github.com/agamm/batchata/blob/main/batchata/providers/openai/models.py) | [Gemini models](https://github.com/agamm/batchata/blob/main/batchata/providers/gemini/models.py) |
| Batch Discount | 50% | 50% | 50% |
| Polling Interval | 1s | 5s | 2s |
| Citations | ✅ | ❌ | ❌ |
| Structured Output | ✅ | ✅ | ✅ |
| File Types | PDF, TXT, DOCX, Images | PDF, Images | PDF, TXT, Images |
## Configuration
Set your API keys as environment variables:
```bash
export ANTHROPIC_API_KEY="your-key"
export OPENAI_API_KEY="your-key"
export GOOGLE_API_KEY="your-key" # For Gemini models
```
You can also use a `.env` file in your project root (requires python-dotenv):
```python
from dotenv import load_dotenv
load_dotenv()
from batchata import Batch
# Your API keys will now be loaded from .env
```
## Limitations
- Field/citation mapping is heuristic, which means it isn't perfect.
- Citation mapping only works with flat Pydantic models (no nested BaseModel fields).
- Cost tracking is not precise as the actual usage is only known after the batch is complete, try setting `items_per_batch` to a lower value for more accurate cost tracking.
## License
MIT License - see LICENSE file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "batchata",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.12",
"maintainer_email": null,
"keywords": "ai, ai-batch, ai-tools, anthropic, anthropic-batch, api, async, batch, batch-api, batch-processing, batch-requests, claude, cost-optimization, gemini, gemini-batch, google, gpt, llm, llm-batch, llms, machine-learning, openai, openai-batch",
"author": "Agam More",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/af/e3/5588e2265ff5a9d8530ee74e656e2081613e642daf197c9b83a1f6ece1bd/batchata-0.4.0.tar.gz",
"platform": null,
"description": "# Batchata\n\n<img alt=\"Batchata AI Batch Build Status\" src=\"https://github.com/agamm/batchata/workflows/Tests/badge.svg\" /><a href=\"https://pypi.org/project/batchata/\"><img alt=\"Batchata AI Batch PyPI version\" src=\"https://badge.fury.io/py/batchata.svg\" /></a>\n\nUnified Python API for AI Batch requests with cost tracking, Pydantic responses, citation mapping and parallel execution.\n\n<img width=\"1328\" height=\"598\" alt=\"image\" src=\"https://github.com/user-attachments/assets/b1b43070-f664-41a2-a85d-e2c589df556c\" />\n\n*This library is currently in beta - so there will be breaking changes*\n\n## Why AI-batching?\n\nAI providers offer batch APIs that process requests asynchronously at 50% reduced cost compared to real-time APIs. This is ideal for offline or batch processing tasks. However, managing batch jobs across providers, tracking costs, handling failures, and mapping citations back to source documents quickly becomes complex - that's where Batchata comes in.\n\n## Batchata Features\n\n- Native batch processing (50% cost savings via provider APIs)\n- Set `max_cost_usd` limits for batch requests\n- Time limit control with `.add_time_limit(seconds=, minutes=, hours=)`\n- State persistence in case of network interruption\n- Structured output `.json` format with Pydantic models\n- Citation support and field mapping (Anthropic only)\n- Multiple provider support (Anthropic, OpenAI, Google Gemini)\n\n## Installation\n\n### pip\n```bash\npip install batchata\n```\n\n### uv\n```bash\nuv add batchata\n```\n\n## Quick Start\n\n```python\nfrom batchata import Batch\n\n# Simple batch processing\nbatch = Batch(results_dir=\"./output\")\n .set_default_params(model=\"claude-sonnet-4-20250514\") # or \"gpt-4.1-2025-04-14\" or \"gemini-2.5-flash\"\n .add_cost_limit(usd=5.0)\n\nfor file in files:\n batch.add_job(file=file, prompt=\"Summarize\")\n\nrun = batch.run()\n\nresults = run.results() # {\"completed\": [JobResult], \"failed\": [JobResult], \"cancelled\": [JobResult]}\n```\n\n## Complete Example\n\n```python\nfrom batchata import Batch\nfrom pydantic import BaseModel\nfrom dotenv import load_dotenv\n\nload_dotenv() # Load API keys from .env\n\n# Define structured output\nclass InvoiceAnalysis(BaseModel):\n invoice_number: str\n total_amount: float\n vendor: str\n payment_status: str\n\n# Create batch configuration\nbatch = Batch(\n results_dir=\"./invoice_results\",\n max_parallel_batches=1,\n items_per_batch=3\n )\n .set_state(file=\"./invoice_state.json\", reuse_state=False)\n .set_default_params(model=\"claude-sonnet-4-20250514\", temperature=0.0)\n .add_cost_limit(usd=5.0)\n .add_time_limit(minutes=10) # Time limit of 10 minutes\n .set_verbosity(\"warn\") \n\n# Add jobs with structured output and citations\ninvoice_files = [\"path/to/invoice1.pdf\", \"path/to/invoice2.pdf\", \"path/to/invoice3.pdf\"]\nfor invoice_file in invoice_files:\n batch.add_job(\n file=invoice_file,\n prompt=\"Extract the invoice number, total amount, vendor name, and payment status.\",\n response_model=InvoiceAnalysis,\n enable_citations=True\n )\n\n# Execute with rich progress display\nprint(\"Starting batch processing...\")\nrun = batch.run(print_status=True)\n\n# Or use custom progress callback\nrun = batch.run(print_status=True)\n\n# Get results\nresults = run.results()\n\n# Process successful results\nfor result in results[\"completed\"]:\n analysis = result.parsed_response\n citations = result.citation_mappings\n print(f\"\\nInvoice: {analysis.invoice_number} (page: {citations.get(\"invoice_number\").page})\")\n print(f\" Vendor: {analysis.vendor} (page: {citations.get(\"vendor\").page})\")\n print(f\" Total: ${analysis.total_amount:.2f} (page: {citations.get(\"total_amount\").page})\")\n print(f\" Status: {analysis.payment_status} (page: {citations.get(\"payment_status\").page})\")\n\n# Process failed/cancelled results \nfor result in results[\"failed\"]:\n print(f\"\\nJob {result.job_id} failed: {result.error}\")\n\nfor result in results[\"cancelled\"]:\n print(f\"\\nJob {result.job_id} was cancelled: {result.error}\")\n```\n\n\n## Interactive Progress Display\n\nBatchata provides an interactive real-time progress display when using `print_status=True`:\n\n```python\nrun = batch.run(print_status=True)\n```\n\n<img width=\"2230\" height=\"222\" alt=\"image\" src=\"https://github.com/user-attachments/assets/caf549a6-92a1-4ee0-8ac7-eda2d0f280a7\" />\n\nThe interactive display shows:\n- **Job Progress**: Completed/total jobs with progress bar\n- **Batch Status**: Provider batch completion status \n- **Real-time Cost**: Current spend vs limit (if set)\n- **Elapsed Time**: Time since batch started\n- **Live Updates**: Refreshes automatically as jobs complete\n\n## File Structure\n\n```\n./results/\n\u251c\u2500\u2500 job-abc123.json\n\u251c\u2500\u2500 job-def456.json\n\u251c\u2500\u2500 job-ghi789.json\n\u2514\u2500\u2500 raw_files/\n \u2514\u2500\u2500 responses/\n \u251c\u2500\u2500 job-abc123_raw.json\n \u251c\u2500\u2500 job-def456_raw.json\n \u2514\u2500\u2500 job-ghi789_raw.json\n\n./batch_state.json # Batch state\n```\n\n## Supported Providers\n\n| Feature | Anthropic | OpenAI | Google Gemini |\n|---------|-----------|--------|---------------|\n| Models | [All Claude models](https://github.com/agamm/batchata/blob/main/batchata/providers/anthropic/models.py) | [All GPT models](https://github.com/agamm/batchata/blob/main/batchata/providers/openai/models.py) | [Gemini models](https://github.com/agamm/batchata/blob/main/batchata/providers/gemini/models.py) |\n| Batch Discount | 50% | 50% | 50% |\n| Polling Interval | 1s | 5s | 2s |\n| Citations | \u2705 | \u274c | \u274c |\n| Structured Output | \u2705 | \u2705 | \u2705 |\n| File Types | PDF, TXT, DOCX, Images | PDF, Images | PDF, TXT, Images |\n\n## Configuration\n\nSet your API keys as environment variables:\n```bash\nexport ANTHROPIC_API_KEY=\"your-key\"\nexport OPENAI_API_KEY=\"your-key\"\nexport GOOGLE_API_KEY=\"your-key\" # For Gemini models\n```\n\nYou can also use a `.env` file in your project root (requires python-dotenv):\n```python\nfrom dotenv import load_dotenv\nload_dotenv()\n\nfrom batchata import Batch\n# Your API keys will now be loaded from .env\n```\n\n## Limitations\n\n- Field/citation mapping is heuristic, which means it isn't perfect.\n- Citation mapping only works with flat Pydantic models (no nested BaseModel fields).\n- Cost tracking is not precise as the actual usage is only known after the batch is complete, try setting `items_per_batch` to a lower value for more accurate cost tracking.\n\n\n## License\n\nMIT License - see LICENSE file for details.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Unified Python API for AI batch requests with 50% cost savings on OpenAI and Anthropic",
"version": "0.4.0",
"project_urls": {
"Changelog": "https://github.com/agamm/batchata/releases",
"Documentation": "https://agamm.github.io/batchata/",
"Homepage": "https://github.com/agamm/batchata",
"Issues": "https://github.com/agamm/batchata/issues",
"Repository": "https://github.com/agamm/batchata"
},
"split_keywords": [
"ai",
" ai-batch",
" ai-tools",
" anthropic",
" anthropic-batch",
" api",
" async",
" batch",
" batch-api",
" batch-processing",
" batch-requests",
" claude",
" cost-optimization",
" gemini",
" gemini-batch",
" google",
" gpt",
" llm",
" llm-batch",
" llms",
" machine-learning",
" openai",
" openai-batch"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "452343ec301b6e8ec2d48c8c446c3a3cc591d4414d90b4fb2e3af1203653df22",
"md5": "8c69483e725636d497de0a81291c338e",
"sha256": "cd9acdd61fa4aa7f561afbbddbddc072dac5215814106db2d93e5000845d142a"
},
"downloads": -1,
"filename": "batchata-0.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8c69483e725636d497de0a81291c338e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.12",
"size": 67202,
"upload_time": "2025-07-27T17:08:51",
"upload_time_iso_8601": "2025-07-27T17:08:51.411719Z",
"url": "https://files.pythonhosted.org/packages/45/23/43ec301b6e8ec2d48c8c446c3a3cc591d4414d90b4fb2e3af1203653df22/batchata-0.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "afe35588e2265ff5a9d8530ee74e656e2081613e642daf197c9b83a1f6ece1bd",
"md5": "9daa3ccd88e99cdfd7c3b8d74315f9f0",
"sha256": "c019781d266a9fb55c89e2d55a7dee4fe9e17c3278f332ec776ec98e03507405"
},
"downloads": -1,
"filename": "batchata-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "9daa3ccd88e99cdfd7c3b8d74315f9f0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.12",
"size": 258657,
"upload_time": "2025-07-27T17:08:53",
"upload_time_iso_8601": "2025-07-27T17:08:53.109471Z",
"url": "https://files.pythonhosted.org/packages/af/e3/5588e2265ff5a9d8530ee74e656e2081613e642daf197c9b83a1f6ece1bd/batchata-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-27 17:08:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "agamm",
"github_project": "batchata",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "batchata"
}