Automating Invoice Processing with Python and LLMs: A Production Walkthrough

Nov 17, 2026 15:00 · 1682 words · 8 minute read Python AI Automation LLM Django

Total Solution Industries runs a plumbing crew across several Florida cities. Every completed job used to be invoiced by hand: someone read the technician’s notes, looked at the photos, found the matching items in the regional price book, worked out the labour and typed the invoice into the company’s ERP, then filed it again on the platform the job came from and once more in QuickBooks.

Now every completed work order becomes an invoice draft automatically, about 530 a month. An LLM checks the work against the technician’s notes and before-and-after photos, Python computes every amount, and 44% of approved invoices approve themselves. The office reviews the rest, and one approval files the invoice in all three systems.

This post is how that is built, which parts the model does and which it is never allowed to touch, and what we would do the same way on any invoice-processing project.

The problem: free text and photos in, an exact invoice out

The inputs to every invoice are messy:

  • Technician notes, written on a phone at the end of a job, in the technician’s own words.
  • Before-and-after photos that show what was actually done.
  • A regional price book. The same task costs different amounts in different regions, and commercial work has its own list.
  • An estimate or proposal that may already have been approved for the job.

The output has to be exact: line items from the right price book, the right labour, and an invoice the customer pays without querying it, in three systems that must agree.

That is why invoicing is a good fit for an LLM and a dangerous one. Matching “replaced wax ring, reset toilet” and a photo of a new supply line to price-book items is a reading problem that rules handle badly. Setting the price is an arithmetic problem that language models handle badly. The whole design keeps the two apart.

Where it started, and why it moved

We built the first version in n8n in spring 2026: a webhook from the ERP, a model call to match items, a code step for the price, and a write back. It worked, and the business kept adding automations around it, until moving to Django retired 62 n8n workflows.

In September 2026 we moved invoicing, with payroll, scheduling and estimates, to a Django and Celery platform with an office panel. Cost wasn’t the reason. Invoicing had grown three writes that move money, a review queue, an auto-approval switch the client controls, and rules that needed tests. That logic belongs in code you can test and review. n8n still runs the business’s AI chat agents, which is what it is good at.

The flow today

  1. Intake. When a work order is marked completed in the ERP, the platform creates an invoice draft.
  2. Copy, don’t re-price. If an estimate or proposal was already approved for the job, its lines are copied. The customer already agreed to them.
  3. Read. Otherwise GPT-5 reads the technician’s notes and photos and chooses codes from the regional price book, citing the evidence for each one.
  4. Price. Python reads the labour and materials for every code from the price table, drops any code or evidence reference it cannot verify, and computes the total.
  5. Decide who approves. A draft that passes a fixed list of checks approves itself. Everything else waits in the office’s review queue on the panel.
  6. File. One approval writes the invoice to the ERP, files it on the work platform and books it in QuickBooks, confirming each by reading it back.

Step 3: the model chooses, and only from the list

The model gets the technician’s text, the photos and the price-book rows for one region. It must answer in a fixed shape: the codes it chose, and for each one the note or photo that supports it. In Python the contract looks like this:

from pydantic import BaseModel


class Pick(BaseModel):
    code: str             # must be a code from this region's price book
    quantity: int
    evidence: list[str]   # ids of the notes or photos that show this work


class Picks(BaseModel):
    picks: list[Pick]
    narrative: str        # the customer-facing description of the work


def verified(picks: Picks, price_book: dict, evidence_ids: set) -> list[Pick]:
    """Keep only codes that exist and evidence that was actually provided."""
    kept = []
    for pick in picks.picks:
        if pick.code not in price_book:
            continue                      # the model invented a code
        pick.evidence = [e for e in pick.evidence if e in evidence_ids]
        if pick.evidence:                 # no evidence, no charge
            kept.append(pick)
    return kept

Three rules matter more than the prompt wording:

  • The model never sees a target total. It is asked what work was done, not what the invoice should come to, so it can’t steer towards a number.
  • Only codes that exist, only evidence that exists. Anything the model returns that isn’t in the price book or the job’s notes and photos is dropped.
  • The narrative is checked too. The customer-facing description is rejected if it mentions money, an address or a name.

