Get a quote on WhatsApp in 2 min
Contact

Mexico City, Mexico
Available for remote and on-site projects.

Professional networks

LinkedIn · GitHub · Email
Applied engineering, measurable results.

Building Production-Grade OCR+LLM Pipelines for Enterprise

· 5 min read · Samuel Torres

A production-grade OCR+LLM pipeline separates three jobs: OCR reads the pixels, the LLM turns messy text into structured fields, and plain code decides. I ran that split behind a promotion that managed a fixed pool of 80,000 folios, verifying purchase receipts with Tesseract and LLaMA 3.3 70B, unattended, every 2 minutes.

This article is about architecture and scale. I covered the Python side in Python automation for OCR + LLM and Python optimization for 80,000 folios, and the WordPress side in Optimizing WordPress for OCR + LLM in production.

Microservice Architecture for OCR+LLM

OCR+LLM pipeline: a document flow where OCR extracts text, an LLM structures it, and deterministic rules approve or reject it.

The order matters more than the models. OCR is cheap and dumb. The LLM is smart and non-deterministic. Rules are boring and auditable. Each one does only what it is good at.

A recent paper on running this in production, Operationalizing Document AI (arXiv:2605.18818), splits the system into independently deployable microservices on Kubernetes, with message queues distributing the work. Two of its findings match what I saw on a much smaller system:

  • OCR, not the language model, dominated end-to-end latency in their batch profiling.
  • Concurrency was capped by shared inference capacity, not by the number of workers.

The consequence: scale OCR and inference separately, and put a queue between them. This is the worker I would deploy for each document. It is a simplified example, not the production file:

def process(job):
    # Idempotency: the job key is the receipt hash, so a retry after a crash
    # never creates a second verdict for the same image.
    if verdicts.exists(job.sha256):
        return
    text = pytesseract.image_to_string(preprocess(job.image), lang="spa")
    # The model only proposes structured fields. It never approves anything.
    fields = llm.extract(text, schema=RECEIPT_SCHEMA, temperature=0)
    # Business rules live in code: testable, versioned, auditable.
    ok, reason = rules.validate(fields, promo=job.promo)
    status = "approved" if ok else ("review" if reason.uncertain else "rejected")
    verdicts.save(job.sha256, status, reason, fields)

Three states, not two. "Review" is the release valve: anything the rules can't settle goes to a human instead of being guessed.

Handling Enterprise-Scale Workloads

Throughput is how many documents finish per minute. The bottleneck is the slowest stage, and in OCR+LLM pipelines it is rarely the Python loop.

On the real system, the whole pipeline ran from cron every 2 minutes on an AlmaLinux VPS. The rule that kept it stable: a slow batch must never overlap the next one. flock solves that in one line.

⚠️ This touches a production server. Back up the crontab (crontab -l > crontab.bak) before editing, and keep the old line commented so you can roll back in seconds.

# /etc/cron.d/receipt-verifier — every 2 minutes, never two runs at once.
# flock -n exits immediately if the previous run still holds the lock:
# a slow LLM response delays ONE batch instead of doubling the load.
# timeout kills a hung run before the next slot, so the lock is always freed.
*/2 * * * * www-data flock -n /var/lock/receipt-verifier.lock \
  timeout 110 /usr/bin/php /var/www/tools/verify.php \
  >> /var/log/receipt-verifier.log 2>&1
# Roll back: comment the line above and run the previous script by hand once
# to drain anything left half-processed (the worker is idempotent).

When one VPS is no longer enough, I split OCR and LLM calls into separate containers, each with its own memory limit, so the OOM killer takes down one worker and not the website:

# docker-compose.yml — OCR and LLM scale independently.
services:
  ocr-worker:
    image: verifier-ocr:1.0
    deploy: { replicas: 4, resources: { limits: { memory: 1g } } }  # Tesseract is RAM-hungry on big images
    environment: [QUEUE_URL=redis://queue:6379/0]
  llm-worker:
    image: verifier-llm:1.0
    deploy: { replicas: 2 }        # bounded by the provider's rate limit, not by CPU
    environment: [QUEUE_URL=redis://queue:6379/1, LLM_TIMEOUT=20]
  queue:
    image: redis:7

Why two replicas for the LLM and four for OCR? Because the inference provider sets the ceiling. Adding LLM workers past the rate limit only adds retries.

Case Study: an 80,000-Folio Pool

For Panini Fan Zone FWC 2026, built as part of the Conqr Digital Agency team, I wrote two custom WordPress plugins in PHP. They issued and tracked a fixed pool of 80,000 folios with a QR code per registration, and verified purchase receipts with Tesseract OCR followed by a Groq-hosted LLaMA 3.3 70B. It ran unattended, with no one reviewing receipts one by one.

The hardest bug was not in the AI. The record-attempt registration needed "one person, one entry, per activity". The first uniqueness index checked email globally, so the same email could register twice under different activities. The fix was a composite unique index, with a guard that inspects the schema before altering it. The plugin went from v1.1.0 to v1.1.1.

⚠️ Schema changes in production: take a database dump first (mysqldump --single-transaction) and run the migration in a low-traffic window. Rollback is restoring that dump.

// Uniqueness scoped to the activity: same email + different activity = valid;
// same email + same activity = rejected by MySQL itself, even under a race.
global $wpdb;
$table = $wpdb->prefix . 'record_registrations';
// Guard: read the current schema first, so the migration is idempotent and
// safe to re-run on every plugin update.
$has = $wpdb->get_var("SHOW INDEX FROM $table WHERE Key_name = 'uniq_email_activity'");
if (!$has) {
    $wpdb->query("ALTER TABLE $table DROP INDEX uniq_email");
    $wpdb->query("ALTER TABLE $table ADD UNIQUE KEY uniq_email_activity (email, activity_id)");
}

A good index is still a hypothesis until it survives load. Before shipping, a standalone verifier and a 15-check suite included a concurrency test: 50 simultaneous workers hitting the same registration path. Exactly one registration succeeded per slot, with zero server errors. The module passed 24/24 verifier checks and 15/15 tests.

Implementation status panel: schema migration, fixed bugs and the 15/15 test suite, without participant data
Implementation status panel: schema migration, fixed bugs and the 15/15 test suite, without participant data

The error I see most in the industry: running OCR and the LLM call inside the web request that receives the upload. It works in the demo. Under a real campaign, every slow inference call holds a PHP worker, the pool fills up, and the site goes down for everyone, including people who were only browsing. Receive the file, queue it, answer "received", and process it out of band.

What I would NOT do

I don't let the LLM approve or reject anything. A language model can be confidently wrong, and "the model said so" is not an audit trail. The model extracts; rules written in code decide; anything uncertain goes to a person.

I don't start with Kubernetes for a single campaign. A cron job with flock on one VPS handled an 80,000-folio pool. I split into services when OCR and inference genuinely need to scale apart, not before, because every extra moving part is one more thing to monitor at 3 a.m.

Next step

Have a document flow that still depends on someone reading receipts by hand? Send me one anonymized sample and the rules it has to pass. I'll tell you which part belongs to OCR, which part to an LLM, and which part should stay a rule in code. Write to me. Get a quote on WhatsApp in 2 min

Samuel Torres Pichardo

Samuel Torres Pichardo
Software and cloud engineer in Mexico City, with about 3 years of professional experience.
About me · LinkedIn · GitHub · Contact