AGENSPHERE/ JOURNAL
← JOURNAL
J-043NOTE3 MIN READ

Document extraction with LLMs needs a schema and a second check

LLM document processing turns invoices, contracts and forms into structured data, but a model that reads well still makes confident mistakes. A schema, validation rules, a second check and a review queue make the output trustworthy.

IN SHORT
  • LLM document processing works well for messy, varied documents that template-based OCR tools struggle with, but the model will sometimes return a confident, wrong value.
  • Make output trustworthy with four layers: a strict schema, deterministic validation rules, an independent second check on high-stakes fields and a human review queue for whatever fails.
  • Keep the source location of every extracted value, so a reviewer can verify it in one click.
  • Measure field-level accuracy on a labelled sample of your real documents before automating, and track the review rate after.

LLM document processing is one of the most practical uses of language models in business: reading invoices, purchase orders, contracts, claims and forms, and turning them into structured data a system can use. Models handle layout variety far better than older template tools. They also produce plausible, confident mistakes. The engineering is about catching those.

01Why use LLMs for document processing at all?

Traditional extraction relies on templates or trained layouts: it works well when every document looks the same and breaks when a new supplier sends a new format. A language model, often combined with OCR or a vision-capable model, reads documents the way a person does: it finds the total even when the label says "Amount payable" instead of "Total". That flexibility is the reason to use it.

02What goes wrong with LLM document extraction?

  • Confident errors. A date in the wrong format, a total taken from the wrong line, a figure from the previous page.
  • Invented values. A missing field filled with something plausible instead of left empty (see why LLMs hallucinate).
  • Silent drift. A prompt or model change that improves one document type and breaks another.

None of these show up in a demo with five clean PDFs.

03How do you make LLM document processing reliable?

Four layers, each cheap on its own.

1. A strict schema. Ask for exactly the fields you need, with types, and allow null for "not present".

extraction/invoice.py
from pydantic import BaseModel, Field
from datetime import date
from decimal import Decimal

class LineItem(BaseModel):
    description: str
    quantity: Decimal
    unit_price: Decimal
    amount: Decimal

class Invoice(BaseModel):
    supplier_name: str
    invoice_number: str
    invoice_date: date
    currency: str = Field(pattern=r"^[A-Z]{3}
quot;) subtotal: Decimal tax: Decimal | None # null when the document has no tax line total: Decimal line_items: list[LineItem] source_pages: dict[str, int] # field name -> page it was read from

2. Deterministic validation. Rules the model cannot argue with: line items sum to the subtotal, subtotal plus tax equals the total, the date is not in the future, the supplier exists in your vendor master. Failures are retried once with the error message, then routed to review (see validate structured outputs at the boundary).

3. A second check on fields that matter. For totals, bank details or contract dates, extract the field a second, independent way (a different prompt, a different model or a targeted regular expression on the OCR text) and require agreement.

4. A review queue. Anything that fails validation or disagrees goes to a person, with the document open at the right page and the field highlighted.

Automate the documents you can verify. Route the rest, quickly, to someone who can.

04How do you measure accuracy before you automate?

Label a sample of real documents, a few hundred across your document types and suppliers, with the correct values. Measure accuracy per field, not per document, because a 98 percent document score can hide a field that is wrong a third of the time. Then decide per field whether it can flow straight through or always needs review. Re-run the same set whenever the prompt or model changes (see ship a prompt change with an eval).

After launch, the number to watch is the review rate: the share of documents a person has to touch. It should fall as you fix the common failures.

05What about long documents such as contracts?

Contracts need retrieval as much as extraction. Split them along their own structure, clauses and schedules, rather than by token count (see chunk by structure, not token count), find the relevant clauses, then extract from those with the clause reference kept as the source.

06Where to go next

Document processing is often the first step of a larger workflow: approvals, payments, filing. See LLM application development for how we build those, or put the model inside the workflow for why the surrounding process matters.

Have this problem, but no system for it yet?DESCRIBE YOUR SYSTEM →