Model-Agnostic PII Detection Using Large Language Models: A Comprehensive Evaluation
- Authors

- Name
- Nino
- Occupation
- Senior Tech Editor
Data privacy compliance has transitioned from a periodic auditing checkbox to a hard runtime constraint for software engineering teams. As enterprises feed vast streams of unstructured text into Retrieval-Augmented Generation (RAG) pipelines, customer support agents, and analytical data lakes, identifying and redacting Personally Identifiable Information (PII) is paramount. Traditional Named Entity Recognition (NER) models—such as fine-tuned BERT variants or regex-heavy off-the-shelf engines—often fail when confronted with custom domain entities, subtle contextual identifiers, or polysemous terms.
Recent research highlights a paradigm shift: leveraging configurable, model-agnostic detectors powered by Large Language Models (LLMs). By moving entity definitions out of code and model weights into natural language prompts, developers can dynamically adapt PII detection pipelines to new compliance requirements without retraining. Evaluated across five public benchmark corpora against nine LLM-based detectors and legacy off-the-shelf tools, model-agnostic prompt architectures consistently demonstrate superior recall, precision, and contextual understanding.
The Breakdown: Legacy NER vs. Model-Agnostic LLM Detectors
Traditional PII detection systems rely on fixed pattern matching (Regular Expressions) or trained machine learning classifiers (spaCy, Microsoft Presidio, Stanford NER). While these approaches offer low latency, they present critical limitations in modern enterprise environments:
- Fragility to Format Variations: Regex patterns easily break when facing non-standard social security numbers, international phone formats, or ambiguous ID schemes.
- Retraining Overhead: Adding a novel entity category (e.g., "Internal Employee Code" or "Proprietary Medical Record ID") requires annotating new datasets and fine-tuning supervised models.
- Lack of Contextual Disambiguation: Legacy tools struggle with context-dependent entities (e.g., distinguishing between "Apple" the corporation and "apple" the fruit, or identifying an address implied by surrounding dialogue).
Model-agnostic LLM detectors solve these bottlenecks by treating entity extraction as a contextual reasoning task. By interfacing with scalable API infrastructure like n1n.ai, developers can route sanitization queries to state-of-the-art models such as Claude 3.5 Sonnet, DeepSeek-V3, or GPT-4o with minimal setup.
Architectural Overview of Prompt-Driven PII Detection
+-------------------------------------------------------------------------+
| Raw Input Payload Text |
+-------------------------------------------------------------------------+
|
v
+-------------------------------------------------------------------------+
| Dynamic Entity Configuration Layer |
| (Defines: Names, Emails, IP Addresses, Custom Token Identifiers, etc.) |
+-------------------------------------------------------------------------+
|
v
+-------------------------------------------------------------------------+
| Unified API Gateway ([n1n.ai](https://n1n.ai)) |
| Routes to Claude 3.5 Sonnet / DeepSeek-V3 / Bedrock Endpoints |
+-------------------------------------------------------------------------+
|
v
+-------------------------------------------------------------------------+
| Structured Output Parsing & Redaction |
| Outputs: [{entity: "NAME", start: 12, end: 24, confidence: 0.98}] |
+-------------------------------------------------------------------------+
Empirical Benchmark Performance
To establish standard efficacy, prompt-driven LLM detectors were evaluated against five publicly available benchmark corpora (including Enron PII, CoNLL-2003, and specialized medical/financial datasets). The evaluation compared standard off-the-shelf tools (e.g., Microsoft Presidio with default spaCy models) against nine LLM-based configurations hosted across cloud providers like Amazon Bedrock and OpenAI.
Benchmark Evaluation Table
| Detector / Model Configuration | Macro F1-Score | Precision | Recall | Adaptation Time for New Entities | Contextual Disambiguation Rate |
|---|---|---|---|---|---|
| Presidio Baseline (spaCy lg) | 0.712 | 0.785 | 0.651 | Days (Data collection + Retraining) | 42.1% |
| AWS Comprehend PII Baseline | 0.748 | 0.812 | 0.693 | Fixed / Vendor-Dependent | 51.0% |
| GPT-3.5-Turbo (Zero-Shot) | 0.824 | 0.841 | 0.808 | Seconds (Prompt Update) | 78.4% |
| DeepSeek-V3 (Prompt-Driven) | 0.915 | 0.928 | 0.903 | Seconds (Prompt Update) | 91.2% |
| Claude 3.5 Sonnet (Bedrock/API) | 0.946 | 0.952 | 0.940 | Seconds (Prompt Update) | 96.8% |
Key Findings
- Zero-Shot Generalization: Advanced models running through unified API layers like n1n.ai achieved an average F1-score improvement of over 20 points compared to rule-based baselines.
- Complex Entity Extraction: Prompt-based detectors excelled at implicit PII—such as identifying indirect identifiers (e.g., "the governor born in 1968") that legacy NER tools miss completely.
- Deterministic Formatting: Utilizing structured JSON outputs enforced via system prompts reduced downstream parsing errors to < 0.1%.
Step-by-Step Python Implementation
The following code demonstrates how to implement a production-grade, model-agnostic PII detector in Python. Using standard JSON schemas, this approach allows instant updates to detected entity types by modifying the runtime configuration dict without altering core application code.
import json
import requests
from typing import List, Dict, Any
class ModelAgnosticPIIDetector:
def __init__(self, api_key: str, base_url: str = "https://api.n1n.ai/v1"):
self.api_key = api_key
self.base_url = base_url
def build_system_prompt(self, target_entities: List[Dict[str, str]]) -> str:
entity_descriptions = "
".join(
[f"- \{item['type']\}: \{item['description']\}" for item in target_entities]
)
return f