# How to redact PII before your app calls the OpenAI or Anthropic API

**Short answer.** Put the redaction between your code and the provider, on
your own machine: a local proxy that you point your SDK's base URL at, or a
middleware inside your process. It replaces names, SSNs, medical record
numbers, emails and account numbers with stable placeholders such as
`[PERSON_1]`, sends that to the model with your own key, and puts the real
values back into the answer. The model never sees the identifiers, your code
barely changes, and anything that cannot be redacted (an image, a scanned PDF)
is refused instead of being forwarded.

---

## Why "redact in the prompt" is not enough

A support bot, a claims assistant or a KYB agent builds its prompt from data
it did not write: tickets, PDFs, database rows. Asking the model to "ignore
personal data" does nothing, because the data has already left. Cleaning the
prompt by hand in each call site works until someone adds a new one. The
reliable place is the one every request goes through: the client that talks
to the API.

Three properties matter more than how many identifier types a tool lists:

1. **It runs where the data already is.** A redaction service in someone
   else's cloud is one more place your documents go.
2. **It is reversible inside your process.** The model reasons with
   `[PERSON_1]`; your user should still read the real name in the answer.
3. **It fails closed.** What it cannot read, it does not forward.

## Option 1: a local proxy, one line of code

[Sırdaş Gateway](/en/developers/gateway/) listens on `127.0.0.1:8787` and
speaks the OpenAI and Anthropic APIs. You change the base URL and nothing else:
your key, model and prompts stay as they are.

```bash
npx -y @sirdaspdf/gateway@0.6.0
```

```python
from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:8787/openai/v1")

from anthropic import Anthropic
client = Anthropic(base_url="http://127.0.0.1:8787/anthropic")
```

What happens on each request:

- The text is redacted with one map per request. The same value always gets
  the same placeholder, so the model can still tell two patients apart.
- An inline PDF is converted to text on your machine and redacted with the
  rest. The PDF bytes never reach the provider.
- The answer comes back with the real values, streaming included: a
  placeholder split across two chunks is held until it is complete.
- An image, audio or a scanned PDF gets a `422` and **nothing is sent**.
  Unknown routes get a `404`; they are never forwarded.

Every response carries `x-sirdas-redacted-types` (for example
`PERSON:2,SSN:1`), and the log line has counts, never values.

## Option 2: inside your process, with the Vercel AI SDK

If you would rather not run a second process, the same engine wraps the model:

```ts
import { generateText, wrapLanguageModel } from "ai";
import { openai } from "@ai-sdk/openai";
import { sirdasMiddleware } from "@sirdaspdf/gateway/ai-sdk";

const model = wrapLanguageModel({
  model: openai("gpt-5"),
  middleware: sirdasMiddleware({ block: ["SSN"] }),
});
const { text } = await generateText({ model, prompt: ticket });
```

`block` turns a type into a hard stop: a ticket that contains a Social
Security number never reaches the model at all, even redacted. The
[recipes](/en/developers/recipes/) page has three complete cases (a support
bot, a KYB agent and questions over health records) whose code runs in CI
against the real SDK.

## Option 3: LangChain's own middleware, with a better detector

LangChain 1.x ships a `piiMiddleware` that redacts, masks, hashes or blocks,
but its built-in detectors cover emails, cards, IPs, MAC addresses and URLs.
Keep LangChain's strategy and give it Sırdaş as the detector, which adds SSNs,
EINs, labelled names, medical record numbers, accounts and national IDs:

```ts
import { piiMiddleware } from "langchain";
import { sirdasDetector } from "@sirdaspdf/core/detect";

piiMiddleware("sirdas", { detector: sirdasDetector(), strategy: "redact" });
```

In Python, `sirdas.integrations.langchain_pii_detector()` does the same for
LangChain's `PIIMiddleware`. LangChain's strategies do not restore the values
in the answer; if you need that, use options 1 or 2.

## Make it a control, not a convention

A proxy only protects the requests that go through it. If a security team is
going to rely on it:

- **One key per application, in a policy.** `SIRDAS_GATEWAY_POLICY` lists keys
  by their sha256 and sets which providers, models and data types each one may
  use. An unlisted key gets `403`; a typo in the file stops the gateway from
  starting.
- **Block the direct route.** Without an egress rule that sends provider
  traffic through the gateway, a developer can still call the API directly.
- **Let someone else check it.** `npx -y sirdas@0.6.0 comprobar` runs the
  engine with the network cut, redacts and restores sample documents and
  prints a fingerprint that repeats on any machine with the same version. It
  is a check you run yourself, not a certification.

## What it will miss

Redaction here is rule-based, plus lists of first names and surnames for
names in prose. On the public [pii-v1 benchmark](/benchmarks/pii-v1.jsonl)
(60 synthetic documents, 192 identifiers) the next release catches 95 %;
0.5.0, on npm today, catches 94 %. The misses are mostly names in prose whose
first name is not on the lists ("we spoke to Kwame this morning") and company
names in prose. Treat it as a large reduction of what reaches the provider, review the output on
your own documents, and keep the cases where a single leak is unacceptable
behind `block`.
