Skip to content

    AI Architect Case Files

    Case: Classifying 2 Million Documents, Batch and Cascade

    A public body priced real-time model calls for 2 million documents at 40,400 units. Rules, a batch cascade and human sampling cut it to about 5,250.

    By Published 7 min read

    • ai-architect
    • case-file
    • industry-public-sector
    • pattern-batch-classification
    • failure-cost-overrun
    • kind-composite
    Share

    Composite scenario, not a real company. Assembled from common industry patterns; every number is illustrative and its assumptions are listed at the end.

    This case belongs to Models, routing and inference for AI Architects.

    Context

    • A public body must sort 2,000,000 text documents (letters, forms, reports) into 20 categories, so each reaches the right records team and retention schedule, within 30 days.
    • The first design called the largest approved model once per document, as if each were a customer waiting.

    Problem statement

    At 1,900 input and 30 output tokens per document, real-time calls cost 40,400 cost units (cu) against a 6,000 cu budget. Speed was never the issue: 2,000,000 documents in 30 days is 0.77 a second.

    Goal Target
    Model cost 6,000 cu or less
    Time Backlog done in 30 days
    Human load Fewer than 2% of documents sent to humans
    Quality Machine-accepted error rate: 95% interval upper bound at 5% or less

    Constraints

    • Personal data goes only to the approved provider and region.
    • Every decision must be traceable: document id, category, tier, confidence and prompt version.
    • A person decides any document the machine is unsure about.
    • Categories are fixed at 20; a new one needs a policy decision, not a prompt edit.

    Why the traditional approach fell short

    Approach Where it breaks
    Rules on metadata Work for form-coded documents, about 35% of the backlog; that part stays rules
    Keyword rules Break on free-text letters: the same topic in different words, boilerplate shared across categories
    Classic ML classifier Needs thousands of labelled examples per category, and retraining when categories change
    Manual sorting 3 minutes a document is 100,000 hours

    Options considered

    Option Verdict Reason
    A. Rules for form-coded documents, people for the rest (non-AI) Rejected as the whole plan The remaining 1,300,000 documents at 3 minutes each are 65,000 hours
    B. Real-time call to the large model per document Rejected 40,400 cu, nothing in the process needs an instant answer
    C. Rules, then a batch cascade (small model, then large for uncertain documents), then a human queue, with weekly sampling Accepted Large tier only for hard documents, asynchronous pricing

    Solution architecture

    Figure 1. Components of the batch classification pipeline

    Batch classification pipeline componentsDocument store to Orchestrator; Orchestrator to Worker; Worker to Model gateway (batches); Model gateway to LLM provider; Model gateway to LLM provider; Worker to Approval service (human review); Worker to Trace store; Trace store to Eval service (sample)Document storeDocument storeOrchestrator: rules by form codeOrchestratorrules by formcodeWorker: batch builderWorkerbatch builderModel gatewayModel gatewayLLM provider: small tierLLM providersmall tierLLM provider: large tierLLM providerlarge tierApproval serviceApprovalserviceTrace storeTrace storeEval service: weekly sampleEval serviceweekly samplebatcheshuman reviewsample

    How to read this: documents flow left to right and get more expensive attention only when the cheaper stage is unsure.

    Figure 2. One batch, with expiry and the human path

    sequenceDiagram accTitle: Runtime path for a classification batch accDescr: The Worker builds a batch of up to 50,000 requests keyed by document id and submits it through the Model gateway to the small tier. It polls until the batch ends, then maps results by document id. Confident results are stored. Uncertain ones go to a second batch on the large tier. Results still uncertain go to the human queue. Expired requests are resubmitted once. autonumber participant W as Worker participant M as Model gateway participant L as LLM provider participant D as Document store participant A as Approval service W->>M: batch of up to 50,000 requests, custom id is the document id M->>L: submit batch to the small tier loop until the batch has ended W->>M: poll status M-->>W: in progress end M-->>W: results in any order Note over W: Map results by document id, not by position loop each result alt confident and valid category W->>D: write category, tier, prompt version else uncertain W->>M: add to second batch for the large tier else expired or errored W->>M: resubmit once with the same id end end Note over W: Large-tier results still below the threshold go to people W->>A: documents for human review

    How to read this: the loop at the top is waiting, the second loop sorts each result into one of three paths, and the document id ties every step together.

    How AI solved it, step by step

    Step through the pipeline, then try "Without the fix".

    Classifying the backlog in batches (interactive when JavaScript is on)

    As designed

    1. Rules first. The Orchestrator classifies the 700,000 form-coded documents by rule. No model is involved.
    2. Small-tier batches. The Worker packs the other 1,300,000 into 26 batches of 50,000 requests (label definitions plus text) for the small tier.
    3. Accept the confident. Each result has a category and a confidence score. At or above the threshold with a valid category, it is accepted: 910,000 documents. This is the FrugalGPT cascade: a scoring function rates an answer, and it is returned if the score is above a threshold, otherwise the next model is queried.
    4. Large-tier batches. The 390,000 uncertain documents go to the large tier in 8 batches.
    5. Humans for the rest. Large-tier results below the threshold, or without a valid category (8%, 31,200 documents), go to the human queue.
    6. Weekly sample. Every week the Eval service draws 400 accepted documents, and reviewers mark each right or wrong.
    7. Interval gate. The result is a 95% Wilson interval for the error rate. If its upper bound is above 5%, the small-tier threshold is raised and the next week's uncertain share grows.

    Without the fix

    1. One queue. Each of the 2,000,000 documents is sent to the largest model on its own, as if a customer were waiting.
    2. Full price. Every document costs 0.0202 cu, with no rules, no small tier and no batch pricing.
    3. Over budget. The total is 40,400 cu against a 6,000 cu budget, for a job that only needs 0.77 documents a second.

    Under the hood

    Two things are called batching. The API batch used here is a queue processed asynchronously. As of 2026-10-22, the Anthropic and OpenAI batch documentation both state a 50% discount against synchronous calls, a 24-hour maximum window, results in any order (match by custom id) and per-batch limits that differ (the plan uses the smaller 50,000). Neither explains how the discount is produced, so do not assume.

    Inside a model server, batching is a different mechanism (Under the hood: Continuous batching and paged KV memory). The Orca paper schedules per model iteration, not per request, so finished requests leave and new ones join. The PagedAttention paper observes that key-value cache memory limits how many requests fit, and that earlier systems stored only 20.4% to 38.2% of it as real token state.

    The paper's 13-billion-parameter model Value
    Cache per token 800 KB
    One 1,900-token document 1,556,480,000 bytes, about 1.56 GB
    Cache space on a 40 GB card (about 30%) about 12 GB
    Whole documents at once 7 (7.7 by division)

    This is the paper's model, not the provider's, but it shows why server batching matters at 2 million documents.

    A small call costs 0.1 of a large one, and 30% of the 1,300,000 model-path documents escalate. Set escalation to 0.5.

    Small-then-large cascade per document (cu, synchronous prices) (interactive when JavaScript is on)
    Cascade cost per request0.0081
    Always-large cost per request0.0202
    Break-even escalation rate90.0%

    At 0.3 the cascade costs 0.00808 cu a document: 10,504 cu for 1,300,000, or 5,252 cu at batch prices. At 0.5 it is 0.01212 cu, 7,878 cu at batch prices, over budget.

    What went wrong / failure modes

    Failure What happens Mitigation
    Miscalibrated confidence The small tier reports 0.95 on wrong answers Weekly Wilson-interval gate, per category, and a threshold that moves
    Expired or failed requests Unfinished requests expire at the window's end Resubmit once by document id; writes are keyed by id and prompt version, so a retry cannot duplicate
    Results matched by position Output order may differ from input Join on custom id only
    Uncertain share grows At 50% uncertain instead of 30%, cost is 7,878 cu, over budget Alarm on the uncertain share; review label definitions first
    Prompt edited mid-run Two prompt versions in one backlog Record the version on every decision and re-run a sample when it changes

    Results

    These are illustrative targets, not measurements.

    Design Model cost (cu) Share of real-time cost Documents to humans
    Real-time, large tier for all 40,400 100% Not designed
    Rules, cascade, synchronous prices 10,504 26% 31,200 (1.56%)
    Rules, cascade, batch prices 5,252 13% 31,200 (1.56%)

    The batch cascade meets the 6,000 cu budget and the 2% human share; review is 1,560 hours (31,200 documents at 3 minutes).

    Weekly sample Errors Rate 95% interval Gate (upper bound 5% or less)
    First 12 of 400 3.0% 1.72% to 5.17% Fails: threshold raised
    Later 8 of 400 2.0% 1.02% to 3.90% Passes

    Tradeoffs and when NOT to use this approach

    • Batch results arrive hours later: not for a person waiting.
    • The cascade runs two passes on uncertain documents; it pays only when the small tier handles most.
    • Where a wrong category has legal effect, sample more before auto-accepting.
    • At small volumes the pipeline may cost more than it saves.
    • Practitioners differ on fine-tuning a small model instead; that needs labelled data this body lacks.

    Design rules extracted

    1. Ask whether anyone is waiting. If not, use asynchronous batch pricing.
    2. Send deterministic cases to rules before any model.
    3. Cascade by confidence, and calibrate the threshold against a human-labelled sample.
    4. Join batch results by id and make every write idempotent.
    5. Gate on an interval for the error rate, not on a single percentage.

    Assumptions

    Prices are invented cost units per million tokens: large 10 input and 40 output, small 1 and 4. Batch discount 50% (the documented figure, as of 2026-10-22, used as a ratio).

    • 2,000,000 documents; 1,900 input tokens and 30 output tokens each. Large per document (1,900 x 10 + 30 x 40) / 1,000,000 = 0.0202 cu; small (1,900 x 1 + 30 x 4) / 1,000,000 = 0.00202 cu. Real-time large: 2,000,000 x 0.0202 = 40,400 cu. Rate: 2,000,000 / (30 x 86,400) = 0.77 per second.
    • 35% rules: 700,000; model path 1,300,000. Small tier accepts 70%: 910,000; uncertain 390,000. Large tier leaves 8% for humans: 31,200 (1.56%), at 3 minutes = 1,560 hours. Manual: 2,000,000 x 3 / 60 = 100,000 hours; 1,300,000 x 3 / 60 = 65,000 hours.
    • Batch cost: 1,300,000 x 0.00202 x 0.5 = 1,313; 390,000 x 0.0202 x 0.5 = 3,939; total 5,252; without discount 10,504. If 50% are uncertain: 650,000 x 0.0202 x 0.5 = 6,565 plus 1,313 = 7,878. Per document, cascade = 0.00202 + 0.3 x 0.0202 = 0.00808 cu; at 0.5, 0.01212.
    • Batches: 1,300,000 / 50,000 = 26; 390,000 / 50,000 = 7.8, so 8.
    • Wilson interval at z = 1.96 (the NIST e-Handbook's recommended interval for a proportion); error counts are invented.
    • Cache: 2 x 5,120 x 40 x 2 bytes = 819,200 per token; x 1,900 = 1,556,480,000; 12,000,000,000 / 1,556,480,000 = 7.7.
    • The agency and its documents are invented.

    Last verified: 2026-10-22

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments