guardrail-rs
A small Rust proxy that checks LLM requests before they leave your network.
Your app sends its chat requests to guardrail-rs instead of the LLM provider. guardrail-rs runs a few checks, then forwards the request, cleans it up, or refuses it.
your app ──► guardrail-rs ──► OpenAI / Anthropic / Azure / any OpenAI-compatible API
│
├─ looks like prompt injection? refuse it (403)
├─ contains emails, cards, keys? replace them with [EMAIL] etc.
└─ breaks one of your rules? refuse it (403)
Status: experimental. On a public set of labelled prompt injections, the built-in pattern rules catch 2% of them and the optional ML model catches 40%; the model also flagged 3 of 399 harmless prompts (intervals and method in eval). Treat guardrail-rs as one extra layer, not as a security control you can rely on by itself.
Quick start
Create guardrail.toml:
[]
= 8080
[]
= "https://api.openai.com"
[]
= "allow" # or "block"; see "Defaults to know" below
Check it, then start the proxy:
Point your SDK at the proxy:
=
OpenAI Chat Completions and Responses, Anthropic Messages and Azure OpenAI
chat requests are checked. GET /v1/models is passed through; other routes
are refused unless you list them.
Try a check without starting the server:
No Rust installed? The Colab notebook installs everything and walks through a blocked and a redacted request. Docker and Kubernetes setups are in deployment.
What it checks
- Prompt injection (pattern rules). 37 regular expressions for specific phrasings; tested examples include "ignore all previous instructions", "you are now DAN", "stay in character" and "reveal your full system prompt". On by default. They catch common jailbreak templates (43% of the role-play jailbreaks they were developed on) and little else: 2% of a separate public injection dataset, and none of its held-out test split (results and misses).
- Personal data. Emails, phone numbers, card numbers (Luhn-checked),
SSNs, IP addresses and API keys are replaced before the request leaves.
On a labelled PII dataset it removed over 99% of emails, SSNs, IP
addresses, valid card numbers and US-format phone numbers, 87% of
international phone numbers, and 48% of API keys. A key is caught when
it has a known prefix such as
sk-or follows a phrase like "API key" orapi_key=; a bare random token is not (results). On by default for requests; for model responses, turn onredact_responses. - Your own rules. Block keywords, cap token counts, or require a system prompt.
- Optional ML models (
--features onnx, experimental). A DeBERTa prompt-injection model and a RoBERTa toxicity model, run locally with ONNX Runtime on the CPU. Much better at injection than the patterns, and much slower (see below).
What it does not do
These are deliberate limits, not oversights:
- It doesn't check what the model says back, apart from optional PII redaction. Instructions hidden in a web page or a tool result that the model then follows (indirect injection) are out of scope.
- It looks at one request at a time. An attack spread across several turns can get through.
- It can be evaded. A determined attacker can rephrase around pattern rules and fool ML classifiers. guardrail-rs raises the cost of casual attacks; it doesn't stop a targeted one.
- It isn't a rate limiter or a DoS shield. It can cap the requests it handles at once, but per-client limits belong in a reverse proxy or load balancer in front of it.
- It doesn't make your provider or your app trustworthy. Upstream compromise and application bugs are outside its view.
The full list, with the reasoning, is in the threat model.
Defaults to know
- Errors fail open. If a check errors on a request (for example the ML
model fails), that check is skipped and the request goes through. Set
pipeline.on_error = "block"to refuse the request instead.guardrail validatewarns until you choose either value. - No request limit. Unless you set
server.max_concurrent_requests, every request is accepted, and each one's body is held in memory while it is checked. The example configuration sets 64. - Typos are errors. A configuration key guardrail-rs doesn't know stops
validate, startup and reload, so a misspelt setting can't silently fall back to its default. - The audit log holds no message text. It records the decision, the model, the kinds of personal data found and timings. A test checks this for the common request paths; what it covers.
How fast
With the default checks, one request took 5 µs at 512 bytes and 47 µs at 8 KB (0.4.0, one local run). The optional ML models are much slower: 36–42 ms per model for a short prompt, around 0.4 s at 1 KB on the CPU. Method and CI numbers are in benchmarks.
The release binary is about 10 MB (x86_64 Linux, default features) and needs no Python, GPU or model downloads unless you turn on the ML models.
Where this is going
guardrail-rs reaches 1.0 when:
- Detection is measured and stated. Every check is measured on labelled data, with intervals and the method published, and the defaults say plainly what they catch. Done for prompt injection and PII; the optional toxicity model is next.
- Load is bounded inside the proxy. Done for requests in flight
(
server.max_concurrent_requests, answered with503andRetry-After), which also bounds the ML model queues; a per-model limit weighted by input length is still open. - Streaming is covered. Done: response redaction works on every streamed route, including the Responses API.
- The configuration is stable. No breaking changes to
guardrail.tomlwithin 1.x.
Decided for 1.0: errors fail open by default. guardrail-rs is meant
as one extra layer, so a check that breaks shouldn't take your app down
with it. The cost is that a request can go through unchecked, so it is
never silent: guardrail validate warns until pipeline.on_error is set,
and every skipped check is counted in /metrics and named in the audit
log (failed_open_stages). If you would rather refuse the request, set
pipeline.on_error = "block".
Everything outside the "does not do" list above is up for discussion. Open work is tracked in the roadmap.
Contributing
Contributions are welcome, and the most useful ones right now are small:
- Test cases. Real-world injection attempts, or harmless text that gets flagged by mistake. These feed the detection measurements.
- Rules. A new injection pattern or a new kind of personal data, with tests.
- Reports. Something that didn't work the way this README says.
Start with CONTRIBUTING.md. Security issues go through SECURITY.md, not public issues.
For AI coding agents
AGENTS.md has the crate map, the exact build, test and lint commands CI runs, and where to make common changes. The short version:
Documentation
- Configuration: every
guardrail.tomlkey - Threat model: what is and isn't covered, and why
- Architecture: how a request flows through the stages
- ONNX models: turning on the ML checks
- Policy rules: writing your own rules
- Custom stages: using the pipeline as a Rust library
- Changelog
GuardRail Studio is a separate, optional dashboard and policy editor for a running proxy.
License
MIT or Apache-2.0, at your option.