{"slug": "the-layers-i-put-in-front-of-agents", "title": "The layers I put in front of agents", "summary": "A developer built BleedStop, a layered prompt-injection defence that stacks a phrase-list filter, a DeBERTa classifier served via FastAPI, and an optional 8-bit ONNX sidecar in front of agents and an MCP gateway. On a held-out set of 80, the classifier caught 77.5% of injections at a 5% false positive rate, while the two-stage pipeline reached 80.0% at the same 5.0% rate; the phrase list alone managed only 22.5% on that set despite scoring 96.4% on the dev data it was tuned on. Classifier latency ran about 46-49 ms p50 and 55-132 ms p99 on four CPU threads, with a configurable fail-open or fail-closed mode when the sidecar is unavailable.", "body_md": "Prompt injection defence in BleedStop is a stack of checks in front of the model, not one classifier score. The default check is a phrase list. A DeBERTa classifier is a second stage I turn on, and on my held out set it caught 77.5% of injections at a 5% false positive rate.\n\nEverything in this post is from [the BleedStop case study](https://osamamalik.me/bleedstop.html) or from the public repo, [usmamalik345/bleedstop](https://github.com/usmamalik345/bleedstop), especially the README and `classifier/server.py`. The spend guard around these checks is in the [BleedStop deep dive](https://osamamalik.me/blog/bleedstop-cost-and-security-guard.html).\n\nThe phrase list is the default detector. The README says the ONNX sidecar runs only when `BLEEDSTOP_DETECTOR` is `classifier` or `both`. Until that flag is set, the live demo's server check is the phrase list. The model that runs in the browser on the demo page is a demo only. It does not protect the API.\n\nOn the phrase list, the honest held out number is weak. The README's agent guard row calls the held out set 22.5%, with 0% false positives, and calls that the honest number. A later note in the same README, phrase list only, cutoff 0.6, normaliser off, is the same rate as a fraction: 9 of 40 on the held out set (N=80, false positive rate 0 of 40). Dev, which the regex was tuned on, is 53 of 55 (N=112, benign 57, false positive rate 0 of 57), which is the 96.4% in the summary table. Lowering the cutoff to 0.3 did not raise dev recall and raised the dev false positive rate to 1 of 57, so the cutoff stayed at 0.6. Turning normalisation on did not raise dev recall. `BLEEDSTOP_NORMALIZE` stays off.\n\nThat is why I do not lead with 96.4%. That figure is the set the phrase list was tuned on.\n\n`classifier/server.py` serves `protectai/deberta-v3-base-prompt-injection-v2` with FastAPI. With `CLASSIFIER_URL` set, `/agent/precheck` and the MCP gateway run the regex first, then the classifier. There is also a small ONNX sidecar under `services/classifier/`, on CPU, the same 8 bit model the browser demo can load. The case study describes that browser build as a DeBERTa classifier quantized to 8 bit.\n\nMode `both` runs the phrase list first and sends only text the phrase list allowed on to `POST /classify`. The timeout is `BLEEDSTOP_CLASSIFIER_TIMEOUT_MS`, default 800. If the sidecar is down or slow, `BLEEDSTOP_CLASSIFIER_FAILMODE=open` is the default: keep the phrase list verdict, log a warning, and increment `bleedstop_classifier_errors_total`. `closed` turns that outage into a block. That is safer against injection, and it also means a dead sidecar stops those calls.\n\nThe README's comparison, which is the table I trust for this post, is:\n\n| Set | Phrase list recall / false positive rate | Classifier recall / false positive rate | Two stage recall / false positive rate | \n|---|---|---|---|\n| deepset/prompt-injections, all 662 | 4.2% / 0% | 41.4% / 1.0% | 41.4% / 1.0% | \n| BleedStop held out, 80 | 22.5% / 0% | 77.5% / 5.0% | 80.0% / 5.0% | \n| BleedStop dev (phrase list tuned on it), 112 | 96.4% / 0% | 83.6% / 7.0% | 96.4% / 7.0% | \n\nThe 77.5% at a 5% false positive rate on the case study is this classifier row, held out, N=80. It is not the phrase list. Two stage recall on that set is 80.0% at the same 5.0% false positive rate, slightly higher than the classifier alone.\n\nLatency in the README, on 4 CPU threads: phrase list p50 about 9 microseconds; classifier p50 about 46 to 49 ms, p99 55 to 132 ms. The README's caveats, which I share: deepset includes German rows and the model is English only; training overlap with public sets cannot be ruled out; the PINT row in the report is the public 8 row example file, not a PINT score; Llama Prompt Guard 2 is gated and I did not evaluate it.\n\nThe browser build is the same DeBERTa v2 model in the visitor's browser with onnxruntime-web, an 8 bit weight only build (256 MB, served from GitHub Pages, loaded only on click). Against the fp32 ONNX it gave identical labels on the held out, dev and deepset test sets. Single threaded inference was p50 245 ms in headless Chrome on the build box. That check is still a demo. Server side enforcement on the live demo remains the phrase list until `BLEEDSTOP_DETECTOR` is set. The README says free Docker hosting for the sidecar was not available, because Hugging Face Docker Spaces need a PRO plan, which is why the demo can run the model in the browser instead.\n\nA detector score does not spend money. `POST /decide` still returns ALLOW, LIMIT, CHALLENGE or DENY from the rules, and Redis still enforces the budget. The golden set on the case study is a different measurement from the injection table: 12,140 labeled requests, precision 100%, benign false positive rate 0.0%, recall 48.0%. The golden set and the rules share an author, so I do not call that a field result.\n\nOn the MCP path, the README says every `tools/call` goes through, in order: a definition pin (trust on first use, sha256 over name, description, inputSchema and annotations), a tool description poisoning scan, a per tenant Cedar policy in `config/mcp-policy.cedar`, the agent injection guard on the arguments, then the normal budget decision. Tool results are scanned for indirect injection. `MCP_GATEWAY_MODE=shadow` logs without blocking. The threat mapping is in `docs/THREAT-MODEL.md`.\n\nFor `npm run evals:mcp`, 70 hand written tool definitions, the README reports poisoning scan precision 100%, recall 80% (20 of 25), false positive rate 0% (0 of 45). Rug pull detection caught 180 of 180 mutations, with 0 false alarms on 45 unchanged lists. Added latency per `tools/call` over HTTP, n=2000, was p50 2.21 ms, p95 2.84 ms, p99 3.89 ms. The misses are the 5 of 25 poisonings the README calls subtle and polite, with no trigger phrase. I wrote that eval set, so I treat the recall as optimistic. The README says the same.\n\n`BLEEDSTOP_DETECTOR` is set.\n[BleedStop repo](https://github.com/usmamalik345/bleedstop) · [Case study](https://osamamalik.me/bleedstop.html) · [Cost guard deep dive](https://osamamalik.me/blog/bleedstop-cost-and-security-guard.html) · [Hire me](https://osamamalik.me/hire/)\n\nOriginally published at [https://osamamalik.me/blog/prompt-injection-defence.html](https://osamamalik.me/blog/prompt-injection-defence.html)", "url": "https://wpnews.pro/news/the-layers-i-put-in-front-of-agents", "canonical_source": "https://dev.to/osamamalik/the-layers-i-put-in-front-of-agents-1ha7", "published_at": "2026-11-19 06:09:08+00:00", "updated_at": "2026-11-19 06:11:51.274580+00:00", "lang": "en", "topics": ["ai-safety", "ai-agents", "ai-tools", "large-language-models", "mlops"], "entities": ["BleedStop", "DeBERTa", "protectai/deberta-v3-base-prompt-injection-v2", "FastAPI", "ONNX", "onnxruntime-web", "deepset/prompt-injections", "Llama Prompt Guard 2"], "also_reported_by": [], "takeaway": "A two-stage phrase-list-then-DeBERTa pipeline hit 80.0% recall at 5.0% false positives on an 80-example held-out set, versus 77.5% for the classifier alone and 22.5% for the phrase list alone.", "recommendation": {"advice": "Layer a cheap phrase list ahead of a DeBERTa classifier and report held-out recall at a fixed false positive rate, since the tuned dev set overstated phrase-list recall at 96.4% versus 22.5% held out.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/the-layers-i-put-in-front-of-agents", "markdown": "https://wpnews.pro/news/the-layers-i-put-in-front-of-agents.md", "text": "https://wpnews.pro/news/the-layers-i-put-in-front-of-agents.txt", "jsonld": "https://wpnews.pro/news/the-layers-i-put-in-front-of-agents.jsonld"}}