Metadata-Version: 2.5
Name: green-batch-ai
Version: 0.1.0
Summary: Run bulk AI jobs cleaner and cheaper: find the lowest-carbon time (and place) to run them from grid forecasts, write OpenAI, Anthropic and Gemini batch files at half price, and plan concurrency under rate limits.
Project-URL: Homepage, https://www.ahmadzia.com/tools/green-batch-ai
Project-URL: Documentation, https://www.ahmadzia.com/tools/green-batch-ai
Project-URL: Source, https://github.com/MuhammadAhmadZia/green-batch-ai
Project-URL: Issues, https://github.com/MuhammadAhmadZia/green-batch-ai/issues
Project-URL: Changelog, https://github.com/MuhammadAhmadZia/green-batch-ai/blob/main/CHANGELOG.md
Project-URL: Author, https://www.ahmadzia.com
Project-URL: Research, https://www.ahmadzia.com/research
Author-email: Muhammad Ahmad Zia <muhammad.zia@cs.uol.edu.pk>
License-Expression: MIT
License-File: LICENSE
Keywords: anthropic-batch,batch,carbon-aware,carbon-intensity,electricity-maps,gemini-batch,green-ai,llm,openai-batch,rate-limits,scheduling,sustainability
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.9
Requires-Dist: llm-cost-shield>=0.1.0
Provides-Extra: test
Requires-Dist: pytest>=7; extra == 'test'
Description-Content-Type: text/markdown

# green-batch-ai

