Category: Engineering
Domain: AI Engineering
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.
Before recommending or designing a batch job, settle the inputs below. When one is missing or vague and would change the result, ask about it; when it wouldn't, use a sensible default and say which one you used.
--latency-tolerance and the batch-vs-realtime verdict).--requests, --avg-input-tokens, --avg-output-tokens for the cost estimate).--realtime-input-price, --realtime-output-price, --batch-discount; defaults are neutral placeholders, not real prices).Ask about the two or three inputs that matter most for this request. If the user says "just draft it," proceed and list your assumptions.
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
| 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.
Exit codes (both). 0 estimate or plan printed; 2 invalid arguments (negative or zero counts,
discount outside 0-1, unknown latency tolerance or retry policy).
| Anthropic Message Batches | OpenAI Batch API | |
|---|---|---|
| Discount vs realtime | 50% | 50% |
| Max per batch | 100,000 requests or 256 MB | 50,000 requests per input file, input file up to 200 MB |
| Completion window | most finish within an hour; expire after 24 h | 24 h window |
| Result retention | 29 days | output file deleted 30 days after the batch completes (set 1 hour to 30 days with output_expires_after) |
| Prompt caching | stacks with the batch discount | check current docs |
Use these to set --max-batch-size and --batch-discount. Limits change, so read the provider's
batch documentation before sizing a production job.
Done when:
--requests, --avg-input-tokens, --avg-output-tokens).batch_cost_estimator.py with the real --latency-tolerance (realtime, minutes, or hours).references/batch-patterns-and-decision-tree.md.batch_job_planner.py with --total-items, --max-batch-size, and a --retry-policy.In Scope:
realtime, minutes, hours, days)batch_cost_estimator.py from request volume, average token counts, the user's own per-token prices and batch discountbatch_job_planner.py: chunk sizes, an idempotency-key scheme, retry policy (exponential, fixed or none) and partial-failure handlingOut of Scope:
engineering/llm-cost-optimizer/)engineering/agentic-evaluation-framework/ and engineering/agent-harness/)Important Caveats:
batch_cost_estimator.py are neutral placeholders, not real prices. Estimates mean something only after the user supplies current vendor prices and discount.Mistake: Sending user-facing requests through the batch API to save money.
Why it happens: The discount is large and the latency cost is invisible in a spreadsheet.
Instead: Batch only work nobody waits on. batch_cost_estimator.py recommends realtime whenever --latency-tolerance realtime is set.
Mistake: Submitting a whole backfill as a single maximum-size batch.
Why it happens: Fewer jobs feel simpler to track.
Instead: Chunk below the provider limit so a bad prompt or schema error costs one chunk, not the run. Pilot a small chunk first.
Mistake: Zipping results to inputs by position.
Why it happens: Small tests come back in order.
Instead: Join on the request id you set (custom_id or equivalent) and send unmatched results to a dead-letter set.
scripts/batch_cost_estimator.py — realtime vs batch cost and a batch/realtime verdict from latency tolerance.scripts/batch_job_planner.py — chunk plan, idempotency-key scheme and retry plan.references/ — decision tree and job design; cost and throughput economics.