Sub topic 3
Trading latency for lower cost
This chapter assumes you can call a chat completions API from Python, know what input and output tokens are, and have read the previous chapters on synchronous versus batch calls and on workloads that fit offline processing.
What you give up and what you get
A batch API discounts your requests in exchange for time. With the OpenAI Batch API, tokens are billed at about half the synchronous price, and the job gets up to 24 hours to finish. Anthropic's Message Batches offer a similar discount. Prices change, so check the provider's pricing page before you plan around a number.
The discount is not the only gain. Batch jobs usually draw on a separate rate-limit pool, so a large job does not use up the quota your real-time traffic needs.
You give up three things:
- Predictable latency. The job often finishes in minutes to hours, but the provider only commits to the completion window. If the window passes, unfinished requests expire.
- Interactivity. You cannot stream tokens or react to a result in the middle of the job.
- Simplicity. You now maintain files, job status, and partial-failure handling.
Estimating the saving
The saving scales with token volume, so estimate it before you build anything. This script compares both modes. The prices are example values, so replace them with your model's current rates.
# Example prices in USD per 1M tokens. Replace with current rates.
SYNC_INPUT = 0.15
SYNC_OUTPUT = 0.60
BATCH_DISCOUNT = 0.5 # batch pays 50% of the synchronous price
def estimate(requests, input_tokens, output_tokens, retry_rate=0.0):
"""Return (sync_cost, batch_cost) in USD."""
total_in = requests * input_tokens / 1_000_000
total_out = requests * output_tokens / 1_000_000
sync_cost = total_in * SYNC_INPUT + total_out * SYNC_OUTPUT
# Resubmitted requests are paid for again.
batch_cost = sync_cost * BATCH_DISCOUNT * (1 + retry_rate)
return sync_cost, batch_cost
sync_cost, batch_cost = estimate(100_000, 400, 60, retry_rate=0.02)
print(f"sync: ${sync_cost:.2f}")
print(f"batch: ${batch_cost:.2f}")
print(f"saved: ${sync_cost - batch_cost:.2f}")For 100,000 requests of 400 input tokens and 60 output tokens, the synchronous cost is $9.60. The batch cost is $4.90 with a 2% retry allowance, so you save $4.70. At this scale the money matters less than the engineering time. At tens of millions of requests, or with an expensive model, the saving becomes large.
Deciding whether to wait
The decision comes down to one question: can anyone act on the result sooner than 24 hours from now?
Common mistakes
- Treating the window as an estimate. Jobs often finish early, but a schedule that depends on early completion will eventually break. Plan for the full window and treat early completion as a bonus.
- Ignoring retries in the budget. Failed or expired requests must be resubmitted, and the work you resubmit costs money again. Include a retry rate in your estimate, as the script above does.
- Batching tiny workloads. For a few hundred requests, the saving is cents and the extra code is a liability. A plain loop of synchronous calls is easier to debug.
- Skipping the pilot. A malformed request file can waste hours of waiting before you see the error. The pilot chapter covers how to avoid this.
- Mixing urgent and bulk work. Do not put time-sensitive requests in a batch to save money. Keep them synchronous and batch only the work that can wait.
Exercise
A nightly job classifies 2 million support tickets. Each request uses 300 input tokens and 20 output tokens. Results are read the next morning. A second feature classifies a ticket the moment a customer submits it, so an agent can see the category on screen. Which workload should use the batch API, and why?
Show answer
The nightly job should use batch. Nobody needs its results before morning, so the 24-hour window costs nothing, and at 2 million requests the roughly 50% discount adds up. The second feature has a user waiting on the result, so it needs synchronous calls despite the higher price. Batch turnaround is not guaranteed to be fast.
Exercise
Using the estimate function above, a job has 50,000 requests and a 10% retry rate. Does the retry rate cancel the batch discount? Explain in terms of the formula.
Show answer
No. Batch cost is synccost 0.5 (1 + retryrate), which is sync_cost * 0.55 at a 10% retry rate. The batch job is still 45% cheaper. The discount would only be cancelled if the retry rate reached 100%, which means every request was paid for twice.