29.8 Realtime or batch
- Approved by
- Panaversity ·
In everyday life. Express mail costs more than standard mail. You pay for express only when someone is waiting. A shop that posts its paperwork at night uses standard mail, as long as it arrives before the office opens. If it might not, the shop keeps a courier ready for what is still on the way. In this section, express is a realtime request and standard mail is a batch. The office opening is the 7:10 run. The courier is the 6:45 rule.
What you will learn. You practice this in Lab 29, Task 3.
- What is a batch, and what does it cost?
- How do you read a batch's results?
- When do you use realtime, and when a batch?
- How does a two-request extraction run as a batch?
- What does the code do at 6:45?
Why it matters. Overnight work sent one request at a time pays full price for speed nobody needs. But a batch that ends after 7:10 misses the morning triage, and the triage reads no invoice that was not extracted.
At work. Tuesday, 11:00 a.m. You send the 16 extraction cases as one batch for 29.3's comparison. Then you plan the same move for the nightly extraction. Its binding constraint is the deadline. Every invoice's result must be ready before the 7:10 run (29.4).
What is a batch, and what does it cost?
A batch is many Messages API requests sent together and answered later, at half the price. Each request carries a custom_id that you choose. The service runs the requests when it has room. A batch costs 50% of the standard price, on input and output alike.1 On Sonnet 5.5 that is $1 per million input tokens and $5 per million output tokens.2 Section 24.10 gave the size limit: 100,000 requests or 256 MB in one batch.
You pay with time instead. Most batches finish within an hour. Results come only when the whole batch has ended, or after 24 hours, when every request not yet processed expires. Results are kept for 29 days.1 A batch's processing_status is in_progress until it is ended, and canceling while a cancel takes effect.1
Two more rules change how you build:1
- Errors arrive last. The API checks each request's parameters while the batch runs, and "returns validation errors when processing of the entire batch has ended". So send one request of the same shape on the Messages API first, and fix it before you send a thousand.
- Some settings are not allowed. A batch request takes no streaming, no fast mode
speed, and nofallbacks.
How do you read a batch's results?
Match each result to its request by custom_id, then act on its result type. The results file is JSON Lines, in any order. A custom_id is 1 to 64 letters, digits, hyphens or underscores, and it must be unique in the batch.1 Each result has one of four types, and only one of them is billed:1
| Result type | What it means | Billed | What Brightline's code does |
|---|---|---|---|
succeeded | The request ran | Yes | Send the answer through the three gates |
errored | The request was invalid, or the server failed | No | An error that passes, such as an overload: run it in realtime. Any other: needs_review, never retried blindly |
canceled | The batch was canceled before this request ran | No | Run it in realtime |
expired | The 24 hours ran out first | No | Run it in realtime, because before 7:10 there is no time for a new batch |
Which errors pass, and how to retry them, is the subject of Chapter 30. Here the code needs one sorting only: passing, or not.
When do you use realtime, and when a batch?
Realtime when someone waits, a batch when nobody does and the result is not needed within the hour.
| Choose | When | At Brightline |
|---|---|---|
| Realtime | A person, or a next step, is waiting for the answer | Ana's question in the portal |
| Batch | Nobody waits, and the result is needed hours later | The overnight extraction, the release suite (24.6), the trials of 29.3 |
A batch changes the price and the timing, not the text the model reads. So it is checked by its deadline, not by the suite.
How does a two-request extraction run as a batch?
As two rounds, with one row per invoice. A batch request is one call. The extraction's second request needs the model's get_invoice call from the first, and the tool result your code serves. So:
- Round 1, at 1:00 a.m. Request 1 goes for every new invoice, with ids like
INV-8340-r1. - When round 1 has ended. Your code runs
get_invoicefor each succeeded result, and sends round 2, request 2 for each, with ids likeINV-8340-r2.
The portal keeps one row for each invoice: the batch id, the custom_id, the round, and the outcome. By 7:10 every row has exactly one outcome. It is accepted, needs_review, or run in realtime.
Caching still applies inside a batch, and the batch discount applies to its prices too.2 But a hit inside a batch is "best-effort", typically between 30% and 98%.1 The page recommends the one-hour cache, written by one request first. Round 2 waits for round 1 to end, so a five-minute entry is likely gone by then. Write the tools and rules once with "ttl": "1h", with one request, then send round 1.
What does the code do at 6:45?
It reads each batch's status, cancels what has not ended, keeps what succeeded, and runs the rest in realtime. The rule, in order:
- Read each batch's
processing_status. - A batch that has not ended is canceled. The code waits for it to reach
ended. "Canceled batches end up with a status ofendedand might contain partial results."1 It keeps every succeeded result. - Each canceled or expired request runs in realtime. An invoice that finished round 1 sends only its request 2. An invoice that did not sends both. Neither batch request is billed.
- An errored request whose error passes, such as an overload, also runs in realtime. Any other errored request goes to
needs_review, with its error beside it for Ana.
The 25 minutes before 7:10 are for the realtime requests. Whether they are enough depends on how many requests are left on the day.
Figure 29.4. Two rounds from 1:00 a.m. At 6:45 the code ends what has not ended. Nothing waits past 7:10.
The operating rule: send work nobody waits for as a batch, and match every result by its custom_id. At 6:45, run whatever is left in realtime.
Check yourself
Question 1 / 7
0 answered
One request in a batch of a thousand has an invalid parameter. When does your code learn of it?
Sources
- Owner
- Panaversity