<!-- badges -->
[![PyPI](https://img.shields.io/pypi/v/green-batch-ai?color=3a7a4e&label=PyPI)](https://pypi.org/project/green-batch-ai/) [![CI](https://github.com/MuhammadAhmadZia/green-batch-ai/actions/workflows/ci.yml/badge.svg)](https://github.com/MuhammadAhmadZia/green-batch-ai/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/badge/license-MIT-0d2018)](LICENSE) [![Agent skill](https://img.shields.io/badge/agent%20skill-green--batch--ai-c5dfc9)](skills/green-batch-ai/SKILL.md) [![Guide](https://img.shields.io/badge/guide-ahmadzia.com-3a7a4e)](https://www.ahmadzia.com/tools/green-batch-ai)

**Run your bulk AI jobs cleaner and cheaper.** Find the time (and place) when the grid is cleanest for jobs you run yourself, turn a list of prompts into OpenAI, Anthropic or Gemini batch files at half the price, and work out how many requests to keep in flight under your rate limits.

```bash
pip install green-batch-ai
green-batch-ai plan --signal uk --hours 4 --watts 2000 --deadline 2026-12-05T12:00Z --tz Europe/London
```

```
Carbon-aware plan (uk): 4 h job, 8 kWh
Forecast in your window: 96 to 246 gCO2/kWh (average 162)
Best start: Sat 05 Dec 02:00 Europe/London, finishing Sat 05 Dec 06:00 Europe/London
Emissions: 789 g at 99 g/kWh, against 1.87 kg if started now (234 g/kWh)
Saving: 1.08 kg (58%)
```

(From the real Great Britain forecast of 4 December 2026, saved in `examples/`: a windy night more than halves the carbon of the same job.)

## What it does

### 1. When and where to run (`plan`, `run`)

For jobs on your own hardware or cloud machines (embedding a catalogue, scoring a dataset, fine-tuning, nightly evaluations), it reads a carbon-intensity forecast and finds:

- the best **start time** for a job that runs in one go, or
- the cleanest **slots** for a job that can pause and resume (`--interruptible`),
- or the best **place** among several zones (`--zones DE,FR,SE`).

It compares that with starting now and shows the saving. `run` waits until the best start and then runs your command:

```bash
green-batch-ai run --signal uk --postcode RG1 --hours 3 --watts 1500 --deadline 2026-12-06T08:00Z -- python embed_catalog.py
```

Forecast sources:

| `--signal` | Coverage | Key |
|---|---|---|
| `uk` | Great Britain, national, by region (`--region-id`) or outward postcode (`--postcode`), half-hourly, 48 h | none (NESO Carbon Intensity API, CC BY 4.0) |
| `electricitymaps` | Worldwide zones (`--zone`), or a cloud data centre (`--provider aws --cloud-region eu-west-1`), hourly | `ELECTRICITYMAPS_TOKEN` |
| `file:forecast.csv` | Anything: your utility's forecast, a WattTime export (`time,value` CSV or JSON) | none |
| `static:450` | Grids with no forecast | none |

**It tells you when waiting is pointless.** Where carbon intensity hardly changes over a day (much of South Asia today, where most power is fossil all day), shifting saves little, and the plan says so. Research on real grids finds the same: the gains from shifting are real but limited and depend heavily on the region (Sukprasert et al., EuroSys 2024). In those places, a smaller model or a cleaner region matters more.

### 2. Batch files at half price (`batch-files`)

If results can wait up to a day, every major provider charges half for batch jobs, with their own, higher limits:

| Provider | Price | Limits per batch | Turnaround |
|---|---|---|---|
| OpenAI Batch API | 50% off | 50,000 requests, 200 MB file | within 24 h |
| Anthropic Message Batches | 50% off | 100,000 requests or 256 MB | most within 1 h, at most 24 h; results kept 29 days |
| Gemini Batch Mode | 50% off | input file up to 2 GB | target 24 h |

```bash
green-batch-ai batch-files prompts.csv --provider anthropic --model your-model-id --out batches/ --prices prices.json --output-tokens 150
```

It reads a CSV or JSONL of prompts (`id`, `prompt` or `messages`, optional `system`, `max_tokens`, `model`), writes files in each provider's format (OpenAI JSONL for chat completions or the Responses API, the Anthropic request body, Gemini JSONL with `key` and `request`), splits them under the limits, and estimates normal and batch cost from an [llm-cost-shield](https://www.ahmadzia.com/tools/llm-cost-shield) price table.

The provider decides when a batch runs inside its window, so carbon-aware timing is for jobs you run yourself; batching is the saving for API jobs.

### 3. Concurrency under rate limits (`concurrency`)

For jobs that must use the normal API:

```bash
green-batch-ai concurrency --requests 100000 --rpm 600 --tpm 2000000 --input-tokens 500 --output-tokens 200 --latency 2
100,000 requests: run 18 at a time for about 9.00 requests/s, limited by requests per minute (with 10% headroom).
Expected duration: 185 minutes (3.1 h).
Use the provider's batch API instead if results can wait: half the price, separate limits.
```

By Little's law, requests in flight equal throughput times latency. More concurrency than that only produces 429 errors and retries, which waste time, money and energy.

## From Python

```python
from green_batch_ai import uk, plan, format_plan
p = plan(uk(postcode="M1"), hours=3, kwh=4.5)
print(format_plan(p, tz="Europe/London"))
```

## Prior work

[CATS, the Climate-Aware Task Scheduler](https://github.com/GreenScheduler/cats) (Bartholomew et al., JOSS 2025) schedules jobs for the UK grid and HPC schedulers. green-batch-ai adds other forecast sources, pausable jobs, comparing places, and the parts specific to AI workloads: provider batch files, batch pricing and rate-limit planning.

## Limits

- Forecasts are forecasts; real intensity can differ by a few percent or more.
- Average intensity is used (what the sources publish). Marginal intensity, the plants that respond to extra demand, can differ.
- For the energy of a job, measure a short run with [greeninfer](https://www.ahmadzia.com/tools/greeninfer) or a power meter, or use the machine's typical draw.

## SDG alignment

This tool supports **SDG target 7.3** (energy efficiency): batching and right-sized concurrency remove wasted retries and idle time. It also supports **target 13.3** (improve awareness and capacity on climate change mitigation), by making the carbon cost of when and where a job runs visible and actionable, and **target 7.2** (increase the share of renewable energy), since running flexible work when wind and solar are plentiful uses more of them.

## Sources

- NESO Carbon Intensity API. https://carbonintensity.org.uk and https://carbon-intensity.github.io/api-definitions/
- Electricity Maps API reference. https://portal.electricitymaps.com/developer-hub/api/reference
- OpenAI Batch API guide. https://developers.openai.com/api/docs/guides/batch
- Anthropic Message Batches. https://platform.claude.com/docs/en/build-with-claude/batch-processing
- Gemini API Batch Mode. https://ai.google.dev/gemini-api/docs/batch-mode
- Wiesner P. et al. (2021). Let's Wait Awhile: How Temporal Workload Shifting Can Reduce Carbon Emissions in the Cloud. Middleware '21. arXiv:2110.13234
- Sukprasert T. et al. (2024). On the Limitations of Carbon-Aware Temporal and Spatial Workload Shifting in the Cloud. EuroSys '24. arXiv:2306.06502
- Bartholomew S. L. et al. (2025). CATS: The Climate Aware Task Scheduler. Journal of Open Source Software 10(111), 8251.

## About

Built by [Ahmad Zia](https://www.ahmadzia.com), an AI and e-commerce consultant and green-AI researcher who teaches parallel and distributed computing. MIT licensed. Forecast data keeps its own licence (NESO data CC BY 4.0).

<!-- ahmadzia-links -->
## Guide, help and author

- **Full guide with examples and FAQs:** [www.ahmadzia.com/tools/green-batch-ai](https://www.ahmadzia.com/tools/green-batch-ai)
- **Background reading:** [How to reduce LLM costs: 10 methods ranked by what they save](https://www.ahmadzia.com/blog/how-to-reduce-llm-costs)
- **All 30 free tools and agent skills:** [ahmadzia.com/tools](https://www.ahmadzia.com/tools) · [ahmadzia.com/skills](https://www.ahmadzia.com/skills)
- **Want it set up, fixed or tuned for your store?** [Book a free call](https://www.ahmadzia.com/contact)

Built by **[Ahmad Zia](https://www.ahmadzia.com)** (Muhammad Ahmad Zia), an AI consultant for ecommerce since 2020 and green AI researcher at the University of Lahore. [Research](https://www.ahmadzia.com/research) · [Google Scholar](https://scholar.google.com/citations?user=sqjdX2EAAAAJ&hl=en) · [ORCID](https://orcid.org/0000-0002-3208-6246) · [LinkedIn](https://www.linkedin.com/in/muhammad-ahmadzia/) · [GitHub](https://github.com/MuhammadAhmadZia)
<!-- /ahmadzia-links -->

## More AI cost control tools

- [llm-cost-shield](https://github.com/MuhammadAhmadZia/llm-cost-shield): Stop surprise LLM bills with per-user budgets, caching and cheap-model routing
- [right-size-model](https://github.com/MuhammadAhmadZia/right-size-model): Find the cheapest LLM that is good enough for each task, with a statistical test
- [parallel-inference-tuner](https://github.com/MuhammadAhmadZia/parallel-inference-tuner): Find the fastest, most energy-efficient threads, workers and batching for model serving

All 30 tools and agent skills: [github.com/MuhammadAhmadZia](https://github.com/MuhammadAhmadZia) · [ahmadzia.com/tools](https://www.ahmadzia.com/tools)
