Dev.to AI 🤖 Ai 👁 0 📖 2 min read

I kept finding credit card numbers in LLM prompts, so I wrote a redactor

If you've shipped any AI feature in the last year, you've probably written a line that looks about like this: ts const summary = await llm.complete(`Summarize this chat: ${conversation}`); It works, you move on.

If you've shipped any AI feature in the last year, you've probably written a line
that looks about like this:

ts
const summary = await llm.complete(`Summarize this chat: ${conversation}`);

It works, you move on. But conversation is whatever the user typed, and in a
fintech product that occasionally means a card number, or an IBAN, or someone's
email sitting right there in the string. Which you've now sent off to a model
provider, where it gets logged on a server you don't control.

Nothing breaks when this happens. The data just flows through and the feature
does its job, so it's the kind of thing that's really easy to ship without
thinking about. That's what bugged me about it.

So I made a small thing to sit in front of that call. It's on npm as
[scrubly.]

The whole idea is: find the sensitive bits before the prompt leaves, replace them
with placeholders, and then swap the real values back into whatever the model
sends you.

import { redact } from 'scrubly';

const { text, restore } = redact(
  'Charge €40 to IBAN DE89 3704 0044 0532 0130 00, receipt to [email protected]',
);

text;
// 'Charge €40 to IBAN [IBAN_1], receipt to [EMAIL_1]'

const reply = await llm.complete(`Rewrite this as a formal note: ${text}`);

restore(reply);
// the real IBAN and email are back in the model's answer

The placeholders stay consistent inside a single call, so if the same IBAN turns
up three times it's [IBAN_1] every time. The model still understands it's one
value, you've just never actually handed it over.

The thing I spent the most time on was not flagging stuff that only looks
sensitive. A plain "match 16 digits" regex will cheerfully redact your order IDs
and invoice numbers too, and then your output is full of [CARD_1] where there
was never a card. So instead of just matching a shape, scrubly runs the same math
a card number or an IBAN has to pass to be valid in the first place. If it
doesn't check out, it's left alone. Same for emails, SSNs, IP addresses, and a
couple of API-key formats, which have tighter patterns so they don't grab random
tokens.

I want to be honest about what this is, because "redact PII" promises a lot. It's
a good first layer, not a safety net. It won't catch a name, it won't catch a
card number someone typed with spaces or words in the middle, and it won't catch
a format I haven't added yet. If you're dealing with regulated data this does not
stand in for your actual compliance work. It just means the obvious stuff stops
leaking into prompts by accident.

npm install scrubly

Code's here: https://github.com/shilpidaswani/scrubly

If there's a format you'd want it to catch, I'd genuinely like to know. I built
the detectors around what I see in European fintech, so my blind spots are
probably non-European account formats and regional ID numbers. Open an issue or
just tell me in the comments.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.