Guardrails
Guardrails inspect every request before a provider sees it. They read the content of the request in any API format — chat messages, system prompts, tool calls and tool results, Responses API input, embeddings input, attached files — and can refuse the request, mask what they find, or just record it.
What guardrails can do
Section titled “What guardrails can do”| guard | does | default | |
|---|---|---|---|
ip_allowlist |
admits requests from listed IPs and CIDRs only, behind trusted reverse proxies too | 403 | details |
time_window |
admits requests during set hours only, per weekday and time zone | 403 | details |
keywords |
refuses words and phrases you name — codenames, customers, projects | block, user content |
below |
length |
bounds the characters of one request | block |
below |
prompt_injection |
“ignore previous instructions”, role assumption, system-prompt leaks, chat-template tokens; in tool results and attachments also notes addressed to the AI, hidden tasks, hiding from the user, sending data out — English and German | block, user content |
details |
prompt_injection_contextual |
injections in tool results and attachments however they are phrased — found by a model you choose | mask, tool results and attachments |
details |
secrets |
API keys, tokens, private keys, connection-string passwords (PromptGate rules + 221 gitleaks formats) | block, all roles |
details |
pii |
e-mail, phone, IBAN, credit card, SSN, IP address — with checksums | mask, all roles |
details |
pii_contextual |
names, postal addresses, dates of birth and kinds you describe — found by a model you choose | mask, user content |
details |
token_saver |
trims tool results — escape codes, whitespace, repeated output, optional size cap | apply, tool results |
details |
attachments |
reads files in requests (text, docx, xlsx, pptx, pdf) for every guard above, or refuses them | log, all roles |
below |
commands |
reads the model’s answer for commands that download and run code — what a poisoned model slips in for a coding agent (agent safety) | block, answers |
details |
For all of them:
- Actions —
block(422, nothing reaches the provider),mask,redact(the answer gets the original back) orlog. - Lists — keywords, IP allow and deny lists, your own injection phrases and PII exceptions from text files or URLs, fetched again while the gateway runs.
- Scopes — other rules per model alias, AI Gateway
endpoint, MCP server, API and
pg_key. - Roles — which parts of a request a guard reads.
- Every surface — the LLM proxy, AI Gateway endpoints, MCP tool calls and results, the API Gateway.
- Ready-made levels to start from.
Everything in one config:
guardrails: ip_allowlist: allow: [10.0.0.0/8] lists: [{ url: https://vpn.example.com/egress-ranges.txt, into: allow }] time_window: timezone: Europe/Berlin windows: [{ days: mon-fri, from: "07:00", to: "20:00" }] keywords: words: ["Project Falcon", "confidential"] lists: [{ file: lists/codenames.txt }] # one entry per line length: { max_chars: 400000 } prompt_injection: custom: [{ kind: exfiltration, phrase: "send ~4 passwords to" }] secrets: { action: mask } pii: action: redact # restored in the answer allow: ["@acme.com"] pii_contextual: { provider: openai, model: gpt-4o-mini } prompt_injection_contextual: { provider: openai, model: gpt-4o-mini } token_saver: {} attachments: { unreadable: block }
# Scopes name an alias, endpoint, MCP server or key the config defines. models: # per model alias local: { keywords: { enabled: false } } endpoints: # per AI Gateway endpoint — wins over the alias summarize-ticket: { pii: { action: mask } } mcp: # per MCP server browser: { prompt_injection: { roles: [user, tool] } } keys: # per pg_ key — wins over everything else ci: { keywords: { action: log } }Each guard’s own settings are described below and on its page.
Ready-made protection
Section titled “Ready-made protection”promptgate setup writes one of four levels into the config, which you
can then tune guard by guard:
| level | secrets | personal data | prompt injection | also |
|---|---|---|---|---|
recommended |
mask | redact (restored in answers) | block in your prompts, mask in tool results | token saver; commands in answers blocked |
strict |
block | mask | block, in tool results too | token saver, unreadable files refused, commands in answers blocked |
log |
log | log | log | token saver and commands as a dry run |
off |
— | — | — |
recommended masks secrets instead of refusing the request. A coding
agent that read a .env file keeps that tool result in its history and
sends it with every later request, so a block would stop the whole
session. Masked, the provider sees [GITHUB_PAT] and the session goes
on. For the same reason, an injection in a page or file the agent read
is masked there — the instruction is replaced by [IGNORE_PREVIOUS],
[ADDRESSED_TO_AI] … — while one in your own prompt is refused.
Without any config file, PromptGate applies recommended. A config file
without a guardrails: section has none.
Actions
Section titled “Actions”| action | effect |
|---|---|
block |
refuse with 422 in the client SDK’s error shape — Request blocked by guardrail keywords: keyword detected. Nothing reaches a provider; no rate-limit token or budget is used. |
mask |
replace each finding with its label ([KEYWORD], [EMAIL], …) and forward the request |
redact |
like mask, with a numbered token per value ([[EMAIL_001]]) that the answer gets back as the original — see reversible redaction |
log |
forward unchanged and record the finding; for transformers, a dry run |
apply |
transformers only: rewrite content to save tokens |
Each guard lists the actions it supports; the first is its default.
Every guard that can mask can also redact.
Findings — guard, action, kind and count, never the matched text — go to
the console line, the request log (promptgate logs, guard in
--json) and the promptgate_guardrail_findings_total metric. A block
shows as the request’s error.
Reversible redaction
Section titled “Reversible redaction”With action: redact the provider never sees the value, yet the user
reads it in the answer:
guardrails: pii: { action: redact }client sends Write a reply to [email protected] and call +49 30 1234567.provider sees Write a reply to [[EMAIL_001]] and call [[PHONE_001]].provider answers Dear [[EMAIL_001]], …client receives Dear [email protected], …- The same value gets the same token within a request, so the model knows an address it sees twice is one address. Tokens are numbered per kind and per request — never shared between requests, where they would let anyone correlate data.
- Answers are restored in every shape: buffered JSON and event
streams, in the client’s format after any translation, in text and
in tool-call arguments. Models write tokens in pieces —
[[,EMAIL,_001,]]— one event each; an event that ends with the start of a token waits until the next piece arrives (usually the next event), then both go out. Text that turns out not to be a token is sent unchanged; nothing is dropped. The cost is ≈ 0.5 µs per event. - Only exact tokens are restored. A model that rewrites one
(
EMAIL_001,[[Email_001]]) leaves it as written. - The mapping lives in memory for the duration of the request and is
never logged or stored. Request logs show
pii: redacted email×2. - Values are restored with JSON escaping for the string they land in. A value with quotes or backslashes inside a tool call’s argument string (JSON in a JSON string) would need escaping twice; e-mail addresses, phone numbers, IBANs and card numbers never do.
Settings every guard has
Section titled “Settings every guard has”| field | default | description |
|---|---|---|
enabled |
true |
false turns the guard off in this scope |
action |
per guard | block, mask or log, where supported |
roles |
per guard | whose content the guard reads: user, assistant, system, tool — a list, or a mapping to one action per role: { user: block, tool: mask } |
Roles come from the request itself: a message’s role (wherever it
stands in the message, also after content), the top-level
system / instructions (system), and tool results — role: tool
messages, tool_result blocks, function_call_output items (tool).
Embeddings and completions input count as user. Guards that look for
things users type (keywords, prompt injection) read user only by
default, so the developer’s own system prompt and earlier answers don’t
trip them; guards that look for leaks (secrets, PII) read everything,
including tool results — where a .env file an agent just read ends up.
Ids, model names and tool definitions are never read; attachments are
read as described under attachments.
With a mapping — prompt_injection: { roles: { user: block, tool: mask } }
— each role gets its own action: refuse an injection the user typed,
but mask one in a page the agent fetched, where a refusal would stop
the session (the tool result stays in its history). A role’s action
wins over action; a scope that sets roles as a list replaces the
mapping, so { action: log, roles: [user, tool] } makes a dry run.
Scopes
Section titled “Scopes”Rules apply in four layers, each overriding the one before field by field:
guardrails.<guard>— every requestguardrails.models.<alias>.<guard>— requests for that model aliasguardrails.endpoints.<name>.<guard>— requests to that AI Gateway endpointguardrails.keys.<key>.<guard>— requests with thatpg_key
Calls through the MCP gateway layer the global
rules, guardrails.mcp.<server> and the key’s; aliases and endpoints
don’t apply to them. Calls through the API Gateway
layer the global rules, guardrails.apis.<api> and the key’s.
A list such as words is replaced, not extended; entries a
lists setting brings count as part of the field
they extend. A scope can switch a
guard off (enabled: false) or on with its own settings. Guardrails
apply before routing, so every provider and fallback receives the same
inspected request. Config changes reload without a restart; a broken
rule keeps the running config and names its path
(guardrails.keys.ci.keywords: …).
Guards
Section titled “Guards”The guards with no page of their own. The others: see the overview.
keywords
Section titled “keywords”Words or phrases that must not be sent.
| field | default | |
|---|---|---|
words |
— | list of words or phrases (required, unless lists brings them) |
lists |
— | more words from files or URLs — see lists |
whole_words |
true |
match only whole words, so ass doesn’t hit class |
case_sensitive |
false |
Actions block (default), mask, log; roles user. The client is
not told which word matched.
length
Section titled “length”Bounds the total characters of content in one request.
| field | |
|---|---|
max_chars |
refuse larger requests |
min_chars |
refuse smaller ones |
Actions block (default), log. Message: Request blocked: input too long (412000 characters, max 400000).
attachments
Section titled “attachments”Files in requests — Anthropic document and image blocks, OpenAI
file and image_url parts, Responses input_file / input_image,
audio — are read like any other content: their text goes through every
content guard, the format recognised by content, not the claimed media
type. No setting is needed for that; this rule changes it.
| format | read as |
|---|---|
| plain text (txt, md, csv, json, html, code) | text — masked in place, the file re-encoded |
| docx, xlsx, pptx | text of the document, sheets, slides and notes — masked, the file becomes that text |
| pdf with a text layer | text of every page — masked, the file becomes that text |
images, scanned or encrypted PDFs, audio, unknown formats, http(s) URLs, file ids |
unreadable — see unreadable |
| field | default | |
|---|---|---|
action |
log |
log reads attachments; block refuses every request with an attachment (422 Request blocked: attachments are not allowed.) |
unreadable |
allow |
block refuses attachments PromptGate can’t read; allow passes them and logs attachments: logged unreadable_image |
masked |
text |
a PDF or Office file a guard masks: text replaces it with its masked text, block refuses the request (unmaskable_pdf) |
max_bytes |
20MB |
larger files count as unreadable (too_large) |
roles |
all | whose attachments the rule covers |
enabled |
true |
false passes attachments unread |
guardrails: keywords: { words: ["Project Falcon"] } attachments: { action: block } # no files at all… keys: legal: { attachments: { action: log, unreadable: block } } # …except for legal, read strictlyFindings in attachments name the format: keywords: blocked keyword [pdf], and the client reads keyword detected in a pdf attachment.
A PDF or Office file can’t be rewritten safely: where a guard masks
something in one, the model gets its masked text instead (a text block,
headed [PromptGate: this PDF file was replaced by its text …]; logged
attachments: logged rewritten_pdf) — without the file’s images and
layout. masked: block refuses such requests instead.
Reading runs in-process with limits: archives and PDFs may unpack to 64 MB (a PDF stream to 16 MB), text is capped at 4 MB, PDFs at 2,000 pages and 5 s, checked inside PromptGate’s own PDF reader — a file built to inflate or keep it busy stops within milliseconds. A 10-page PDF or docx takes about 0.1 ms. Images are not OCR’d.
Files uploaded beforehand through a provider’s Files API arrive as a
file_id only — their content never passes PromptGate: unreadable.
Quality gate
Section titled “Quality gate”Every build attacks the guardrails with thousands of generated secrets, personal data, injections and list entries, and fails below the required recall — see quality gate.
Performance
Section titled “Performance”Requests are walked once, without decoding them into objects; content
is matched in lowercased copies with literal search instead of
case-insensitive regular expressions; keywords finds a list of 20,000
words in one pass (lists). With keywords and length on:
4 µs for a 2 KB chat request, 0.34 ms for a 200 KB agent
context (≈ 590 MB/s, Apple M3 Pro). Without guardrails the stage costs
nothing.