# How to remove private and sensitive data from a prompt before it reaches OpenAI or Claude

> Python code that hides names, emails and tax numbers behind numbered badges before the model call, then puts the real details back in the answer.

1.  [Home](/)
2.  [Blog](/blog)
3.  How to remove private and sensitive data from a prompt before it reaches OpenAI or Claude

How-to

# How to remove private and sensitive data from a prompt before it reaches OpenAI or Claude

[![](/img/greg-stoos-portrait.jpg)**Greg Stoos**Founder of Opami](/authors/greg-stoos)

-   13 Nov 2026
-   Updated 14 Nov 2026
-   4 min read

![](/img/blog/flow.jpg?v=4)

In this article

1.  [The plan in four steps](#the-plan-in-four-steps)
2.  [Step 1 and 2: find and number](#step-1-and-2-find-and-number)
3.  [Step 3 and 4: call the model, then swap back](#step-3-and-4-call-the-model-then-swap-back)
4.  [Why a blank \[REDACTED\] breaks answers](#why-a-blank-redacted-breaks-answers)
5.  [Choose which types stay visible](#choose-which-types-stay-visible)
6.  [Where the text goes](#where-the-text-goes)

Share this article

-   [](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "LinkedIn")
-   [](https://x.com/intent/post?url=https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude&text=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude "X")
-   [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "Facebook")
-   [](https://bsky.app/intent/compose?text=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude%20https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "Bluesky")
-   [](https://www.threads.net/intent/post?text=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude%20https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "Threads")
-   [](https://wa.me/?text=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude%20https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "WhatsApp")
-   [](mailto:?subject=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude&body=https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "Email")
-   Link copied

In this article

1.  [The plan in four steps](#the-plan-in-four-steps)
2.  [Step 1 and 2: find and number](#step-1-and-2-find-and-number)
3.  [Step 3 and 4: call the model, then swap back](#step-3-and-4-call-the-model-then-swap-back)
4.  [Why a blank \[REDACTED\] breaks answers](#why-a-blank-redacted-breaks-answers)
5.  [Choose which types stay visible](#choose-which-types-stay-visible)
6.  [Where the text goes](#where-the-text-goes)

Send the text to Opami Protect first, replace each item it finds with a numbered badge, call the model with the badged text, and swap the real details back into the answer.

Your app sends a support ticket to a language model and asks for a reply draft. The ticket looks like this:

> Ana Silva from Lusomar says her tax number 123 456 789 is wrong on invoice 2041. She wrote from [ana.silva@lusomar.example](mailto:ana.silva@lusomar.example) and wants Bruno Costa to call her. Ana also asked Marta Reis to join.

The model needs the problem: a wrong tax number on an invoice, a call to arrange. It does not need Ana's name, her email or her number. The steps below hide them before the call and put them back after the answer.

## The plan in four steps

1.  Send the text to Opami Protect and get back what it found, with positions.
2.  Replace each item with a numbered badge, such as _PERSON_**1**. The same person gets the same badge each time.
3.  Send the badged text to the model.
4.  Swap the real details back into the answer.

## Step 1 and 2: find and number

`POST https://api-protect.opami.ai/v1/detect` takes your text and returns detections with `text`, `type`, `start` and `end`. The offsets count Unicode code points, which is what Python strings index by. The code below builds the badges.

pythonCopy

```
import os
import requests

URL = "https://api-protect.opami.ai/v1/detect"
HEADERS = {"Authorization": f"Bearer {os.environ['OPAMI_API_KEY']}"}

LABELS = {"person.name.full": "PERSON", "contact.email": "EMAIL"}

def hide(text, exclude=()):
    body = {"model": "protect-v1", "text": text}
    if exclude:
        body["exclude"] = list(exclude)
    reply = requests.post(URL, headers=HEADERS, json=body, timeout=30)
    reply.raise_for_status()
    found = sorted(reply.json()["detections"], key=lambda d: d["start"])

    badge_for = {}   # (label, lowercase text) -> "[PERSON_1]"
    count = {}       # label -> how many badges so far
    real = {}        # "[PERSON_1]" -> "Ana Silva"
    out, pos = [], 0
    for d in found:
        if d["start"] < pos:            # overlaps the previous detection
            continue
        label = LABELS.get(d["type"], d["type"].split(".")[0].upper())
        key = (label, d["text"].casefold())
        if key not in badge_for:
            count[label] = count.get(label, 0) + 1
            badge_for[key] = f"[{label}_{count[label]}]"
            real[badge_for[key]] = d["text"]
        out.append(text[pos:d["start"]])
        out.append(badge_for[key])
        pos = d["end"]
    out.append(text[pos:])
    return "".join(out), real
```

Two details matter. The `key` is the label plus the lowercase text, so "Ana Silva" and "ana silva" share one badge, and two different people get two badges. The `real` dictionary stays in your process. Opami Protect keeps neither your text nor what it found, so this map is the only copy of the way back.

A first name on its own ("Ana") gets its own badge in this code, because the code matches on the text it sees. Add a rule of your own if your tickets use first names a lot.

## Step 3 and 4: call the model, then swap back

Use whichever model you work with. `call_model()` stands in for the vendor library you already have.

pythonCopy

```
def call_model(prompt):
    ...   # your OpenAI, Claude or own-model call; returns the answer text

def restore(answer, real):
    for badge, original in real.items():
        answer = answer.replace(badge, original)
    return answer

ticket = (
    "Ana Silva from Lusomar says her tax number 123 456 789 is wrong on "
    "invoice 2041. She wrote from ana.silva@lusomar.example and wants "
    "Bruno Costa to call her. Ana also asked Marta Reis to join."
)

safe_text, real = hide(ticket)
answer = call_model("Draft a short reply to this ticket:\n" + safe_text)
print(restore(answer, real))
```

The model reads "_PERSON_**1** from _COMPANY_**1** says her tax number _TAX ID_**1** is wrong". Your customer reads "Dear Ana Silva". The labels for types other than names and emails come from the type the API returns, so they can differ from the ones shown here.

## Why a blank \[REDACTED\] breaks answers

Take the same ticket with each name replaced by \[REDACTED\]. The model sees "\[REDACTED\] from \[REDACTED\] asked \[REDACTED\] to call \[REDACTED\]". It cannot tell the customer from the colleague, so it writes a vague reply or invents a role. A numbered badge keeps the structure: _PERSON_**1** asked _PERSON_**2** to call her, and the model can say so. It also gives your code something to find when it puts the details back.

## Choose which types stay visible

Some details are harmless for your task, and a model can use them. The `exclude` field takes categories, `category.sub` pairs or full types, and the API returns no detection for them, so they stay in the text:

pythonCopy

```
safe_text, real = hide(ticket, exclude=["organization"])
```

Now Lusomar stays readable and the names, email and tax number still turn into badges. An entry that matches no type returns `400 unknown_category`, which catches a typo early. The full list of 159 types in 23 categories is in the console docs. The text is charged in full whether or not you exclude types.

## Where the text goes

Your server sends the text to Opami Protect, which processes it in the EU (Sweden, with a fallback in Germany) or on your own servers, and keeps neither the text nor what it found. Your server then sends the badged text to the model vendor. That vendor receives badges, and the real details stay in your `real` dictionary. One call handles up to 20,000 characters, so split a longer document into parts and run each part through `hide()`. Badge numbers restart in each call, so keep one `real` dictionary per part.

Hidden data is still personal data under EU law, and Opami Protect misses some private data. Run it on a sample of your own tickets and read what it missed before you rely on it.

Share this article

-   [](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "LinkedIn")
-   [](https://x.com/intent/post?url=https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude&text=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude "X")
-   [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "Facebook")
-   [](https://bsky.app/intent/compose?text=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude%20https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "Bluesky")
-   [](https://www.threads.net/intent/post?text=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude%20https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "Threads")
-   [](https://wa.me/?text=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude%20https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "WhatsApp")
-   [](mailto:?subject=How%20to%20remove%20private%20and%20sensitive%20data%20from%20a%20prompt%20before%20it%20reaches%20OpenAI%20or%20Claude&body=https%3A%2F%2Fwww.opami.ai%2Fblog%2Fremove-private-data-before-openai-or-claude "Email")
-   Link copied

![Greg Stoos](/img/greg-stoos-portrait.jpg)

Written by

[Greg Stoos](/authors/greg-stoos), founder of Opami

Greg Stoos founded Opami in Lisbon and builds Opami Protect, an API that finds and hides private and sensitive data in a text before an AI model reads it.

[His articles →](/authors/greg-stoos)

## Try it on your own data

Opami Protect is in private beta. Register at the console, run a sample of your own text, and see what it hides. No sales call.

[→ Protect console](https://console.opami.ai)[Read the quickstart](https://console.opami.ai/docs)

## Keep reading

[All articles →](/blog)

-    [![](/img/blog/measure.jpg?v=4) Proof**How to measure what your PII filter misses, on your own data**4 min read_Read →_](/blog/measure-what-your-pii-filter-misses)
-    [![](/img/blog/routes.jpg?v=4) How-to**Regex, an open-source detector or a model: which PII filter fits your AI app?**4 min read_Read →_](/blog/regex-detector-or-model-for-pii)

## Get the answers in your inbox

Each week, the three articles, one thing we learned and one number, in one email.

Email

What do you want to hear? Tick any The weekly letter: three articles, one thing we learned, one number The day Opami Chat opens The day Opami Bot opens The day Opami Brain opens

I agree to receive these emails. I can stop them with one click in each email.

Sign up
