Redacting personal data from logs and LLM prompts in Python: emails, CNICs, cards, IBANs and phone numbers
Personal data spreads through logs and LLM prompts without anyone deciding to put it there: an error message includes the request body, a support summary pastes the customer's email, a debugging line prints a document number. Redact it before it's written or sent: match each kind of identifier and r

eplace it with a typed placeholder such as [EMAIL] or [CNIC], so the text stays readable and you can still see what kind of data was there. Two lessons from testing my own redactor: a broad pattern produces false positives (my phone-number pattern redacted an order number), and card numbers need a Luhn check to tell them apart from other long numbers. Here's the tested version and its limits. ```python title="redact.py" def luhn_ok(digits: str) -> bool: PATTERNS = [ def redact(text: str) -> str: **Order matters.** The specific patterns (email, CNIC, IBAN) run before the generic digit patterns.
Otherwise a CNIC such as `35202-1234567-1` could be partly consumed by the card or phone pattern, leaving fragments behind. ## What the tests check | Input | Output | | -------------------------------------------------------- | --------------------------------------- | | `Customer ali@example.com uploaded CNIC 35202-1234567-1` | `Customer [EMAIL] uploaded CNIC [CNIC]` | | `Card 4111 1111 1111 1111 declined` | `Card [CARD] declined` | | `Order 1234567812345678 shipped` | unchanged: fails Luhn, not a card | | `Payout to GB82 WEST 1234 5698 7654 32 failed` | `Payout to [IBAN] failed` | | `Call +92 300 1234567 after 5pm` | `Call [PHONE] after 5pm` | | `or 0300-1234567` | `or [PHONE]` | | `Latency p95 was 1234 ms on 2026-11-20` | unchanged | | `Request 7f9c2a10-55e1-4c3b-9d1a-0b6f3e2c8a77 ok` | unchanged: request IDs stay usable | My first phone pattern accepted any run of 10 to 16 digits with optional `+`, spaces and dashes. It redacted the **order number** as `[PHONE]`, which would have made the logs useless for exactly the debugging they exist for. The fix is to be specific: international numbers must start with `+`, and local numbers must match a known national format (here Pakistan's mobile `03XX-XXXXXXX`).
Bare digit runs are left alone. That's a deliberate trade-off: a local number typed without a recognisable format gets through, and you add formats for the countries you serve. The Luhn rule works the same way for cards: one random number in ten passes Luhn, so it reduces false positives without eliminating them. The algorithm is explained in [Luhn and IBAN check digits](https://aliahmed.si/blog/luhn-iban-check-digits/).
## Where should redaction happen? * **In the logger**, not at every call site. A logging filter or formatter that runs `redact()` on every message catches the lines people forget about. Redact structured fields by **name** too (`email`, `cnic`, `document_number`), which is more reliable than matching values.
* **Before LLM calls.** Summarising a support ticket doesn't need the customer's card number. Redacting before the prompt also keeps the data out of the provider's logs, and makes prompt-injection leaks less damaging; see [canary tokens for detecting prompt leaks](https://aliahmed.si/blog/canary-tokens-system-prompt-leaks/). * **Not in the system of record.** The KYC case itself needs the real document number. Redaction is for copies, logs and prompts, not for the data the business actually has to process.
## What regex can't catch * **Names and addresses.** "Ali Ahmed, House 12, Street 4" has no fixed shape. That needs a named-entity recognition model, or better, not logging free text at all. * **Digits in other scripts.** Python's `\d` matches Urdu and Arabic digits (`Ϋ³Ϋ΅Ϋ²Ϋ°Ϋ²`), but the same patterns in many other regex engines and log tools match only ASCII digits. Convert digits to ASCII before redacting, as in [validating a Pakistani CNIC number](https://aliahmed.si/blog/pakistan-cnic-number-validation/).
* **Images and documents.** A photo of an ID card in a log bucket is personal data too. Treat the redactor as a safety net that **reduces** exposure, and keep the rule that personal data shouldn't be logged in the first place. ## Summary * Redact logs and prompts with **typed placeholders**, so text stays readable. * Run specific patterns first; confirm card numbers with **Luhn**.
* Make broad patterns (phone numbers) **specific**, or they eat IDs you need for debugging. * Redact in the logger and before LLM calls, not in the system of record. * Regex misses names, addresses and images: log less, as well as redacting. Related: [KYC vs AML vs KYB](https://aliahmed.si/blog/kyc-vs-aml-vs-kyb/) and [request IDs in Node.js logs](https://aliahmed.si/blog/request-id-logging-asynclocalstorage/).
--- Written by Ali Ahmed Β· [Original article](https://aliahmed.si/blog/redact-pii-logs-llm-prompts-python/)
Key Takeaways
- β’Two lessons from testing my own redactor: a broad pattern produces false positives (my phone-number pattern redacted an order number), and card numbers need a Luhn check to tell them apart from other long numbers.
- β’This story was reported by Dev.to, covering developments in the dev space.
- β’AI advancements continue to reshape industries β read the full article on Dev.to for complete coverage.
π Continue reading the full article:
Read Full Article on Dev.to βShare this article



