A cut-off batch was paid for three times. Fixed the next day.

An open book with a neatly stitched tear down one page, with stacked coins and a small wrench lying on it.

Retry loop Fixed upstream

A book translator that is already very cache-aware: session mode, Anthropic cache marks, cached-token metering.

≈ 58,000 tokens per 1,000 calls, for a cut-off batch sent twice more, before the fix estimate

Estimates, not measurements. Every figure on this page is an estimate from reading the code, not a measurement of the app in use.

Repository
yihong0618/bilingual_book_maker
Commit audited
5e40cbc
leanprompt version
0.1.0-dev.15
Audit date
Status
The maintainers have changed the code since this audit.
Maintainers
the pull request

A book translator that is already very cache-aware: session mode, Anthropic cache marks, cached-token metering, the batch APIs. A retry of a cut-off batch was fixed in pull request #590 (merged 2026-11-27); a question about stacked timeouts and an unchecked cut-off reply remain. Audited with leanprompt 0.1.0-dev.11, re-scored with 0.1.0-dev.13.

leanprompt read the code at commit 5e40cbc and found 1 place where its LLM calls could use fewer tokens, and 1 already fixed, plus 1 other problem, not scored. Static score: 99 of 100 (an estimate from the findings). The code may have changed since.

Findings

  1. LP-GE-04 Stream long outputs; timeouts that match generation time

    book_maker/translator/chatgptapi_translator.py:341 · severity high · saves prefill and generation · needs a fact to confirm

    What happens. The 300-second timeout with one SDK retry (line 341) is wrapped by a 3-attempt tenacity retry on the batch path (line 1807) and, after the batch gives up, by a per-paragraph sweep in which each paragraph has its own 3-attempt retry (line 1513). A batch that times out is therefore attempted up to 6 times, about 30 minutes, which is the stall the comment above REQUEST_LIMITS set out to prevent, and each abandoned attempt may still be generated and billed.

    The change. Keep one retry layer for timeouts: either set max_retries to 0 on this client and let tenacity own retries, or exclude APITimeoutError from the tenacity retry on the batch path and let the existing one-by-one fallback (or the ladder) handle it. Non-streaming requests can also use streaming so a slow but working answer is not mistaken for a dead one.

    Why it helps. A client timeout stops the client waiting; it does not stop the server generating. Each retry after a timeout pays the prompt again while the abandoned answer may still be billed, and the same request times out the same way.

    Estimate about 18k output and 17k prompt tokens per 1,000 calls (estimate, if timeouts happen at all)

    Assumptions. 0.2% of grouped requests exceed 300 seconds; each produces 5 extra attempts of about 1.7k prompt and 1.8k output tokens, billed although abandoned. If the timeouts come from a gateway that never answers, no tokens are lost and the cost is only wall time.

    To confirm. Whether any grouped request on the endpoints people use (reasoning models at high effort, slow local servers, gateways) takes longer than 300 seconds, and whether the endpoint keeps generating and billing after the client gives up. A run log with request durations, or the provider's usage page against the run's own meter, shows it.

Already fixed

  1. LP-GE-05 Retry only retryable errors; never retry a truncation unchanged

    book_maker/translator/chatgptapi_translator.py:1811 · severity high · saves prefill and generation · how often is assumed

    What happens. When a grouped structured translation is cut off at the output limit, the OpenAI SDK's parse() raises LengthFinishReasonError. The retry decorator on _execute_structured_batch_translate retries every exception except three, and this one is not among them, so the same cut-off request is sent twice more unchanged, each time paying the full prompt and a full output limit, before the code falls back to translating the paragraphs one by one.

    The change. Fixed after this audit by pull request #590, merged 2026-11-27: a truncated batch is treated as a mismatch, not a transport failure. LengthFinishReasonError and ContentFilterFinishReasonError are caught around the parse call, e.completion is metered as on the single-paragraph path, and BatchMismatch is raised, which the retry skips and the plan-mode ladder answers by halving the chunk.

    Why it helps. A request cut off at the output limit will be cut off again: the prompt, the model and the limit are the same. The two identical retries add cost without a usable answer, and they are the most expensive calls of the run, because each one generates up to the output limit.

    Estimate about 41k output and 17k prompt tokens per 1,000 calls (estimate)

    Assumptions. 1 in 200 grouped requests truncates (endpoints with a low default output limit, or reasoning models whose thinking counts toward it); each repeated retry costs about 1.7k prompt tokens and about 4.1k output tokens (a 4,096-token limit); 2 repeated retries per truncation; grouped requests of 1,200-1,600 source tokens (SESSION_BUDGET_FLOOR/CEILING) with about 1.8k output on average. The first truncated call is not counted: it is how the truncation is found.

    How often. how often a grouped request stops with finish_reason length, from a run log of one book

Other problems the audit found

Problems read in the code that cost no tokens of their own, such as a cost meter or an error path. Not scored.

  1. A cut-off plain translation is accepted silently

    book_maker/translator/chatgptapi_translator.py:1366 · LP-GE-12 Read the finish reason before using an answer · severity high

    What happens. _plain_translation returns message.content without looking at finish_reason. After a structured single-paragraph truncation the code retranslates on the plain path (line 1529) with the same output limit, so a paragraph that did not fit can be written into the book half translated, with no warning.

    The change. Check finish_reason == "length" in _plain_translation and print a warning or mark the paragraph, or retry it once with a larger limit.

    To check. Translate one paragraph with a deliberately small output limit: the run prints a warning instead of writing a truncated paragraph.

The maintainers’ response

No response from the maintainers yet.

How this was made

The llm-call-audit skill read the code and wrote findings.json; nothing was run against the project’s providers. The full audit page is what leanprompt report --html writes from the same findings.

Corrections and takedown: email info@podwarden.com or open an issue.