The mistake: trusting the model to “behave”
The first time I tried to use generated personas in a survey system, I gave the model a dead-simple setup: “You’re a janitor, you make 15k a year.” Then I lobbed the easiest question possible: “What’s your job title?”
It replied: “CEO.”
That was the whole lesson in one response. An LLM is not “role-playing” in any meaningful, trustworthy way. It is trying to spit out likely tokens in the bucket labeled “job title,” and both “janitor” and “CEO” live in that bucket. It is not lying, and it is definitely not “respecting your persona system.” It is just predicting text. arxiv
If you hook that thing up to real user data and assume persona prompts and fine-tuned vibes will enforce your security and compliance rules, you’re building a fence out of wet paper.
You need something in front of the model whose only job is: “Should this request be allowed, and under what constraints?” That is the LLM proxy.
The architecture pattern: client → proxy → model (and tools)
The basic shape looks like this:
- Client: your app, your users, your admin UI, your “agent” frontends.
- Proxy: the enforcement layer.
- LLM / MCP host: the model plus whatever tool stack you’ve wired in (databases, ticketing, email, internal APIs). docs.litellm
The proxy is where you answer questions like:
- Is this prompt allowed at all?
- Does it contain PII/PHI/PCI?
- Which tenant is this, and what are their rules?
- Which region / jurisdiction applies to this request? c-sharpcorner
- Are we allowed to send this specific prompt to this specific model provider?
- Are we allowed to call that tool with those parameters for this user?
If you skip the proxy, you’re effectively letting “CEO Janitor” run your security program. The model happily mixes “dump the database” and “answer the user’s question” in the same space of “things that sound helpful.” petronellatech
What the proxy actually does
1. Input classification
First step is tagging the request. It doesn’t have to be perfect to be useful.
You want tags like:
contains_piicontains_phicontains_pcitenant_idjurisdiction/allowed_regionssprinto
Even a naive classifier or a couple of lookups from your auth context is better than nothing. Once you have tags, you can write actual policy instead of giant if statements scattered across services.
2. Catch the obvious stuff with rules
Regex and simple rules get you surprisingly far:
- SSNs with sanity checks so you don’t flag every nine-digit number.
- Credit cards with a Luhn check. protecto
- Phone numbers.
- Email addresses.
- Parts of addresses or obvious ID formats. dev
These are fast, cheap, and easy to explain. When legal or security asks “Why did we block this?”, you can point at the exact pattern instead of shrugging at a model output.
3. Fuzzy PII detection with ML / LLM (optional)
Once you outgrow rules, you can layer on a classifier. That can be:
- A small in-house model.
- A separate LLM call with a prompt like: “Does this text contain personal identifiers? Return JSON with categories.” python.useinstructor
Do this before the main model call and, if you care about data boundaries, don’t use the same external provider you’re trying to keep data away from. Otherwise your “privacy filter” is just another path where raw data leaks out while you congratulate yourself on being compliant. datasunrise
4. Redaction strategies
You’ve basically got three knobs:
- Mask in place:
**** **** **** 1234for display cases that need the last few digits. - Tokenization: swap sensitive spans with placeholders like
{{token:abc123}}and keep the mapping inside your own vault. protecto - Full drop: reject the request or strip the field entirely when something should never leave your boundary.
Most real systems end up mixing all three. Cards might get tokenized, SSNs might be hard-blocked, emails might be masked except on internal routes.
5. Observability and audit tags
You want to log the fact that sensitive data was there, not the data itself.
Example audit record:
request_idtenant_idmodelroutecontains_pci=trueaction=redactpatterns_matched=["cc_luhn","email"]datasunrise
This makes incident reviews bearable. You can actually answer “Did this leave our boundary? What rule fired?” instead of playing forensic guessing games across log streams.
Where to enforce: three choke points
You get three main choke points:
- Incoming prompts (user → model).
- Tool parameters (model → tools).
- Outgoing responses (tools → model → user). startupsoft
People usually nail number one and kind of remember number three. Number two is where the fire starts.
If you give the model generic access to tools, sooner or later it will happily attempt:
SELECT * FROM patients;- “Export all customer emails to CSV.”
- “Dump the entire ticket history for tenant X.”
Not because it’s evil. Because that looks like a complete, helpful solution according to the training data. The model has no concept of “scope” unless you give it one with real guardrails. arxiv
This is where the old principle of least privilege shows up in a new outfit: tools should only have the minimum permissions needed to do the thing they’re meant to do, scoped to the current user, tenant, and context. If a “view invoice” tool can also quietly enumerate the entire customer table, you don’t have AI agents, you have a breach generator. nightfall
A concrete code walk: FastAPI middleware scrubbing JSON
Here’s a trimmed-down example of a JSON-scrubbing middleware in FastAPI. The point is the shape, not the exact framework details. fastapi.tiangolo
import re
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
SSN_RE = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")
EMAIL_RE = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b")
def mask_ssn(text: str) -> str:
return SSN_RE.sub("***-**-****", text)
def contains_pii(text: str) -> bool:
return bool(SSN_RE.search(text) or EMAIL_RE.search(text))
app = FastAPI()
@app.middleware("http")
async def pii_scrubber(request: Request, call_next):
body = await request.body()
text = body.decode("utf-8", errors="ignore")
tags = {
"contains_pii": contains_pii(text),
# Put tenant / jurisdiction here based on headers or auth context
}
# Example policy: block SSNs for external models
if SSN_RE.search(text) and request.headers.get("x-model-class") == "external":
return JSONResponse(
status_code=400,
content={"error": "SSN detected. Request blocked.", "tags": tags},
)
# Example policy: redact SSNs on a chat route
if request.url.path.startswith("/chat"):
text = mask_ssn(text)
# Here you’d rebuild the request with the scrubbed body
request.state.policy_tags = tags
response = await call_next(request)
response.headers["x-policy-tags"] = str(tags)
return response
Rebuilding the request object with the modified body is annoying and slightly framework-specific. You’ll need to read the docs or wrap your own request class. But once you’ve got this in place, you have a single, testable place to enforce rules before anything reaches the model. fastapi.tiangolo
Why this matters even more with “AI glue”
I’ve worked at a place that marketed “AI automation” and the core engine was spreadsheet column-mapping plus some string cleaning. That wasn’t a scam. That was the job. Glue code with opinions. datasunrise
LLM-based glue is the same thing, except now the glue can:
- Hit production databases.
- Call third-party APIs.
- Trigger customer-facing workflows.
- Move money.
If you bolt that onto production without a proxy, you’re not “shipping fast,” you’re hand-delivering your risk register to an autocomplete engine.
Put a proxy in front of the model. Scrub inputs. Scope and gate tool calls. Tag everything. Treat Model Context Protocol or any other tool-host system as behind a policy layer, not as the policy layer itself. docs.litellm
The model is great at predicting the next token. It is not your compliance team. It is not your security boundary. And if you ask it who’s in charge, it might still say “CEO Janitor.”
– Sethers