Anonymization protects privacy by removing or replacing identifying information before chat histories are stored, logged, analyzed, or sent to external LLMs. It reduces risks such as identity theft, financial fraud, and privacy violations while supporting data-protection compliance. section 9.5 section 9.5.1
A production-minded implementation should:
- Redact incoming message content before storing it in message history or forwarding it to an LLM. section 9.5.1
- Place anonymization in middleware when possible, so requests cannot bypass the filter. section 9.5.1
- Use stronger PII detection for production, non-English data, or sensitive applications. section 9.5.1
- Combine anonymization with retention policies, access control, logging, auditing, and provider privacy evaluation. section 9.5 section 9.6.2
Redacting prompt input
Install and initialize redact-pii, then pass validated user text through a SyncRedactor before the remaining API logic runs. A message containing a name, email address, and phone number can become PERSON_NAME, EMAIL_ADDRESS, and PHONE_NUMBER. The LLM should receive only the redacted input. section 9.5.1
Listing 9.7 Anonymizing prompt Input data
import { SyncRedactor } from 'redact-pii';
const redactor = new SyncRedactor();
function anonymizeText(text) {
return redactor.redact(text); #1
}
export async function POST(req) {
try {
const body = await req.json();
const validatedData = promptSchema.parse(body);
const { userId } = await auth();
if (!userId) {
return new Response('Unauthorized', { status: 401 });
}
const anonymizedInput =
anonymizeText(validatedData.text); #2
// rest of the code
}
}
Shows the JavaScript API-handler step that converts validated prompt text into anonymized input. section 9.5.1





