All notes

· 4 min read

Treat the model's answer like a form post

A language model turns a pasted list of cars into catalogue ids, and not one of them is trusted. Every id is checked against the real catalogue, the status is re-derived from what survived, and the capacity is read again at the moment the rows are written.

The feature

A SaaS I run lets people record the vehicles they own, picked out of a catalogue of a few hundred. Adding them one at a time is the thing that stops people before they get anything useful back, so in November I built an import: you paste your list in, in whatever form you already have it, and the cars get matched to the catalogue and parked in your garage.

A language model does the matching, because that part is genuinely hard. People write "the new elegy", or "banshee 900", or "2x zentorno". Fuzzy string matching gets some of those and quietly gets others wrong, and quietly wrong is the worst outcome here, because what comes out of this is rows in someone's portfolio.

So the model gets the pasted lines and the catalogue, and hands back something like this:

{
  "items": [
    {
      "input": "the new elegy",
      "status": "ambiguous",
      "vehicle_ids": ["elegy", "elegy-rh8"],
      "quantity": 1
    }
  ]
}

One object per line you pasted, with the line you wrote kept beside it, a status, the ids it thinks you meant, and how many of them ("2x zentorno" is a quantity of two).

And then none of it is trusted.

The rules the validator applies

The file that does this is pure and client-safe, and its comment is the whole design in one line: it sanitises the matcher's JSON so nothing it invents can reach the database.

  • Any id that isn't in the catalogue is dropped, and duplicates are removed.
  • A matched item keeps its best surviving id. If it had none, it becomes not_found.
  • An ambiguous item keeps up to four surviving ids, because four is what the chooser shows. If exactly one survives it becomes matched, and if none do it becomes not_found.
  • A not_found item, or anything with a status I don't recognise, carries no ids at all.
  • Quantity is clamped to between 1 and 20.

The line I'd point at is the second one. The status doesn't come from the model either. It's re-derived from how many ids were left after the check. If I kept the model's own word for it, an item could be matched with nothing to match to, and then the only question left is which part of the code finds that out first.

The gap between preview and confirm

Validating the ids isn't enough on its own, because the preview and the write are two different moments. You paste, you see the matches, you fix the ambiguous ones by hand, and then you confirm. The preview is free. The confirm is what costs credits and writes rows.

So capacity is read again at confirm, live, not carried over from the preview:

const [capacity, current] = await Promise.all([
  capacityForStorageLocation(ownedPropertyId, assignedUpgradeId),
  currentCarCountAt(ownedPropertyId, assignedUpgradeId),
]);
const free = Math.max(0, capacity - current);
if (n > free) return { capacityExceeded: { free, requested: n } };

The slot numbers get the same treatment. You can arrange the cars into specific slots in the preview, and those slots are checked against the garage as it is now. Anything that's been taken since falls back to the lowest free slot instead. A preview also expires after two hours, so a tab left open overnight can't confirm against a garage that's moved on.

None of this is because the model might lie about capacity. The model is never asked about capacity. It's because the person had a correct picture of their garage a few minutes ago, and a few minutes is long enough on another device.

What being this careful actually costs

Less than I expected. The matching is the only part that needs the model, and everything after it is ordinary code: the parser, the catalogue lookup and the validator are three pure modules with 23 tests between them, and none of them need a model call to run.

The rest is caps, and caps are cheap. The input is limited to 2,000 characters and 60 items. There's a rolling limit of 30 match calls per person per day, counted from the import rows I already keep. There's an environment variable that turns the whole thing off without a deploy. And the pasted text is never stored: the record of an import keeps counts, cost and token usage, and nothing else.

The general version

A model's output is input. It arrives from outside, it's shaped like what you asked for, and you have no guarantee about what's in it, which is exactly the position you're in with a form post from a stranger. I don't think that's a special rule for language models. It's the normal rule, applied to a place where the output looks authoritative enough that it's tempting to skip it.

So the model gets to suggest, and the database decides. Everything it returns is checked against the real catalogue before it can be written, and everything about the state of the world is read fresh at the moment of writing. If the model misunderstands a request, you get a wrong car in a chooser and you fix it. There's no version of a misunderstanding that ends up as a row you didn't ask for.

Published llm · validation · correctness