On 3 November I was about to resume a trial translation batch for a 9-locale site. Halfway through typing the command I opened the cost ledger and found it could be empty. That stopped the run.
A per-run budget cap holds only when the ledger is the single source of truth. A resume flag answers a different question: should this page be translated again. It says nothing about what the batch has already spent.
The site has 135 pages after exclusions, 15 pages in each of 9 locales. Machine translation of that scope costs cents, not thousands. The small amount is exactly why a wrong running total survives unnoticed. Nothing crashes. The overspend shows up in a monthly invoice.

Three ways the running total goes wrong
A different ledger starts from zero
The ledger lives in the trial output directory by default. The script also accepted --ledger pointing anywhere. In the first version, --skip-existing skipped the ledger guard entirely. The reasoning sounded fine: a page with a valid translation does not need to be paid for twice.
A translated file proves the page was processed. It does not prove what the batch cost. Point the ledger at a new path and the running total starts at zero, so every budget check passes. Empty the old ledger before a resume and you get the same result.
The fix ignores --skip-existing in the guard. If the output directory contains any result from a previous round — a translation, a check file, a needs_human note, or a review file — the run must use the ledger stored in that directory. That ledger must contain rows. Otherwise the script sends no request at all. This part needed a rework. The tests now cover three refusal paths.
Timeouts booked at zero
A timeout, a dropped connection, a 5xx, or a response that ends mid-stream all leave one possibility open: the provider may have generated and billed the call. The first version left cost_usd at 0 in the exception branch. Clean logs, wrong total.
The gap compounds. The ledger says $0.12 spent while the provider has already billed $0.20. The next call sees room under the cap and goes out. The overrun never raises an error.
The script now books the reserved worst case and sets pending_reconcile, then compares that row against the provider dashboard later. Only 4xx responses get a zero, because the provider rejects those before generation and does not bill them.
401 retried like a network blip
An expired key or a missing permission will not fix itself between retries. The first version treated 401 as a transient error and retried. In a batch, every page hit the same wall, the log filled with failures, and the run kept going until morning.
A 401 or 403 now stops the whole batch. Once the halted flag is set, no later call in that run is sent.
How the reserve is calculated
Every call reserves a worst case before it leaves the process.
| Item | Value | Note |
|---|---|---|
| Input tokens | (prompt + user) characters ÷ 2 | English runs about 4 characters per token, so this overestimates; Chinese can underestimate |
| Output tokens | max_tokens | 4096 for translation, 2000 for review |
| Input price | $1.32 per million at peak | deepseek-v4-pro, half price off peak |
| Output price | $3.96 per million at peak | cache hit $0.044 per million |
The check is one line. Spent + reserved + this call's worst case above the budget means stop, exit code 3. The default budget is $0.30, enough for a trial.
Dividing characters by 2 deliberately overestimates the input. It is a reservation, not a settlement. Settlement uses the usage block in the response. Chinese pages need a wider budget under this formula, or a one-page test first to read the real number.
How failures are booked
| Failure | Provider may bill | Booked as | Retry |
|---|---|---|---|
| 400 / 401 / 403 / 429 | No, rejected before generation | 0 | 401/403 stop the batch, others no retry |
| Timeout / dropped connection / 5xx | Yes | Worst case, pending reconciliation | No, this batch stops |
| Output cut off by max_tokens | Yes | Actual usage | No, change the limit and rerun |
| Review returns invalid JSON | Yes | Actual usage | No, goes to the rework list |
I keep this table next to the code. Every new failure branch gets two questions. Could the provider have billed it already. Could a retry possibly succeed. If both answers are unclear, the branch does not get a retry.
Protect the glossary too
The glossary is generated before translation starts. Writes go to a temporary file and replace the target atomically. Before an overwrite, the old file is copied to a timestamped folder under trash/. Short translations are checked against a length limit. Anything over the limit goes to a rework list instead of being silently truncated.
One wrong glossary term propagates through every page in the batch. It deserves more protection than any single translation.
What makes a ledger trustworthy
Three criteria. Miss one and the cap is decoration.
- The total accumulates across processes in one file, and a restart does not reset it.
- Every row has a cost basis, either the usage block or the worst case with a pending flag.
- A resume cannot switch ledgers. A missing ledger stops the run instead of starting from zero.
Five checks before a paid run
- One ledger path, no second copy
- Budget written into the command, not remembered
- 401 and 403 stop the batch instead of retrying
- Timeouts and 5xx booked at the worst case
- Output files and ledger rows reconciled before a resume
Who it fits / Limits
This applies to metered batch work: translation, review, bulk copy, where one run has a hard budget cap and a cost ledger can be the single source of truth. The $0.30 figure is a trial budget, not a quote for translating a whole site. Page count and locale count change the number.
Monthly plans do not need it. Providers that return no usage data need a separate reconciliation path, since the ledger cannot settle a call from a usage block. For Chinese pages the character÷2 formula can underestimate input, so run a one-page test first or allow a wider budget.
FAQ
What happens if I switch ledgers on a resume
The script refuses and sends no request. The running total would restart at zero, the budget cap would stop working, and the final cost would stay unknown.
If a translation times out with no response, is it still charged?
Yes — book the reserved worst case even when there is no response. Mark it for reconciliation. Booking zero understates the total, lets later calls through, and moves the overrun to the invoice.
Why stop the whole batch on a 401
A key or permission problem does not improve with repetition. Hitting the same wall on every page multiplies failures without producing a single translation. Replacing the key takes less time than a run that fails until morning.
Is the budget cap per site or per batch run?
Per batch run. One run takes one --budget. The ledger file carries the total across batches. Keep the same ledger and the batches stay connected.
How much does machine translation cost per page
Page length, locale, and model all move the number. There is no useful average. Run one real page, read the usage block, and multiply by page count. That beats any estimate.
Last updated: 2026-11-04