Skip to content
Ashraful Islam
Menu

Backend software engineer · Kuala Lumpur

All notes

Note · 30 November 2026 · 6 min

Walls around a model

A model will return a confident number that does not add up. The engineering is everything you put between that number and the database.

LLM · validation · failure modes

Two of my products call language models in production. Neither of them trusts the output, and that is the only reason either one is safe to leave running.

The instinct when wiring a model is to treat it as a function: input in, JSON out. It is closer to a remote colleague who is fast, knowledgeable, occasionally wrong, and impossible to ask for clarification in the middle of a request. The design work is the same as for any unreliable dependency — bounded inputs, validated outputs, a fallback, and a record of what happened.

Rule one: the model does not decide

The invoice service extracts line items, a total and a vendor, and then six deterministic rules decide whether the invoice is acceptable: lines sum to the total, totals are positive, each line is sane, dates fall inside a window, the currency is on an allowlist, and the vendor matches the document.

None of those are questions for a model. They are arithmetic and set membership, they run in microseconds, and they return a named reason a human can act on. Putting them in a prompt makes them probabilistic and unauditable — the worst combination for a financial check.

A rule as a pure function: no binding, no network, one test
def line_sum_matches_total(inv: Invoice) -> Check:
    total = sum(line.amount for line in inv.lines)
    if abs(total - inv.total) > Decimal("0.01"):
        return Check.fail("line_sum", f"lines={total} total={inv.total}")
    return Check.ok("line_sum")

Rule two: parse defensively, because the wrapper lies

The first extraction parser used a regex from the first curly brace to the last. It worked on every clean response and corrupted the first one that came back wrapped in prose with two objects in it — the regex matched from the opening of the wrapper to the closing of the payload.

The replacement walks the string and tracks brace depth, ignoring braces inside strings, and returns the first balanced object that contains the key it is looking for. It is about ten lines and it fails correctly: no payload means an error, not a plausible wrong object.

Rule three: assume the provider disappears

Models get retired. The failure is not gradual — one day every request fails at once, with a model-not-found that your retry loop happily retries forever. A fallback chain across two models turns that into a latency event instead of an outage.

The second model is worse at the job. That is the real cost: quality is not constant, so the system has to record which model produced each extraction and let a human see it. A fallback that hides itself is a silent downgrade.

There is a subtler variant: a reasoning model that spends its whole token budget thinking and returns an empty content field. Treated as a provider failure, it looks like an outage. Handled properly, it is a different response shape with the answer in another field — and the code has to know that before the first long invoice arrives.

Rule four: bound what the output is allowed to be

The telehealth platform generates summaries and trend analyses, and the model is not allowed to produce a diagnosis, a dose, or anything that writes back into the clinical record. It returns a structured artifact — one of four types, with sources attached and a non-diagnostic flag — and the application renders it with a disclaimer and a review note.

This is narrower than a chatbot and it feels less impressive in a demo. It is also the only version I would put in front of a patient: a bounded artifact can be validated, reviewed and traced to its inputs. Free text cannot.

The general shape

Validate the input before the call, validate the shape after it, decide with code rather than with prose, fall back to a second model rather than a second retry, and write down which model answered.

None of that is specific to language models. It is what you would do with any third-party service that is fast, cheap, and occasionally wrong — which is most of them.