← All articles

How to give AI your customer chats without giving away their data

· 5 min read · Anton Tkachev

Translated from Russian. Read the Russian original.

The question I get asked most about AI in business isn't "which model should I pick." It's this one: "Can I give an AI model my customer chats, contracts, order spreadsheets?"

Those are full of phone numbers, emails, ID numbers, addresses, names. Sending that to a third-party service "just to analyze it" means sending data about people who never agreed to it. "I'll ask the model not to remember anything" isn't protection. Protection is when the model simply never sees that data.

This is called masking. It sounds like a job for the security department, but in reality it's one script you can write in an evening. Here's how I do it in my projects.

The principle: originals never leave your computer

The setup is always the same:

  1. The originals stay with you, in a separate folder.
  2. A script replaces everything personal with labels and puts anonymized copies in another folder.
  3. The AI model gets only the anonymized copies.
  4. If the model's answer needs to go back to the customer, a second script puts the real values back in place of the labels. Also on your computer.

A script isn't AI. It doesn't "understand" the text and doesn't send it anywhere. That's why you can trust it with the originals.

Two modes: one-way and round-trip

One-way — when you need analysis, not a reply to a specific person. What do customers ask most often? What do they complain about? Which topics are growing?

That's how I processed 428 thousand chat messages for a city guide. Phone numbers turned into [ТЕЛЕФОН], emails into [EMAIL], documents into [ДОКУМЕНТ], street addresses with a house number into [АДРЕС]. Message authors weren't passed along at all, and the exact date only as a month. Nothing needs to come back: for analysis, "who exactly asked" doesn't matter.

Round-trip — when the model drafts a reply, an email, or a document for a specific customer. Then each value gets its own label: the first phone number is PHONE-1, the second is PHONE-2, the customer is CLIENT-A. The "label → real value" pairs live in a dictionary on your computer. The model writes "Call CLIENT-A back at PHONE-1," and the unmasking script turns that back into normal text.

What to mask

What Label example How it's caught
Phone numbers PHONE-1 with a pattern (regular expression)
Email EMAIL-1 with a pattern
ID numbers, tax numbers DOC-1 with a pattern for your country's format
Bank cards and accounts CARD-1 with a pattern
Street addresses with a house number ADDRESS-1 with a pattern on words like "street," "rua," "avenida" + a number
Links to profiles and messengers PROFILE-1 with a pattern on domains
Names of people and companies CLIENT-A with a dictionary — see below

20 lines of code

Here's the minimal round-trip version. You can use it as a starting point and ask AI to adapt it to your data.

import re

PATTERNS = [                                   # order matters: narrow to broad
    ("EMAIL", r"[\w.+-]+@[\w-]+\.[\w.]+"),
    ("CARD", r"\b(?:\d{4}[ -]?){3}\d{4}\b"),
    ("PHONE", r"\+?\d[\d\s()-]{8,}\d"),
]

def mask(text, vault):
    for label, rx in PATTERNS:
        def swap(m):
            real = m.group(0)
            if real not in vault:              # one value, one label
                vault[real] = f"{label}-{len(vault) + 1}"
            return vault[real]
        text = re.sub(rx, swap, text)
    return text

def unmask(text, vault):
    back = {label: real for real, label in vault.items()}
    return re.sub(r"\b[A-Z]+-\d+\b", lambda m: back.get(m.group(0), m.group(0)), text)

Two details that are easy to miss:

  • Pattern order. A card number is also 16 digits, and the phone pattern will happily "eat" it. So narrow patterns go first, broad ones after.
  • One value, one label. If a phone number appears in the text three times, it has to get the same label every time. Otherwise the model will think those are three different numbers.

The vault dictionary never gets sent to the model. It belongs next to the originals.

Names are the hardest part

Try running the phrase "Ana, +55 48 99123-4567" through this code. The phone number gets hidden, but "Ana" stays as is. You can't catch a name with a pattern: it looks like any other word.

What works in practice:

  • A dictionary of known names. You already have a customer list — in your CRM, a spreadsheet, your contacts. The script takes it and hides every name on the list.
  • Author names. In a chat export, every message has an author. I took the authors' names and hid them wherever they showed up in the text. With one trick: if a word is often written lowercase in the chat, it's a regular word, not a name, so we leave it alone.
  • What I deliberately don't hide. Common first names without a last name, and place names ("Maria," "Florianópolis"). They don't point to a specific person, and without them the text stops making sense.

Mandatory self-check

Masking that nobody checked is hope, not protection. After masking, the script should hunt for leftovers on its own: @ signs, long strings of digits, document formats. If it finds even one, it stops and doesn't pass the file on.

In that same chat analysis, the self-check went through all 9,181 questions and, separately, the 5,660 author names. Leftover phone numbers, emails, usernames, and ID numbers: zero. Only after that did the file go to the server where AI worked with it.

Honest limitations

  • Context gives people away too. "My neighbor, the only dentist in our village" — not a single name or phone number, but the person is recognizable. So give the model the minimum number of fields: month instead of date, no authors, no extra columns.
  • Quotes don't get published. Even masked, the text is still someone else's words. Analysis — yes; verbatim quotes on a website or in a post — no.
  • Files, not just text. Patterns can't read PDFs, scans, or photos of documents. Those need a separate step, or a decision that such files don't go to the model at all.

What to do today

Take one file you've been wanting to give to AI for a while: a chat export, an order spreadsheet, a folder of emails.

  1. Write down what's personal in it: phone numbers, emails, names, ID numbers.
  2. Ask the model: "Write a Python script that replaces these with labels, keeps the label dictionary on my computer, and can restore the real values. Add a self-check for leftovers." You can attach the code from this post.
  3. Check the result with your own eyes: open the anonymized copy and search for @ and long numbers.

If there are no leftovers, you can give the file to the model. Your customers' data stays with you, and you get the same value from AI.