Blog

PII Redaction in Node.js: 4 Approaches Compared

How to redact PII in Node.js before calling an LLM: regex, Transformers.js NER, a Presidio sidecar or a gateway, with restore and streaming.

Engineering7 min readBen @ Grepture

Your prompts are full of personal data. Your stack is TypeScript.

If you build LLM features in Node.js, sooner or later someone asks the uncomfortable question: what personal data are we sending to OpenAI? Support tickets, CRM records, chat transcripts and tool results all flow into prompts, and every one of them carries names, emails, account numbers and the occasional API key.

PII redaction in Node.js is where most teams then get stuck. The Python world has Microsoft Presidio and a dozen NER libraries. The JavaScript world has a handful of small regex packages, a few community ports and a lot of blog posts about something else. There is no official Presidio SDK for Node.

This guide compares the four approaches that actually work in a TypeScript codebase in 2026, with working code for each:

  1. Regex patterns in your own code
  2. A local NER model with Transformers.js
  3. Presidio as a sidecar service
  4. A gateway on the network path

For each one we look at what it catches, how it handles the hard parts (putting values back into the response, and streaming), and what it costs you to run.

What "redaction" has to do in an LLM app

Before comparing tools, it helps to be precise about the job. Redaction for LLM calls is not the same as redaction for log files.

  • Detect sensitive values in the outgoing prompt: structured ones (emails, IBANs, card numbers, keys) and unstructured ones (names, addresses, organisations).
  • Replace each value with a placeholder the model can still reason about. [PERSON_1] keeps the sentence intact. *** does not.
  • Restore the original values in the model's answer, so your user reads "Dear Maria Keller", not "Dear [PERSON_1]".
  • Handle streaming, because most chat UIs stream tokens, and a placeholder can arrive split across two chunks.

Most "redact PII in JavaScript" content stops after the first two steps. The last two are where production apps break. If you want the background on why restoring matters, our mask and restore deep-dive covers it in detail.

Every example below uses the same building block: a vault that maps placeholders to original values for the duration of one conversation.

// vault.ts
export type Vault = Map<string, string>; // "[EMAIL_1]" -> "maria.keller@hanse-klinik.de"

export function tokenFor(vault: Vault, type: string, value: string): string {
  for (const [token, original] of vault) {
    if (original === value) return token; // same value, same token
  }
  const n = [...vault.keys()].filter((t) => t.startsWith(`[${type}_`)).length + 1;
  const token = `[${type}_${n}]`;
  vault.set(token, value);
  return token;
}

export function restore(text: string, vault: Vault): string {
  return text.replace(/\[[A-Z_]+_\d+\]/g, (token) => vault.get(token) ?? token);
}

Reusing the same token for the same value matters more than it looks. If "Maria Keller" becomes [PERSON_1] in the first message and [PERSON_3] in the second, the model loses track of who is who.

Approach 1: Regex in your own code

The fastest way to start is a set of patterns you control. Regex is excellent at structured data with a fixed shape, and it runs in microseconds.

// regex-redact.ts
import { tokenFor, type Vault } from "./vault";

// Order matters: replace IBANs before generic digit runs, so a card
// pattern never eats part of an IBAN.
const PATTERNS: [type: string, re: RegExp, check?: (v: string) => boolean][] = [
  ["SECRET", /\b(?:sk-[A-Za-z0-9_-]{20,}|AKIA[0-9A-Z]{16}|ghp_[A-Za-z0-9]{36})\b/g],
  ["EMAIL", /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g],
  ["IBAN", /\b[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){3,7}(?: ?[A-Z0-9]{1,3})?\b/g],
  ["CARD", /\b(?:\d[ -]?){13,19}\b/g, luhn],
  ["PHONE", /\+\d{1,3}[\s-]?\d{2,4}[\s-]?\d{3,4}[\s-]?\d{2,4}/g],
];

function luhn(value: string): boolean {
  const digits = value.replace(/\D/g, "");
  let sum = 0;
  for (let i = 0; i < digits.length; i++) {
    let d = Number(digits[digits.length - 1 - i]);
    if (i % 2 === 1) d = d * 2 > 9 ? d * 2 - 9 : d * 2;
    sum += d;
  }
  return sum % 10 === 0;
}

export function redactRegex(text: string, vault: Vault): string {
  let out = text;
  for (const [type, re, check] of PATTERNS) {
    out = out.replace(re, (value) => (check && !check(value) ? value : tokenFor(vault, type, value)));
  }
  return out;
}

The Luhn check is not decoration. Without it, the card pattern matches order numbers, timestamps and half your invoice IDs.

Where regex wins: it is deterministic, dependency-free, fast, and easy to explain to an auditor. For secrets and structured identifiers it is the right tool, even inside more sophisticated systems.

Where it fails: names, addresses and organisations. No pattern reliably finds "maria" in a lowercase chat message or "Kastanienallee 12" in a German support ticket. If your prompts contain free text written by humans, regex alone will leak the most sensitive values: the ones that identify a person.

