← Back

Why my AI agent isn’t allowed to check its own work

Casefile uses AI to draft anti-money-laundering case notes, but plain code checks every number against the ledger before anyone reads them. Here’s how that works, and the bug that slipped past it the first time.

Picture an investigation note with one wrong dollar amount in it. That’s worse than no note at all. If the analyst trusts it, they might escalate the wrong case. If they catch it, they stop trusting the tool.

Language models will happily write “$12,400 moved through 14 accounts” whether or not it’s true. So when I built Casefile, an assistant that drafts the first pass of an anti-money-laundering (AML) investigation, I gave myself one rule: the AI can write the note, but code decides whether each fact in it is true.

Most of the job doesn’t need AI

When I broke an AML first pass into steps, most of them didn’t need a language model at all. So each step went to the simplest tool that could do it well:

  • Finding alerts and pulling transactions: SQL, through read-only, parameterized tools. The model never writes SQL.
  • Scoring how risky an alert is: a small tabular model.
  • Matching behaviour to red flags: retrieval over 9 real FINTRAC indicator sentences that I mapped to patterns, plus a rule-based check that the data actually supports them.
  • Writing the narrative: the language model. This is the only step that uses AI.
  • Checking every number in that narrative: a verifier written in plain Python. Same input, same answer, every time.
  • Deciding what to do: a human analyst. Casefile never files anything.

The writer builds the note one sentence at a time, and every sentence carries the transaction IDs it’s based on, like a citation.

What gets checked

For every sentence, the verifier pulls up the cited transactions in the ledger and asks:

  1. Do all the cited transactions exist, and is every transaction ID in the text cited?
  2. Does every dollar amount match a cited transfer, or the total of the cited money coming in or going out?
  3. Is every date the date of a cited transfer? And does “between D1 and D2” cover the cited transfers exactly?
  4. Is every account actually part of a cited transfer?
  5. Are the counts right (“12 transfers”, “10 distinct senders”)?
  6. Is there any other number in there? If the verifier doesn’t recognise a number, it rejects the sentence.
  7. Does every red flag it cites really hold for those transfers? “Fan-in”, for example, needs at least 10 distinct senders. (That threshold is mine, not FINTRAC’s.)

Number 6 is the one I care about most. A checker that skips what it doesn’t understand lets the scariest mistakes straight through.

When a sentence fails

When a check fails, the verifier sends back specific errors the agent can read, saying exactly which check failed on which claim, like an amount that matches no cited transfer. The agent fixes the claim and tries again. It’s a loop, built with LangGraph:

investigate -> match indicators -> write -> verify
     ^                                         |
     +--------------- errors (max 3) ----------+
                                               |
               still failing after 3 -> manual review (bad claims withheld)

If a sentence still fails after three tries, the case goes to manual review, and the sentences that never passed are held back. Whoever picks it up can always see which parts were proven.

Breaking it on purpose

To find out whether the verifier really catches mistakes, I made the writer lie. A fault-injection mode corrupts claims on purpose: wrong amounts, wrong dates, wrong accounts, wrong counts, fake transaction IDs, made-up numbers, and red flags the data doesn’t support.

Over two eval runs, one with 30% of claims corrupted and one with 60%:

  • It flagged 417 of 417 corrupted claims.
  • It wrongly flagged 0 of 994 clean ones.

To see what the verifier is actually worth, I ran the same pipeline without it. At a 30% fault rate, that version got its citations right 70% of the time, and only 28% of its notes were fully correct. With the verifier on, everything Casefile put out checked out: 94% of cases passed, and the other 6% went to manual review.

The bug that got through first

The first version didn’t hit 100%. It caught about 95% of the corrupted claims.

The hole was date ranges. Each date in a sentence only had to match one of the cited transfers. So in “between March 3 and March 19”, a wrong start or end date still passed, as long as it was the date of some cited transfer, even if it wasn’t the first or the last one.

The fix was a stricter rule (a “between” range has to cover the cited transfers exactly), plus a test so it stays fixed.

That same run caught a bug in my fault injector, too. One red flag it had labelled “unsupported” was actually supported by the data. The verifier was right and my test label was wrong. Now the injector only picks red flags that really fail.

What it can’t do (yet)

I’d rather tell you these myself than have you find them:

  • The data is synthetic. I generated the ledger with laundering patterns planted in it, so they’re cleaner than real data.
  • The “AI writer” in these tests is a template stand-in, not a real language model. The numbers above show how well the verifier catches and repairs mistakes I injected. They don’t tell you how often a real model gets things wrong. Testing against a real model is next.
  • “100% of what Casefile puts out is correct” is partly true by design. Casefile only puts out claims that passed the verifier. The numbers that actually mean something are the catch rate, the false-flag rate, and the gap against the version with no verifier.
  • The verifier proves the numbers match the ledger. It doesn’t prove a note is insightful, or that a red flag is the legally right one. That’s still the analyst’s call.

What I took away

I stopped trying to make the model careful and made the pipeline strict instead. Nothing reaches a person until code has checked it against the ledger. And when I find a hole in the checker, it gets a rule and a test, so it stays closed.

Casefile is built with Python, LangGraph, FastAPI, SQLite, React and TypeScript, and has 36 automated tests in CI. The repo is private while I finish it.