← Back to Blog

LLM or Rule-Based? Choosing a Document Parsing Approach, Guardrails, and Eval Design for AI Applications

2026-10-27Β·LLMAI EngineeringGuardrailEvalPDF Parsing

When it comes to extracting data from documents, we often assume the answer is "just hand it to an LLM."

This time, I deliberately did not.

I am building a personal finance system. It reads PDF statements from my brokers, extracts the holdings, and calculates cost and profit and loss (P&L).

The system already had an LLM parsing pipeline: send the PDF text to a model and ask it to return JSON in a specified format.

But for a new type of statement, I switched to rule-based parsing. All 100+ documents parsed successfully.

Based on that experience, this article looks at four topics from an AI engineering perspective:

If you are about to bring an LLM into production, I hope these lessons are useful.

Five Key Terms

1. When Not to Use an LLM

Before deciding whether to use an LLM, I ask three questions:

QuestionAnswer this time
Is the format fixed?Yes. The PDFs are system-generated; column order and decimal places are identical every period
Can the result be verified?Yes. Quantity, unit price, and amount on the document can be cross-checked
Is a mistake costly?Yes. Missing one holding makes the system treat it as "sold everything" and compute the wrong P&L

All three answers point the same way: this task needs determinism, and determinism is achievable here.

Here is how the two approaches compare:

LLM parsingRule-based parsing
Best forVaried formats with no fixed structureFixed, system-generated formats
ConsistencyThe same input may give different resultsThe same input always gives the same result
CostAPI fees and latency on every callAlmost no extra cost
When it failsHard to reproduce; usually needs human reviewReproducible, and can be turned into tests

The implementation is simple. When a system-generated PDF is read with pdf.js, each text item maps neatly to one table cell. Join them with | and match each row with a regex:

typescript
// Add a separator at the start and end of each page so the first row
// on a page still matches when a table spans pages
const pageText = '|' + items.map((i) => i.str.trim()).join('|') + '|\n';

This does not mean the LLM is useless. Other documents in the system with varied formats still go through the LLM, backed by human review.

πŸ’‘ Insight: An LLM is one tool, not the default choice. Asking "do I need an LLM here?" first can save API costs, human review, and a lot of debugging time.

2. The Trust Boundary of LLM Output

The new "cost" field is produced only by the rule-based parser, so it seemed to have nothing to do with the LLM pipeline.

In reality, the LLM's output could also include a field with the same name and carry it all the way into production data. There are two reasons:

Think of a web form: you never trust user input as-is. LLM output deserves the same treatment, because it is external input too.

The fix is a single line:

typescript
// cost may only come from the rule-based parser; drop any cost the LLM returns
delete holding.cost;

Plus a matching test: when LLM output includes cost, the stored value must be empty. If anyone removes that line later, the test fails.

This boils down to three principles:

3. Guardrails: Proving Nothing Was Missed

The most basic check in rule-based parsing is row-by-row validation: quantity Γ— unit price = amount. If it does not match, throw an error and log the failure.

But row-by-row validation has a blind spot: it proves the rows you parsed are correct, but it cannot prove you did not miss a row.

If a row is formatted slightly differently and the regex does not match it, there is no error β€” it just silently disappears. In this system, a missing holding is treated as "sold everything," producing the wrong P&L.

So we add a second layer: an overall check. Count the table's rows in a separate, looser way, then compare that number with the rows actually parsed.

typescript
// Row check: quantity Γ— unit price = amount
// Overall check: count the table's rows another way and compare
const expectedRows = countRowsLoosely(pageText);
if (expectedRows !== rows.length) {
  throw new Error(`Table has ${expectedRows} rows, but only ${rows.length} were parsed`);
}

There is a common trap here: the counting method must not rely on the same assumption as the main parsing logic.

For example, say both require each row to start with |. When a table spans pages, the second page starts with a line break, not |. Both sides miss that row, the counts still match, and the check is useless.

The fix is to add separators at the start and end of every page, plus a test case for "table spans pages, second page has no header."

The same idea applies to LLMs. If you ask an LLM to extract N items from a document, you need an LLM-independent way to know what N should be. Otherwise, if the model skips one, you will never notice.

πŸ’‘ Insight: The core of a Guardrail is a check that is independent of how the output was produced. Whether parsing was done by an LLM or by rules, if it does not add up, fail. Better to stop than to silently write bad data.

4. Eval: Regression Comparison on Real Data

Unit tests have a built-in limitation: the test cases come from the developer's own assumptions.

For example, you might assume an external data source has no data for a certain kind of event. In fact, it does β€” just under a different type. Unit tests will not catch this. Only real data will.

Real data is often not what you imagined.

So when changing calculation logic, pair it with a regression comparison tool:

An example diff table (contents are illustrative):

HoldingDifferenceExplanation
ALower costExtra shares from a stock split were previously treated as bought at market price
BLower costShares issued as a stock dividend were previously treated as bought at market price
CP&L changedCash returned by the company was previously treated as a sale

This is the Eval mindset in LLM applications: prepare a fixed dataset, run it before and after changing a prompt, model, or code, and look at what changed and why β€” not just whether anything errored.

⚠️ If a tool like this can also write to the database, protect it as strictly as production code. A regex over the connection string is not enough to decide "is this a local database?" β€” one misjudgment, and you write to production. A safer approach:

5. Checklist for Bringing LLMs into Production

Not just for PDF parsing β€” whenever you put an LLM into a production flow, run through this table:

CheckWhy it mattersHow
Do you need an LLM?When the format is fixed and determinism is required, an LLM adds riskAsk about format, verifiability, and the cost of mistakes
Fields in LLM outputExtra fields can flow all the way into production dataStrip fields that should not be there; write important fields only from trusted sources
Missing dataRow-by-row checks cannot prove nothing was missedAdd an overall check whose counting does not share assumptions with the main logic
Before/after differencesUnit tests only cover cases you thought ofRun Eval on the same real data; every difference must be explainable
Tools that write dataOne misjudgment can write to productionParse connection settings + allowlist + self-test

πŸ’‘ Takeaways

LLMs are powerful, but in production systems that demand correctness, being verifiable often matters more than how capable the model is.

A reliable AI application depends not only on what the model can do, but on whether the system can confirm it did it right.

Ask two questions: "Do I need an LLM here?" and "How will I verify its output?" Once you can answer both, the LLM is ready for production.

I hope this was helpful!


← Back to BlogNext: LLM ι‚„ζ˜―θ¦ε‰‡εΌοΌŸAI ζ‡‰η”¨ηš„ζ–‡δ»Άθ§£ζžιΈεž‹γ€Guardra… β†’