Batch API Orchestrator
Category: Engineering
Domain: AI Engineering
Overview
Decide when to run LLM work through an asynchronous batch API versus realtime/streaming, then design the job so it is cheap, idempotent, and resilient to partial failure. Batch APIs typically cost roughly half of realtime in exchange for higher latency (results arrive over minutes to hours, not milliseconds), which makes them ideal for evals, backfills, embeddings, and bulk classification/extraction — and wrong for anything a human is waiting on. This skill is model- and vendor-agnostic: it reasons about the batch pattern, not any one provider's API.
Clarify First
Before recommending or designing a batch job, confirm these inputs. If any is unknown or vague, ASK — do not assume:
Stop rule: ask only the 2-3 that most change the output. If the user says "just draft it," proceed and list your assumptions.
Quick Start
cd engineering/batch-api-orchestrator
# 1. Should this be batch or realtime, and what does it cost?
python scripts/batch_cost_estimator.py \
--requests 50000 --avg-input-tokens 800 --avg-output-tokens 200 \
--realtime-input-price 3.0 --realtime-output-price 15.0 \
--batch-discount 0.5 --latency-tolerance hours
# 2. Plan the chunking / idempotency / retry strategy for the job
python scripts/batch_job_planner.py \
--total-items 50000 --max-batch-size 10000 --retry-policy exponential --json
Tools Overview
| Tool |
Purpose |
Key Flags |
scripts/batch_cost_estimator.py |
Compare realtime vs batch cost, show savings, and recommend batch or realtime given latency tolerance |
--requests, --avg-input-tokens, --avg-output-tokens, --realtime-input-price, --realtime-output-price, --batch-discount, --latency-tolerance, --json |
scripts/batch_job_planner.py |
Produce a chunking + idempotency + partial-failure plan for a bulk job |
--total-items, --max-batch-size, --retry-policy, --max-retries, --json |
Both scripts: Python 3 standard library only, argparse CLI, --json and human-readable output. Run --help for full usage.
Workflows
1. Decide batch vs realtime, then size the cost
- Gather volume and token shape (
--requests, --avg-input-tokens, --avg-output-tokens).
- Plug in your vendor prices and batch discount — never assume them.
- Run
batch_cost_estimator.py with the real --latency-tolerance (realtime, minutes, or hours).
- Read the verdict: if work is interactive, the tool recommends realtime regardless of savings; otherwise it quantifies the batch savings.
- Sanity-check against the decision tree in
references/batch-patterns-and-decision-tree.md.
2. Design a resilient bulk job
- Run
batch_job_planner.py with --total-items, --max-batch-size, and a --retry-policy.
- Adopt the generated idempotency-key scheme so re-submitting a chunk never double-charges or double-writes.
- Wire result reconciliation: match every output back to its request id, and collect the unmatched into a dead-letter set.
- Apply the partial-failure handling (retry only failed items, never the whole batch) from the reference.
- Choose polling vs callback for completion, per the reference guidance.
Reference Documentation
- references/batch-patterns-and-decision-tree.md — when-to-batch decision tree; job design (idempotency keys, partial failures, reconciliation, polling vs callback); fitting use cases (evals, backfills, embeddings, bulk classification/extraction); and anti-patterns such as batching interactive requests.
- references/cost-and-throughput-economics.md — the cost/throughput tradeoff in depth: the ~half-cost rule of thumb, throughput vs latency, queueing, chunk sizing, and how to model the break-even between a faster realtime path and a cheaper batch path.
Common Patterns
- Batch the patient, stream the impatient — if no human is blocked on the result, default to batch for the cost win; reserve realtime/streaming for interactive UX.
- Idempotency key per item — derive a stable key (e.g. hash of input + job version) so retries and re-submissions are safe and never double-billed.
- Retry the item, not the batch — on partial failure, re-enqueue only the failed request ids; resubmitting the whole chunk wastes money and re-runs successes.
- Reconcile by request id — never rely on output ordering; join results back to inputs by id and route the unmatched to a dead-letter queue for inspection.
- Right-size chunks — split by the vendor's max-batch limit and by your own blast-radius tolerance, not into one giant job whose failure is all-or-nothing.
- Embeddings and evals are the sweet spot — large, latency-insensitive, embarrassingly parallel workloads capture the full batch discount with the least risk.
1---
2name: batch-api-orchestrator
3description: >
4 This skill should be used when the user asks to "batch LLM requests", "should I use
5 the batch API", "estimate batch vs realtime cost", "design a bulk LLM job", or
6 "process thousands of prompts cheaply".
7license: MIT + Commons Clause
8metadata:
9 version: 1.0.0
10 author: borghei
11 category: engineering
12 domain: ai-engineering
13 updated: 2026-06-29
14 tags: [batch-api, llm, cost-optimization, throughput, async]
15---
16
17# Batch API Orchestrator
18
19> **Category:** Engineering
20> **Domain:** AI Engineering
21
22## Overview
23
24Decide when to run LLM work through an asynchronous batch API versus realtime/streaming, then design the job so it is cheap, idempotent, and resilient to partial failure. Batch APIs typically cost roughly half of realtime in exchange for higher latency (results arrive over minutes to hours, not milliseconds), which makes them ideal for evals, backfills, embeddings, and bulk classification/extraction — and wrong for anything a human is waiting on. This skill is model- and vendor-agnostic: it reasons about the batch *pattern*, not any one provider's API.
25
26## Clarify First
27
28Before recommending or designing a batch job, confirm these inputs. If any is unknown or vague, ASK — do not assume:
29
30- [ ] **Latency tolerance** — is a human waiting (interactive), or can results land in minutes/hours? (sets `--latency-tolerance` and the batch-vs-realtime verdict)
31- [ ] **Volume & token shape** — how many requests, and the average input/output tokens each? (sets `--requests`, `--avg-input-tokens`, `--avg-output-tokens` for the cost estimate)
32- [ ] **Pricing & discount** — your realtime per-token prices and the batch discount your vendor offers (sets `--realtime-input-price`, `--realtime-output-price`, `--batch-discount`; defaults are neutral placeholders, not real prices)
33
34Stop rule: ask only the 2-3 that most change the output. If the user says "just draft it," proceed and list your assumptions.
35
36## Quick Start
37
38```bash
39cd engineering/batch-api-orchestrator
40
41# 1. Should this be batch or realtime, and what does it cost?
42python scripts/batch_cost_estimator.py \
43 --requests 50000 --avg-input-tokens 800 --avg-output-tokens 200 \
44 --realtime-input-price 3.0 --realtime-output-price 15.0 \
45 --batch-discount 0.5 --latency-tolerance hours
46
47# 2. Plan the chunking / idempotency / retry strategy for the job
48python scripts/batch_job_planner.py \
49 --total-items 50000 --max-batch-size 10000 --retry-policy exponential --json
50```
51
52## Tools Overview
53
54| Tool | Purpose | Key Flags |
55|------|---------|-----------|
56| `scripts/batch_cost_estimator.py` | Compare realtime vs batch cost, show savings, and recommend batch or realtime given latency tolerance | `--requests`, `--avg-input-tokens`, `--avg-output-tokens`, `--realtime-input-price`, `--realtime-output-price`, `--batch-discount`, `--latency-tolerance`, `--json` |
57| `scripts/batch_job_planner.py` | Produce a chunking + idempotency + partial-failure plan for a bulk job | `--total-items`, `--max-batch-size`, `--retry-policy`, `--max-retries`, `--json` |
58
59Both scripts: Python 3 standard library only, argparse CLI, `--json` and human-readable output. Run `--help` for full usage.
60
61## Workflows
62
63### 1. Decide batch vs realtime, then size the cost
64
651. Gather volume and token shape (`--requests`, `--avg-input-tokens`, `--avg-output-tokens`).
662. Plug in *your* vendor prices and batch discount — never assume them.
673. Run `batch_cost_estimator.py` with the real `--latency-tolerance` (`realtime`, `minutes`, or `hours`).
684. Read the verdict: if work is interactive, the tool recommends realtime regardless of savings; otherwise it quantifies the batch savings.
695. Sanity-check against the decision tree in `references/batch-patterns-and-decision-tree.md`.
70
71### 2. Design a resilient bulk job
72
731. Run `batch_job_planner.py` with `--total-items`, `--max-batch-size`, and a `--retry-policy`.
742. Adopt the generated idempotency-key scheme so re-submitting a chunk never double-charges or double-writes.
753. Wire result reconciliation: match every output back to its request id, and collect the unmatched into a dead-letter set.
764. Apply the partial-failure handling (retry only failed items, never the whole batch) from the reference.
775. Choose polling vs callback for completion, per the reference guidance.
78
79## Reference Documentation
80
81- **[references/batch-patterns-and-decision-tree.md](references/batch-patterns-and-decision-tree.md)** — when-to-batch decision tree; job design (idempotency keys, partial failures, reconciliation, polling vs callback); fitting use cases (evals, backfills, embeddings, bulk classification/extraction); and anti-patterns such as batching interactive requests.
82- **[references/cost-and-throughput-economics.md](references/cost-and-throughput-economics.md)** — the cost/throughput tradeoff in depth: the ~half-cost rule of thumb, throughput vs latency, queueing, chunk sizing, and how to model the break-even between a faster realtime path and a cheaper batch path.
83
84## Common Patterns
85
86- **Batch the patient, stream the impatient** — if no human is blocked on the result, default to batch for the cost win; reserve realtime/streaming for interactive UX.
87- **Idempotency key per item** — derive a stable key (e.g. hash of input + job version) so retries and re-submissions are safe and never double-billed.
88- **Retry the item, not the batch** — on partial failure, re-enqueue only the failed request ids; resubmitting the whole chunk wastes money and re-runs successes.
89- **Reconcile by request id** — never rely on output ordering; join results back to inputs by id and route the unmatched to a dead-letter queue for inspection.
90- **Right-size chunks** — split by the vendor's max-batch limit and by your own blast-radius tolerance, not into one giant job whose failure is all-or-nothing.
91- **Embeddings and evals are the sweet spot** — large, latency-insensitive, embarrassingly parallel workloads capture the full batch discount with the least risk.