Detect PII and Secret Leaks in AI Agent Output With One Prompt
A reusable prompt to detect PII leaks in AI agent output before it reaches your logs or users. Copy the leak-scan output contract and run it on any model.
An AI agent that reads a support ticket, calls three tools, and writes a summary touches personal data at every hop. Somewhere in that chain it pastes a customer's Social Security number into a log line, or echoes an API key a tool returned, or writes an email address into a vector store as plain text. Nobody notices until an audit does. If you need to detect PII leaks in AI agent output before it reaches a user or a log, the fix isn't another dashboard. It's a scan step the agent runs on its own output.
Most of the tooling being sold for this is a library you pip install and wire into one runtime. That's fine if you run one Python service. It doesn't help the team whose agents span a Node gateway, a Claude tool call, and a Zapier step. A prompt does, because it runs wherever the model runs.
This walks through a reusable leak-scan prompt: what it checks, how the output contract locks its verdicts into a table you can act on, and where it beats a regex-only scanner.
Why agents leak data by default
An agent treats every string the same. A name, a card number, and a boilerplate greeting all flow through the same LLM call, the same trace log, and the same retrieval index. There's no built-in notion that one of those is a liability. The AIR Blackbox writeup on agent PII leaks makes the same point: agents leak by default because they don't classify data before moving it, and redaction has to be added as its own step.
The leaks cluster into a few shapes. A DEV Community post on what agents should never leak lists the usual suspects: PII in tool-call arguments, API keys sitting in agent context, financial data buried in a reasoning chain, and medical or legal text the agent wasn't supposed to repeat. Both pages then point you at their own SDK. Neither hands you a model-agnostic check you can paste into whatever stack you already run.
That's the gap. The detection logic is simple enough to express as a prompt. What's missing everywhere is the prompt.
What you can do with a leak-scan prompt
- Scan a single agent response before it renders to a user
- Gate tool-call arguments so an agent can't send an SSN to an external API
- Sweep a batch of trace logs to find historical leaks after the fact
- Flag high-entropy strings and known key prefixes as secrets, not just names
- Produce a redaction plan (
mask,hash,tokenize, ordrop) per finding - Feed a pass/fail signal into a CI gate for agent changes
Anatomy of the scan prompt
The whole thing is a role, a triple-fenced input, and an output contract that forces a table instead of a paragraph. Structure matters here: a scanner that answers in prose is useless downstream because you can't diff it or route on it.
Role: You are a data-leak scanner for AI agent output.
Input:
"""
{{agent_output}}
"""
Context: {{data_policy}} # what counts as sensitive here
Output contract (return this exact shape, no preamble):
| span | category | severity | why | redaction |
|------|----------|----------|-----|-----------|
- category ∈ {PII, SECRET, FINANCIAL, HEALTH, CLEAN}
- severity ∈ {high, medium, low}
- redaction ∈ {mask, hash, tokenize, drop, none}
Final line: VERDICT: PASS or VERDICT: FAIL
Feed it the agent's raw output in {{agent_output}} and your definition of sensitive in {{data_policy}}. It returns one row per finding, ending with a machine-readable verdict a pipeline can branch on. No "Here's what turned up" wrapper, because the last learning most teams hit is that a prose preamble breaks the parser.
Regex-only scanning is security theater for agents. It nails an SSN or a 16-digit card, and it sails right past "the patient, Maria Alvarez, was prescribed…" because there's no pattern to match a name in free text. Run a pattern pass for structured identifiers and a semantic pass for the rest. The prompt does the semantic half; keep the deterministic regex for card and account numbers where a model's recall is worse than a rule's.
How the models differ on this job
Behavior isn't uniform, so don't write the prompt once and assume it ports. Claude honors a ## Output format heading and the VERDICT: final line reliably across long inputs, which matters because agent outputs you're scanning can be long. GPT-4o drifts on long inputs unless the table schema is restated on the final line of the prompt, right before the pasted output ends, so the contract stays in its recent-token window. Gemini tends to over-flag, marking generic first names as high-severity PII; tightening {{data_policy}} with an explicit allowlist of non-sensitive tokens calms it down.
None of the vendor pages show how to detect PII leaks in AI agent output across models. Both the AIR Blackbox and DEV posts treat detection as a single library call with no model-by-model story, because their answer is their SDK, not a prompt you'd run on three different models.
Prompt-craft patterns that make it hold
Put the contract last. Models weight recent tokens. A scan prompt that opens with the table spec and then pastes 4,000 tokens of agent output will forget the spec. Paste the output first, restate the table shape immediately after, and the verdict format stays locked.
Separate detection from redaction. Ask the model to name the leak and propose an action, but don't ask it to rewrite the text in the same pass. A model that redacts inline will quietly paraphrase clean sentences too, and now you can't tell what changed. Two prompts, or two clearly-fenced sections, keep the audit clean.
Define "clean" explicitly. If CLEAN isn't in the category set, the model invents a leak to justify its existence. Give it a way to say nothing's wrong, or every scan returns a false positive.
Variables you'll set
| Variable | Required | What it is |
|---|---|---|
{{agent_output}} | Yes | The raw text the agent emitted (response, tool-call args, or a log line) |
{{data_policy}} | Yes | What counts as sensitive in your context, plus any non-sensitive allowlist |
{{secret_patterns}} | No | Known key prefixes and token formats to always flag as SECRET |
Getting started
- Grab the last 20 responses your agent produced and paste one into
{{agent_output}}. - Write a two-line
{{data_policy}}: what's sensitive, what's explicitly fine. - Run the scan on Claude and on GPT-4o; compare the tables.
- Wire the
VERDICT: FAILline into your pre-render or pre-log step. - Keep a deterministic regex pass in front for card and account numbers.
- Re-scan a batch of old logs to find leaks you've already shipped.
Once the scan runs clean on live output, the same prompt doubles as a gate on tool-call arguments, which is where the worst leaks happen. An agent that emails a summary is one bad tool call away from mailing a customer's data to an external endpoint. This is a cousin of two jobs the catalog already covers: the Agent Commit Security Harness checks what an agent tries to commit, and the scan here checks what it tries to say.
Browse the AI agent security packs →The Agent PII & Secret Leak Scanner Pack ships this end-to-end: the {{agent_output}} and {{data_policy}} variables feed a scan prompt with the exact leak-table contract above, plus a companion redaction prompt and a batch-log sweep variant so you're not rebuilding the pattern for every surface. It's part of The Complete AI Prompts Bundle, a one-time lifetime license to the whole catalog and every pack added later, worth it if you run more than one of these agent-safety jobs.
A leak that a model catches on the way out is cheap. The same leak found in a breach notice isn't. If you're hardening agents, pair this with the runtime side of the problem in prompt injection defense for AI agents, and with the source-side check in scanning commits for secrets. For the connected-tool version, the Prompt Injection Defense Harness covers the input half of the same threat.
Start with the leak scanner →Common questions
How do you detect PII leaks in AI agent output?
What's the difference between scanning agent output and scanning commits for secrets?
Can a prompt catch API keys and tokens, not just PII?
Get the prompt packs this guide is built on
Ready-to-paste prompts with documented variables and usage guides for ChatGPT, Claude, and Gemini. One-time payment, own it forever.
More prompt guides

A Least-Privilege Audit Prompt for Your AI Agent's Tools
Your agent needs to read one config file. Six months and four features later, it holds write access to the whole repo, a database token that can drop tables, and shell execution — because each was eas…

An Over-Refusal Audit Prompt for LLMs That Say No Too Often
A user asks your assistant how to kill a background process that's eating CPU. The model refuses, citing a policy against violence. Nobody got hurt, but the user's trust just took a hit, and that's a…

An LLM Toxicity and Brand Safety Audit Prompt You Can Actually Run
Your support assistant answers a frustrated customer. The reply is technically correct, faintly sarcastic, and name-drops a competitor in a way legal would hate. Nothing in it is slur-level toxic, so…