Sub topic 3
Trading latency for lower cost
This chapter assumes you can write basic Python, have called a chat completions API, and know what tokens and rate limits are. It also assumes you've seen the idea behind a batch job: you submit many requests as one file and collect the results later. Here you'll learn what you give up in exchange for the lower price, how to estimate the savings, and when the trade stops being worth it.
What the discount is
Major providers price batch work below real-time work. OpenAI's Batch API, Anthropic's Message Batches, and Google's Gemini batch mode have each offered roughly 50% off the standard per-token price for both input and output tokens. The model, the prompt format, and the response format stay the same. Only the delivery guarantee changes.
The provider can discount because you give up control over timing. In exchange for accepting a completion window (24 hours in the OpenAI example), the provider can run your requests when spare capacity exists. Real-time traffic keeps priority. You pay less because you've made the provider's capacity planning easier.
Pricing changes often. Treat every number in this chapter as an illustration and read the current pricing page before you commit to a budget.
What you give up
The discount costs you four things.
- Turnaround time. A batch may finish in minutes or take the full window. The window is an upper bound, not a promise of speed. Plan for the worst case.
- Interactivity. You can't stream tokens, react to one response before sending the next, or run a conversation turn by turn. Every request must be fully specified up front.
- Fast feedback. In a synchronous loop, a bad prompt shows up in seconds. In a batch, you may learn about it hours later.
- Simplicity. You now manage files, job statuses, polling, and result matching. That is engineering time, and it has a cost.
Estimating the savings
The cost of a job is linear in tokens. For each request, multiply input tokens by the input price and output tokens by the output price, then sum over all requests. The batch cost is that total times the discount factor, plus any overhead from retries.
The script below runs as written. The prices are illustrative per-million-token rates, not a quote.
def estimate_cost(
n_requests: int,
input_tokens: int,
output_tokens: int,
input_price_per_m: float,
output_price_per_m: float,
batch_discount: float = 0.5,
retry_rate: float = 0.0,
) -> dict:
"""Compare synchronous and batch cost for one workload.
retry_rate is the fraction of requests that must be resubmitted
and therefore paid for a second time.
"""
per_request = (
input_tokens * input_price_per_m + output_tokens * output_price_per_m
) / 1_000_000
sync_cost = n_requests * per_request
batch_cost = sync_cost * (1 - batch_discount) * (1 + retry_rate)
return {
"sync": round(sync_cost, 2),
"batch": round(batch_cost, 2),
"saved": round(sync_cost - batch_cost, 2),
}
result = estimate_cost(
n_requests=100_000,
input_tokens=800,
output_tokens=100,
input_price_per_m=0.15,
output_price_per_m=0.60,
retry_rate=0.03,
)
print(result)It prints {'sync': 18.0, 'batch': 9.27, 'saved': 8.73}.
The 100,000 requests use 80 million input tokens (about $12.00) and 10 million output tokens (about $6.00), so the synchronous cost is $18.00. The batch price is half of that, $9.00. A 3% retry rate adds $0.27, which leaves $8.73 saved.
The example shows two things. First, the saving scales with volume. On this small model it is about $8.73 for 100,000 documents, which may not justify new infrastructure. On a large model priced 20 to 50 times higher, the same job saves hundreds of dollars. Second, retries eat into the saving only slightly, as long as you resubmit just the failed requests (a later chapter covers this).
Where the arithmetic gets less clear
The simple 50% rule has caveats. Check each one before you promise a number to anyone.
- Prompt caching. Providers offer separate discounts for repeated prompt prefixes. Whether the batch and caching discounts stack, and how reliably cache hits occur inside a batch, differs by provider. Don't assume both apply in full.
- Output length. Output tokens usually cost several times more than input tokens. A job with long outputs, such as essays, saves more in absolute dollars. The rate is still 50%.
- Model coverage. Not every model or endpoint is available in batch mode. Check that your model and endpoint are supported before you design around them.
- Wasted runs. A discount on a wrong run is still a wasted run. If your prompt has a bug and you only find it after the batch finishes, you pay for 100,000 useless responses at half price. A small pilot batch (a later chapter) is the usual defense.
- Quotas. On OpenAI, batch jobs count against a separate enqueued-token quota, not your synchronous rate limits. That is often a benefit, because a large batch won't starve your live traffic. It is also a constraint on how much you can queue at once.
Weighing the trade
The question is not "is batch cheaper?" It nearly always is per token. The question is whether the latency and complexity are acceptable for this workload. Three factors decide it.
- Deadline. If a human or a downstream system needs the answer within seconds or minutes, batch is out. If a result is needed by tomorrow morning, a 24-hour window may still be too tight, because you also need time to submit, collect, and retry. Work backward from the real deadline, not the nominal window.
- Volume. Savings are proportional to spend. Tiny jobs save pennies and still cost you the code to manage them.
- Independence. Batch requests run separately and in no guaranteed order. If request B needs the output of request A, you can't put both in one batch. You would need one batch per stage, and the delays add up. A three-stage pipeline with a 24-hour window per stage has a worst case of three days.
Worked comparison
The table below applies those factors to common workloads.
| Workload | Deadline | Dependencies | Verdict |
|---|---|---|---|
| Chat assistant reply | Seconds | Conversation state | Synchronous |
| Classify 2 million support tickets for a monthly report | Days | None | Batch |
| Nightly summaries of the day's documents | Next morning | None | Batch, with a safety margin |
| Run an evaluation set against a new prompt | Hours to days | None | Batch, if you can wait |
| Multi-step agent that calls tools | Seconds | Each step needs the last | Synchronous |
| One-off summary of 20 files | Today | None | Synchronous (savings too small) |
The nightly summary case deserves a closer look. If the job submits at 22:00 and the window is 24 hours, the worst case finishes at 22:00 the next day. A report due at 08:00 can't depend on that. Either start earlier, use a shorter window if the provider offers one, or accept that on some days the report will be late.
Measuring instead of guessing
You don't have to trust the 50% figure or your token estimates. Two habits keep the numbers honest.
- Count tokens from real data. Sample 100 inputs, run them, and read the
usagefield in each response. Multiply the average by your volume. Don't estimate from word counts alone. - Read the billing result. After the pilot batch, compare the usage dashboard or invoice line to your estimate. A large gap means your token counts or your assumptions about the discount are wrong. Find out why before you submit the full job.
For retries, track the failure rate of your pilot. A 0.5% failure rate adds almost nothing. A 15% failure rate means you are paying for requests twice, and the cheaper fix is usually to repair the cause (malformed bodies, oversized inputs) before the full run.
Failure modes to plan for
Cost planning has its own failure cases.
- Window expiry. If the provider can't finish within the window, the batch ends in an
expiredstatus. Requests that completed are returned and billed. Unprocessed requests are reported as errors, and you resubmit them. Your budget should allow for a second pass on some fraction of the work. - Underestimated outputs. If you set no
max_tokenslimit and the model writes long answers, the output cost can exceed your estimate. Set a limit on every request, as in the request file you built earlier. - Hidden duplicates. Resubmitting a whole file because a few requests failed doubles the cost. Resubmit only the failures.
- Opportunity cost. An engineer building and debugging batch machinery for a $10 job spends far more than the saving. Count time as part of the cost.
Summary
Batch processing swaps latency, interactivity, and some simplicity for a lower per-token price, about half on the major providers at the time of writing. The saving is real but proportional to spend. It shrinks when jobs are small, when runs are wasted on untested prompts, or when retries are sloppy. Decide by deadline, volume, and request independence. Then measure with a pilot before you trust the estimate.
Exercise
A team must label 50,000 product reviews. Each request uses about 600 input tokens and 20 output tokens. The model costs $2.00 per million input tokens and $8.00 per million output tokens. The batch discount is 50%, and about 2% of requests must be resubmitted. (a) What is the synchronous cost, and what is the batch cost including retries? (b) The labels are needed for a dashboard that refreshes in 6 hours. Is a 24-hour batch window suitable?
Show answer
(a) Per request: 600 x $2.00 / 1,000,000 = $0.0012 for input, plus 20 x $8.00 / 1,000,000 = $0.00016 for output, total $0.00136. For 50,000 requests the synchronous cost is $68.00. The batch cost is $68.00 x 0.5 x 1.02 = $34.68, so the saving is $33.32. (b) No. The window is an upper bound of 24 hours, which exceeds the 6-hour refresh. Even if batches usually finish sooner, the team can't guarantee the deadline, and retries add more time. They should use synchronous calls, or accept a looser dashboard schedule.
Exercise
Why can a pipeline with three dependent steps be a poor fit for batch even when each step is cheap? Give the worst-case turnaround for a 24-hour window.
Show answer
A request in step two needs the output of step one, so the steps can't share a batch. Each stage needs its own batch and must wait for the previous one to finish. The worst case is 3 x 24 = 72 hours, plus time to collect, validate, and retry between stages. The latency adds up even though the per-token price is low.