Step 4: code sets the price

The arithmetic is plain Python that anyone in the business can read and anyone can test against last month’s invoices:

from decimal import Decimal


def total(picks: list[Pick], price_book: dict) -> Decimal:
    lines = [price_book[p.code].labour + price_book[p.code].materials for p in picks]
    return sum((line * p.quantity for line, p in zip(lines, picks)), Decimal("0"))

If the client changes a price, they change the price book, not a prompt. If a rule changes, the tests show what else changed.

Step 5: auto-approval you can widen safely

A draft approves itself only if it passes every gate on a closed list: the office’s auto-approve switch is on, the check of the invoice against the photos is green, and the draft carries no note for a person, such as a return visit within 21 days or work that differs from the estimate. At first only drafts that copied an approved estimate qualified; in late September the client extended it to drafts priced from the visit, with extra gates on top. Two details made this safe to switch on:

  • It uses the office’s own Approve button. There is no second code path that could drift from the manual one.
  • It is throttled and checks first. It approves at most one draft every three minutes, because the work platform flags bursts, and it looks on the platform before filing, so it never files on top of an invoice someone already filed by hand.

The client switched auto-approval on in September 2026. In the first two weeks of October, 108 of 243 approved invoices (44%) approved themselves. Each time the gates are widened, the new rule is replayed over real production drafts first, so the office can see exactly which invoices would have gone out without them.

Step 6: three writes that move money

Approving an invoice writes to three systems that cannot simply be undone: the ERP, the work platform and QuickBooks. Each write follows the same rules:

  1. Claim before you write. The draft is marked as being filed in our database before the outside call, so two workers can’t file it twice.
  2. Write once. The submit is sent a single time. A write that moves money is never retried automatically.
  3. Decide success by reading back. We never trust an HTTP 200. Outside systems sometimes accept a request and then do something else, so success means the invoice is visible when we read the other system afterwards.
  4. Unconfirmed is a state, not an error. A run that sent but couldn’t confirm stays “unconfirmed”. A read-only sweep every ten minutes settles it, and nothing is resent until a person decides.

The first invoice filed on the work platform this way took 22 seconds end to end.

Lessons we’d apply to any invoice automation

1. Let the AI choose and the code count. The model reads notes and photos better than any rule. It should never produce the number on the invoice.

2. Don’t show the model the answer. If the model can see a target total, it will drift towards it. Ask it what work was done, not what the invoice should come to.

3. Copy what the customer already approved. An approved estimate is a contract. Re-pricing it invites a dispute.

4. Make every write safe to run twice. Claim, write once, read back. Webhooks fire twice and workers restart; invoices must not double.

5. Widen automation with replays, not hope. Auto-approval started narrow and grew by replaying real drafts against each new rule before switching it on.

6. Start in a workflow tool, move when the logic outgrows it. n8n got the first version live in weeks. Moving to Django was worth it once the process had rules, money and a review queue that needed tests.

Results

From production:

  • About 530 invoice drafts a month (532 in the 30 days to 22 September 2026).
  • 44% of approved invoices approved themselves in the first two weeks of October (108 of 243).
  • One approval files in three systems, each confirmed by reading it back.
  • In a read-only backtest, a work order took a median of 77 seconds to check and price.

The Total Solution Industries case study covers the rest of the platform, including residents booking their own visits and payroll built from van GPS.

Should you build or buy?

If your problem is reading supplier invoices that arrive as PDFs (accounts payable), there are good off-the-shelf OCR products, and you should try one first. This was the other direction: creating customer invoices from your own operational data, against your own price rules, in systems that no product integrates with well. That is where a custom build pays off, because the rules are the business.

The test we use: if the price depends on rules your team argues about, the rules should live in code you own, with an LLM doing only the reading.


We build Python automation services for businesses in the UK, EU and US, from a single workflow to a platform, and take responsibility for the systems we build. The full story of this project is in the field-service operations case study.