ReasonGate

CI

Python

License

Core deps

An explainable security gate for LLM applications. Every decision carries a reason you can audit.

See it prevent a real breach not just flag a bad string

A bank support agent has tools (send_email, transfer_funds) and is handed a customer

record with a hidden instruction inside it (indirect injection the dominant attack on

RAG / agents). Same attack, one variable: the shield.

Stakes demo — Shield OFF: the customer record is exfiltrated and $84,200 is wired out; Shield ON: the same attack is blocked before the model is called

The proof isn't the agent's words it's the side effects that did not happen. Run it

yourself (deterministic, no API key needed); it's a CI-enforced invariant, not a screenshot:

▶ Try the live demo — paste a prompt, watch it get blocked with a reason and an auditable record

Try the live demo

See it block a direct attack or a

hidden, zero-width-obfuscated one — runs on the

zero-dependency core, no API keys, no data leaves the server.

block a direct attack

hidden, zero-width-obfuscated one

Prompt injection is the top item on the OWASP LLM Top 10 for a structural reason: a language model reads instructions and data through the same channel and cannot reliably tell them apart. You do not fix that inside the model. You put a gate in front of it.

OWASP LLM Top 10

Most gates are black boxes — a confidence score and a yes/no. That is not good enough for anyone who has to defend a decision to a security team, an auditor, or a regulator. ReasonGate blocks the attack and tells you which signal fired, what it matched, and the closest known attack it resembles. A block you cannot explain is a block you cannot ship.

ReasonGate is model-agnostic. It wraps any prompt -> str function OpenAI, Anthropic, a local model, your own RAG pipeline and inspects three surfaces: the user prompt, the retrieved context, and the model's output.

The core (rule, normalization, indirect-injection and leakage detectors) is pure Python with zero dependencies.

Architecture: open core + enterprise add-on

The open core is rule-only and self-contained. It exposes a stable Detector

interface and a plugin seam (reasongate.registry, entry point groups

reasongate.detectors / reasongate.provenance). Installing the separate

reasongate-enterprise add-on auto-enables the embedding-based ML detector

and the provenance detector the core needs no code change, and every decision's

ShieldResult.layers shows which layers ran (["injection", "normalization"] vs

+["ml_injection", "provenance"]). With nothing installed, the core runs rule-only,

silently. The methodology, thresholds, and reproducible benchmark harness (eval/,

RESULTS.md) stay in this repo; the trained model and ML/provenance code

ship in the add-on.

RESULTS.md

Defense in layers

A single detector is a single point of failure. ReasonGate runs a stack, and the policy engine fuses their signals before deciding.

What each layer is for:

Normalization / deobfuscation. Strips the tricks attackers use to slip past pattern matching — zero-width characters, Cyrillic homoglyphs, leetspeak (1gn0re), spaced and dotted letters (i.g.n.o.r.e), base64 payloads. Without this, every downstream detector is trivially bypassed.

Injection / jailbreak detection. A rule layer for known patterns and an optional ML layer (embeddings → soft decision tree) for novel phrasings.

Indirect injection. Scans retrieved documents and tool output before they reach the model — the dominant attack vector for RAG and agentic systems, where the malicious instruction lives in the data, not the user's message.

Multi-turn. A stateful session shield that accumulates risk across turns, so a crescendo attack that looks innocent one message at a time still trips the gate.

Output leakage + canary. Catches secrets and PII on the way out. A canary token planted in the system prompt makes a system-prompt leak provable rather than guessed.

The policy engine combines these with a calibrated noisy-OR: several weak signals add up to a block, while isolated noise from a legitimate prompt does not.

Benchmarks

I measure honestly held-out splits, cross-validation, an out-of-distribution set, and significance tests. Full methodology and caveats are in RESULTS.md.

RESULTS.md

ML detector (VoyageAI embeddings → soft decision tree, threshold tuned recall-first):

Data: deepset/prompt-injections, jackhhao/jailbreak-classification, xTRam1/safe-guard-prompt-injection.

Evasion robustness recall when each attack is obfuscated. The attacker-side obfuscators are written independently of the defense, so the gate cannot cheat by sharing code with what attacks it:

Two findings worth stating plainly: an earlier model trained on synthetic data scored 0.98 F1, but an ablation showed punctuation and casing alone reached 0.96 the score was an artifact of the data generator, and the explainable classifier is what surfaced it. And the out-of-distribution drop (0.97 → 0.88) is the real generalization number; it degrades but does not collapse.

Quick start

Scanning retrieved context before it reaches the model:

Multi-turn sessions and the embedding-based detector:

Auditable decisions

explain() is for humans. For a SOC, SIEM, or a compliance trail, every decision

also serializes to a structured, machine-readable record with a unique

decision_id, a UTC timestamp, the action, the deciding risk score, and the full

per-detector evidence:

Wire decisions into your logging once, and every call is recorded automatically:

The audit hook can never break the gate: if your sink raises, the security

decision is still returned and the error is reported on a separate channel.

scan_input, scan_context, scan_output emit one record each; protect emits

exactly one record per request.

Runs air-gapped

The core — rule, normalization, indirect-injection and leakage detectors, the

policy engine, and the full audit/serialization layer is pure Python with zero

dependencies and makes no network calls. It installs and runs on an isolated or

classified network with nothing to phone home. (The optional [ml] detector adds

semantic recall via an embedding model; the default cloud embedding makes an API

call per request, so run core only where data sovereignty is a requirement. An

on-prem embedding option that keeps the ML path fully local is on the roadmap.)

Install options

Reproduce the evaluation

Known limits

I would rather you know these up front than discover them in production.

No guardrail catches everything. Recall runs %76 - %96 depending on distribution and obfuscation; it is never 100%. Run it as one layer, with the model's own safety training behind it.

It is strongest on the attack families it has seen. Genuinely novel ones perform worse until added to training.

The ML detector calls an embedding API per request budget for the cost and latency, or run core-only.

The default is recall-first, which costs some false positives. Tune the threshold to your tolerance.

License

Apache-2.0 — see LICENSE. (Includes a patent grant; the enterprise

add-on is separately licensed.)

LICENSE