Approach 2: A local NER model with Transformers.js

To find names and places, you need a model. Transformers.js runs Hugging Face models on ONNX Runtime directly in Node, so you can do named entity recognition without a Python service.

npm install @huggingface/transformers
// ner-redact.ts
import { pipeline } from "@huggingface/transformers";
import { tokenFor, type Vault } from "./vault";

const LABELS: Record<string, string> = { PER: "PERSON", ORG: "ORG", LOC: "LOCATION" };

// Load once at startup, not per request.
const ner = await pipeline("token-classification", "Xenova/bert-base-NER");

type TokenResult = { entity: string; word: string; score: number };

/** Merge B-/I- tagged word pieces into whole entities. */
function entities(tokens: TokenResult[]): { type: string; text: string }[] {
  const out: { type: string; text: string }[] = [];
  for (const t of tokens) {
    const [tag, label] = t.entity.split("-");
    const type = LABELS[label];
    if (!type || t.score < 0.8) continue;
    const piece = t.word.startsWith("##") ? t.word.slice(2) : t.word;
    const last = out.at(-1);
    if (tag === "I" && last?.type === type) {
      last.text += t.word.startsWith("##") ? piece : ` ${piece}`;
    } else {
      out.push({ type, text: piece });
    }
  }
  return out;
}

export async function redactNer(text: string, vault: Vault): Promise<string> {
  const found = entities((await ner(text)) as TokenResult[]);
  let out = text;
  // Longest first, so "Maria Keller" is replaced before "Maria".
  for (const e of found.sort((a, b) => b.text.length - a.text.length)) {
    out = out.split(e.text).join(tokenFor(vault, e.type, e.text));
  }
  return out;
}

Run regex first and NER second: let patterns take the structured values, then let the model find the people and places in what is left.

A few things to know before you ship this:

  • The model decides your quality. bert-base-NER was trained on news text in English. It is good at "Angela Merkel visited Paris" and noticeably worse at "hey its marta from accounting". Lowercase chat, transcripts and non-English text are where generic NER models miss the most. Our comparison of open-source models for PII redaction goes through the options, including multilingual and zero-shot models like GLiNER.
  • You map entities back yourself. The pipeline gives you tagged word pieces. Joining them and finding them in the original text is your code, and it is where subtle bugs live (repeated names, tokenizer quirks, punctuation).
  • It costs memory and startup time. The model is downloaded on first run and loaded into every Node process that uses it. Plan for that in serverless functions and in containers with tight memory limits.
  • Thresholds are a trade-off. A higher score threshold means fewer false positives and more leaks. Pick it with real prompts from your app, not with the examples in a model card.

Approach 3: Presidio as a sidecar

Microsoft Presidio is the most widely used open-source PII toolkit. It is Python, but it ships as two HTTP services, so Node can call it like any other API.

docker run -d -p 5002:3000 mcr.microsoft.com/presidio-analyzer:latest
docker run -d -p 5001:3000 mcr.microsoft.com/presidio-anonymizer:latest

The analyzer returns character offsets, which makes the replacement step cleaner than with raw NER output:

// presidio-redact.ts
import { tokenFor, type Vault } from "./vault";

type Finding = { entity_type: string; start: number; end: number; score: number };

export async function redactPresidio(text: string, vault: Vault): Promise<string> {
  const res = await fetch("http://localhost:5002/analyze", {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ text, language: "en", score_threshold: 0.5 }),
  });
  if (!res.ok) throw new Error(`Presidio analyzer returned ${res.status}`);
  const findings = (await res.json()) as Finding[];

  // Replace from the end so earlier offsets stay valid; skip overlaps.
  let out = text;
  let lastStart = Infinity;
  for (const f of findings.sort((a, b) => b.start - a.start)) {
    if (f.end > lastStart) continue;
    const value = text.slice(f.start, f.end);
    out = out.slice(0, f.start) + tokenFor(vault, f.entity_type, value) + out.slice(f.end);
    lastStart = f.start;
  }
  return out;
}

We skip the anonymizer service here and build our own reversible tokens from the analyzer's offsets. Presidio's anonymizer can do reversible encryption with its encrypt operator and /deanonymize endpoint, but encrypted blobs are long, opaque strings that models handle worse than short, typed placeholders.

What you get: a mature detection engine with dozens of recognizers, context words, checksums, and a large community.

What you take on: a Python service to deploy, scale and monitor next to your Node app, an extra network hop on every LLM call, and language setup. The default analyzer is configured for English; German or French text needs additional NLP models and configuration. We wrote about where Presidio stops in why Presidio isn't enough for PII redaction.

The part everyone skips: restoring values in a stream

All three approaches so far end with a masked prompt. Now the model answers, and its answer contains your placeholders. For a non-streaming call, restore() from the vault is enough:

const reply = restore(completion.choices[0].message.content ?? "", vault);

Streaming is harder. The model generates text in small chunks, and a placeholder can be split across them: one chunk ends with [EMA, the next starts with IL_1]. Restore each chunk on its own and both halves pass through unchanged, so your user sees a broken placeholder.

