6 min read

Don't Let the LLM Name Your Entities

An LLM called one credit card three different names and my app created three accounts. Use model output for meaning, never for identity: key records on facts the model copies, not labels it writes.
A glowing fingerprint on a dark circular scanner surface with concentric lines
Photo by George Prentzas on Unsplash

I have a small side project that reads the payment notifications my bank sends me and turns them into a ledger. An LLM does the reading: it takes a message like "Your card ending 1234 was charged 12.50 at Coffee Shop" and returns structured fields: amount, merchant, a category, and which account the money came from.

It worked well, until I opened the accounts page and found the same credit card listed three times.

Nothing was wrong with any single extraction. Every one of them was a reasonable reading of its message. The bug was in what I did with the output: I let the model's wording decide which database row a transaction belonged to.

How one card became three accounts

For each parsed message the model returned an account hint: a display name and, when the message included it, the card's last four digits. My first version of "find or create the account" matched on both:

-- the original lookup
SELECT id FROM accounts
WHERE user_id = $1
  AND lower(name) = lower($2)   -- model-generated label
  AND last_four   = $3;
-- no row? insert a new account

That looks careful. Two fields must agree, so surely it is stricter? The problem is that one of those fields is not a fact about the card. It is the model's description of the card, and descriptions drift. Across a few weeks of messages, the same card came back as "Example Bank Card", "Example Bank Credit Card" and plain "Example Bank". Each new spelling missed the lookup and created a fresh account. Transactions for one card were spread across three rows, and every total on the dashboard was quietly wrong.

Adding the label to the key did not make it stricter. It made it unstable. A compound key is only as stable as its least stable part.

Meaning versus identity

The idea I took away is a split I now apply to anything an LLM extracts:

  • Meaning is what the model is good at: "this is a card payment", "this looks like groceries", "a friendly name for this account is Example Bank Card". If it phrases it a little differently next time, nothing breaks.
  • Identity is how you decide two records refer to the same thing. It has to give the same answer every time you ask. Free text generated by a model fails that test almost by definition: it is sampled, it shifts with the prompt and the model version, and it shifts with the surrounding text.

So the rule is: let the model say things, but key records on something the model only copies. A card number fragment printed in the message is a fact the model passes through. A name it composes is an opinion.

The fix: key on the natural identifier

The change in the lookup was small. When the model extracts the last four digits, match on those alone. Only when there is no such identifier (a digital wallet, say, or a catch-all bucket for unparseable messages) fall back to the name, and only against accounts that also have no identifier:

function findAccount(accounts, hint) {
  const lastFour = normalizeLastFour(hint.lastFour); // trim, keep trailing 4
  if (lastFour) {
    return accounts.find(a => a.lastFour === lastFour);
  }
  const name = hint.name.trim().toLowerCase();
  return accounts.find(a => a.lastFour === null && a.name.toLowerCase() === name);
}

Two details mattered more than I expected. First, normalise the identifier too. The model copies whatever the message prints, and some messages print five digits or pad with spaces, so I store only the trimmed trailing four. An identifier that is "the same" in two formats is not the same key. Second, the fallback path is still name-based, so it can still drift. I accepted that because those accounts are few and rarely change, but it is the weak spot and I know where it is.

Looking back, I would also back the rule with the database rather than only with application code: a unique index on (user_id, last_four) where last_four is not null. Then a future code path that forgets the rule fails loudly instead of quietly creating a duplicate.

Cleaning up what was already there

Fixing the lookup stops new duplicates; it does nothing for the existing ones. A migration did the merge: normalise every stored identifier, group accounts by owner and identifier, keep the oldest as canonical, repoint child rows, delete the rest.

WITH ranked AS (
  SELECT id,
         FIRST_VALUE(id) OVER w AS keep_id,
         ROW_NUMBER()    OVER w AS rn
  FROM accounts
  WHERE last_four IS NOT NULL
  WINDOW w AS (PARTITION BY user_id, last_four
               ORDER BY created_at, id)
)
UPDATE transactions t
SET account_id = r.keep_id
FROM ranked r
WHERE t.account_id = r.id AND r.rn > 1;
-- then repoint any other child tables the same way,
-- and delete accounts with rn > 1

Rows with no identifier were left alone on purpose. Two wallets with no card number are not duplicates just because they share a missing value, and merging on NULL would have been the same mistake in reverse.

The same trap in duplicate detection

A little later I added a page that lists probable duplicate transactions, for the cases where the same payment got recorded twice. The natural key seemed to be account, amount, day and merchant. I left the merchant out.

The merchant is model output too. Re-parse the same message and "Coffee Shop" might come back as "Coffee Shop Ltd" or "COFFEE SHOP". Those are exactly the duplicates I wanted to catch, and putting the merchant in the key would have hidden them. So the grouping is account, amount, direction and calendar day, all values the model copies rather than composes. The cost is false positives: two genuine coffees on the same day show up as a pair. That is a cheap mistake, because a person glances at it and moves on. Missing a real duplicate is the expensive one.

Where I went the other way: deleting the rule engine

Around the same time I deleted a hand-written rule engine: regex patterns that mapped merchants to categories, plus alias tables to tidy up merchant names. Every message now goes to the model for categorisation, with manual overrides on top.

That might look like the opposite lesson, so it is worth being honest about the trade. Categorisation is meaning. If the model files a purchase under "Eating out" one week and "Coffee" the next, I get a slightly noisy chart, I fix it in one click, and no rows are created or orphaned. The rules were deterministic, but they were brittle in their own way: every new merchant needed a new pattern, and I was maintaining a growing pile of regexes for a job the model does well.

What I gave up is real, though. I lost reproducibility (the same message can be categorised differently on a re-run), I lost a free and instant path that needed no API call, and I lost the ability to explain a category with "rule 14 matched". For a personal ledger those are acceptable. For anything audited, or anything where a category drives money movement, I would keep a deterministic layer and let the model only fill the gaps.

Takeaway

Treat LLM output as a good source of meaning and a bad source of identity. Before a generated field goes into a lookup, a unique key, a grouping or a join, ask whether it is something the model copied from the input or something it wrote. Key on the first kind. Display the second.