Yeda AI Tips · #128

Español

Regex First, Model for the Edges

Do not send every line of structured text to a language model. Regex handles most of it at a fraction of the cost. For consistent-format text — forms, quizzes, invoices, logs — a well-tuned set of patterns resolves roughly 95–98% of cases deterministically, and teams commonly report order-of-magnitude (~20×) lower per-record cost than an LLM-only pipeline. The model earns its keep on the 2–5% that regex can't handle.

Why LLM-on-everything is the wrong default

An LLM call for a field you could match with \d{2}/\d{2}/\d{4} buys you three downsides at once:

The hybrid pipeline

Parse with regex first. Score each extraction for confidence. Send only the low-confidence ones to the model. Everything else ships straight from the regex.

import re

AMOUNT = re.compile(r"(?i)total[:\s]*\$?(\d{1,3}(?:,\d{3})*(?:\.\d{2})?)")

def extract(line: str) -> dict:
    m = AMOUNT.search(line)
    if m:
        return {"amount": m.group(1), "confidence": 1.0, "source": "regex"}
    # No match, ambiguous match, or failed validation -> queue for the LLM
    return {"amount": None, "confidence": 0.0, "source": "llm_queue"}

results = [extract(line) for line in lines]
edge_cases = [r for r in results if r["confidence"] < 0.8]
# Only edge_cases go to the model — typically ~2–5% of records.

For the LLM leg, ask for a JSON-schema-constrained response (structured outputs) so the fixed edge cases merge back into the same shape as your regex output.

Rules of thumb

SituationRoute
Format repeats consistently (forms, invoices, logs)Regex, always
Field matched and passes validation (checksum, range, date parses)Ship it — no model call
No match, multiple conflicting matches, or validation failsSend to the LLM
Free-form prose, no repeating structureLLM from the start — regex isn't the tool
Latency-tolerant edge casesBatch them: OpenAI's Batch API is 50% cheaper, ≤24h turnaround

Power-user moves

Resources

Building an AI feature? Yeda AI designs, audits, and ships production LLM systems.

Talk to us · Read the blog