Writing

How I got a document pipeline to handle 10x the documents per hour

Most of the documents a business receives are read twice. Once by a person who opens the file, and once more when that person types what they saw into some other system. The pipeline in this post exists to remove the second reading, and most of the first.

I built it in my job as an AI data engineer. It takes in test reports for printed circuit boards, reads them with OCR, attaches the right data label to every record, stores the results and generates the reports people actually look at. When it was finished, it handled ten times as many test documents per hour as before. I can’t name the customers or show their data, so everything below is the general architecture in my own words, with no customer data, names or production code. The code sketch is a generic example written for this post.

What the documents look like

A circuit-board test report is a dense, mostly tabular document. There is a header with identifiers (board, batch, station, date), then pages of measurements, each with a value, a unit and a pass or fail against a limit. Reports like this rarely arrive in one tidy format.

Two things make them awkward. The layouts vary by source, so a template that reads one station’s output breaks on the next. And the numbers matter: a misread decimal point turns into a wrong answer about whether a part works.

The second point shaped the design more than the first. A pipeline like this has to be fast, but it also has to know when it isn’t sure, and say so.

The shape of the pipeline

The whole system is six steps, each a separate piece that does one job and hands its result to the next.

  1. A report arrives and a webhook tells the pipeline it exists.
  2. The webhook handler puts a message on a queue, and an Azure Function picks up that one file from it.
  3. Azure AI’s OCR service reads every page and returns text, tables and positions.
  4. A classification model assigns each extracted record a data label from the company’s own label set.
  5. The labeled records are written to Azure SQL.
  6. Python code builds the test reports from the database.

Next to those six steps sits a set of retention rules that decide what stays in the database and what moves out, which I’ll come back to because it turned out to matter for cost more than for speed.

Why it got faster

The 10x figure is throughput: documents processed per hour. Most of it came from changing what waits for what.

Each document starts its own run

The simplest way to process files is to check a folder every so often and work through whatever is there. That design has a hidden cost: a file that arrives one minute after the check waits for the next one, and a big batch makes everything behind it wait in line.

With webhook ingestion, each document announces itself when it arrives and starts its own run. Nothing waits for a timer, and nothing runs when there is no work.1 On Azure’s consumption plan, functions are billed per execution, which is part of why an event-driven design tends to be cheaper as well as faster.

Each file is its own small job

Azure Functions run many copies of the same small function side by side. Because every document is processed by its own function call, twenty documents arriving together become twenty runs at once rather than one run that takes twenty times as long.

This only works if each step is independent. A function that reads a document should not need to know what happened to the previous document. Keeping the steps that small took some discipline, and it is the single biggest reason the throughput number moved.

OCR is the slow part, so everything else gets out of its way

Reading a page with OCR takes far longer than any other step. Classification, writing to the database and building a report are quick by comparison. So the design question becomes: is anything making the OCR call wait, or making the next document wait for the OCR call?

Here is a simplified sketch of the per-file pattern. The webhook’s only job is to drop a message on a queue; this function is triggered by that message. Production code has more error handling and logging, and the helper functions are left out.

import azure.functions as func

app = func.FunctionApp()

@app.queue_trigger(arg_name="msg",
                   queue_name="incoming-reports",
                   connection="Storage")
def process_report(msg: func.QueueMessage) -> None:
    job = msg.get_json()  # {"blob_url": ..., "source": ...}

    # 1. Read the document. This is the slow call.
    result = read_with_ocr(job["blob_url"])

    # 2. Turn tables into records and label each one.
    records = [label(r) for r in extract_records(result)]

    # 3. Unsure records go to a review queue for a person.
    sure = [r for r in records if r.confidence >= THRESHOLD]
    unsure = [r for r in records if r.confidence < THRESHOLD]

    save_records(sure, source=job["source"])
    if unsure:
        queue_for_review(unsure, blob_url=job["blob_url"])

The queue in front of the function matters as much as the function. If a burst of reports arrives, they line up in the queue and get processed as fast as the OCR service allows, instead of overwhelming it and failing.

Labels at the door

Every record that comes out of a test report belongs to a category, and the reports downstream are built by category. Those labels are specific to the business, so no off-the-shelf model knows them.

I built classification models and ingestion logic that assign the label as the record comes in. It means the database never holds unlabeled rows, so report code never has to guess, and nobody has to sort records by hand before a report can run.

The part I would stress to anyone building something similar is the confidence threshold in the sketch above. A model that is right most of the time is only useful if the cases where it is unsure go somewhere a person will see them.2 In practice, “a person will see them” means a short queue with the original page next to the extracted values, so the reviewer can confirm or correct in a few seconds. A wrong label that nobody flags can sit in a report for months.

The cost side: deciding what to keep

Faster ingestion has a side effect: you store a lot more, a lot faster. Raw files, OCR output, intermediate results and final records all pile up, and cloud storage and database costs grow with them.

So the pipeline includes retention workflows. Each kind of data has a rule for how long it stays in the database and where it goes after that. Final records stay where the reports can reach them. Bulky intermediate output that nobody queries is moved to cheaper storage or removed once it has served its purpose. Webhook ingestion helps here too, because the pipeline only touches what actually arrived instead of scanning storage on a schedule.

With rules like these, the monthly bill tracks what the reports actually use instead of every file that ever arrived.

What a small business can borrow

A twenty-person company does not receive thousands of circuit-board test reports. It receives supplier invoices, purchase orders, signed forms, delivery notes, insurance certificates and inquiry emails. The volume is smaller. The structure of the problem is identical: something arrives, a person reads it, a person types what it says into another system.

The same four ideas carry over at a fraction of the size and cost.

Let the document start the work

An invoice that arrives by email can trigger its own processing within seconds. You don’t need a daily batch.

Keep each step small

“Read it”, “decide what it is”, “put it in QuickBooks” and “tell someone” are separate steps. When one breaks, you know which.

Send the unsure cases to a person

People stay in the process. They stop reading the ninety documents that are fine and look carefully at the ten that aren’t.

Decide what to keep

Even a small system builds up files. Writing the retention rule on day one takes ten minutes, and it saves the afternoon you would otherwise spend after a surprise bill.

What I would do the same way again

If I were starting over, I would keep the same four choices: one small function per document, a queue in front of the slow step, labels assigned at the door, and a written rule for every kind of stored data. Those choices produced most of the 10x.

The thing I would add on day one of any pipeline like this is a simple count of uncertain records: how many the model was unsure about, by source, by day. That number tells you where the documents are getting harder to read, usually before anyone complains.

If you have a pile of documents that someone in your business retypes every week, I’d like to hear about it. The pipeline above is bigger than what most small businesses need, and the smaller version is quicker and cheaper to build.