The fix is to hold back anything that might be the start of a placeholder until you know whether it is one:

// stream-restore.ts
import { restore, type Vault } from "./vault";

const MAX_TOKEN_LENGTH = 32;

export function createStreamRestorer(vault: Vault) {
  let buffer = "";
  return {
    push(chunk: string): string {
      buffer += chunk;
      const open = buffer.lastIndexOf("[");
      const pending =
        open !== -1 && !buffer.slice(open).includes("]") && buffer.length - open <= MAX_TOKEN_LENGTH
          ? buffer.slice(open)
          : "";
      const ready = buffer.slice(0, buffer.length - pending.length);
      buffer = pending;
      return restore(ready, vault);
    },
    flush(): string {
      const rest = restore(buffer, vault);
      buffer = "";
      return rest;
    },
  };
}
import OpenAI from "openai";
import { redactRegex } from "./regex-redact";
import { createStreamRestorer } from "./stream-restore";

const openai = new OpenAI();
const vault = new Map<string, string>();

const stream = await openai.chat.completions.create({
  model: process.env.MODEL!,
  stream: true,
  messages: [{ role: "user", content: redactRegex(userMessage, vault) }],
});

const restorer = createStreamRestorer(vault);
for await (const part of stream) {
  process.stdout.write(restorer.push(part.choices[0]?.delta?.content ?? ""));
}
process.stdout.write(restorer.flush());

Two more edge cases to plan for:

  • Tool calls. If the model passes a placeholder into a tool call, the restored value lands inside a JSON string. A name with a quote or a backslash in it will break the JSON unless you escape it.
  • Multiple output channels. Reasoning text, several choices and parallel tool calls each stream separately. A buffer per channel avoids splicing one channel's half-token onto another's.

Approach 4: A gateway on the network path

The fourth option moves redaction out of your application entirely. A gateway sits between your app and the provider, redacts every outgoing request, and restores every response, streaming included.

The difference is less about detection quality and more about where the control lives:

  • Coverage. Code-level redaction only protects the calls that remember to use it. A new service, a quick script or a coding agent calling the API directly bypasses it. A gateway protects every request that goes through it.
  • One policy. Detection rules, thresholds and custom patterns live in one place instead of being copied across services and languages.
  • Audit trail. You can show what was detected and replaced, per request, without adding logging to every call site.
  • No per-process models. NER runs on the gateway, not in every Node process.

The trade-off is one more component in the request path, and trusting whoever operates it with your raw prompts. That is why hosting location and data retention matter so much when you choose one.

Comparison

Regex in codeTransformers.js NERPresidio sidecarGateway
Structured data (emails, IBANs, keys)GoodPair with regexGoodGood
Names, addresses, organisationsPoorDepends on the modelGood in EnglishDepends on the gateway's models
Non-English textPatterns onlyNeeds a multilingual modelNeeds extra setupDepends on the gateway
Restore in responsesYou build itYou build itYou build itBuilt in
Streaming restoreYou build itYou build itYou build itBuilt in
Added latencyMicrosecondsCPU inference per requestNetwork hop plus inferenceNetwork hop plus inference
Runs whereYour processYour processA Python service you operateA service you or a vendor operates
Covers calls you forgotNoNoNoYes

A pragmatic path for many teams: start with regex today, because it costs nothing and stops the worst leaks (keys, emails, card numbers). Then decide whether you want to own an NER stack yourself or put it on the network path.

How Grepture helps

Grepture is an open-source AI gateway, and approach 4 is what it does. You keep your OpenAI or Anthropic client and change how it is constructed:

import OpenAI from "openai";
import { Grepture } from "@grepture/sdk";

const grepture = new Grepture({
  apiKey: process.env.GREPTURE_API_KEY!,
  proxyUrl: "https://proxy.grepture.com",
  tokenScope: conversationId, // same value, same token for the whole conversation
});

const openai = new OpenAI(
  grepture.clientOptions({
    apiKey: process.env.OPENAI_API_KEY!,
    baseURL: "https://api.openai.com/v1",
  }),
);

From there, every request is scanned with regex patterns for structured data and locally hosted NER models for names, organisations and addresses. Detection runs on Grepture's own servers in the EU, so no prompt text is sent to another AI service to find the PII. Values are replaced with tokens before the request leaves, and restored in the response, including streamed responses where a token is split across chunks and tool-call arguments where the restored value has to be JSON-escaped.

With tokenScope set to a conversation ID, the same value keeps the same token across turns, which keeps multi-turn context and provider prompt caching intact. Zero-data mode keeps request bodies off disk entirely. For calls that are not OpenAI-compatible, grepture.fetch() is a drop-in replacement for fetch. The OpenAI redaction guide walks through the setup step by step, and there is a free tier of 1,000 requests a month if you want to try it on your own prompts.

Redact PII before it reaches the model.

Grepture is an open-source, EU-hosted AI gateway. Names, emails and secrets are replaced on the way out and restored in the response. Free for 1,000 requests a month.

Start free