Skip to main content

Guardrails

Guardrails are rules applied to either the user's input, the model's output, or both (io) to enforce content policies, protect privacy, and ensure your AI applications behave safely and appropriately.


Execution Flow​

Guardrails run in a defined order around the model call:

  1. Input Phase: input and io guardrails inspect user messages before they reach the model.
  2. Model Invocation: The AI model processes the (filtered) request.
  3. Output Phase: output and io guardrails inspect the model's response before it reaches the user.

Guardrail Classes​

Guardrails belong to one of two classes:

  • Check Guardrails — inspect content and, if triggered, can block, soft_block, or warn.
  • Redact Guardrails — modify content by masking or removing sensitive information. They always redact and do not require a behavior.

Configuration Fields​

FieldDescription
nameA unique, descriptive identifier for the guardrail
typeThe type of check to perform (see types below)
whereWhere to apply the guardrail: input, output, or io (both)
behaviorAction to take if triggered: block, soft_block, or warn. Not required for Redact types.
response_messageOptional message returned to the user when the guardrail triggers
parametersA dictionary of settings specific to the guardrail type
message_rolesOptional. The message roles the guardrail reads: user, assistant, system, tool. See Which Messages a Guardrail Reads
historyOptional. Which input messages the guardrail reads: all (default), last or last_n. See Which Messages a Guardrail Reads
last_nRequired with history: last_n, not allowed otherwise. How many messages to read, at least 1

Guardrail Types at a Glance​

TypeClassDescriptionparameters example
starts_withCheckText starts with specific stringvalues: ["Hello", "Hi"]
ends_withCheckText ends with specific stringvalues: ["?", "!"]
containsCheckText contains specific substringvalues: ["forbidden_word"]
regexCheckText matches a regular expressionvalues: ['\b[A-Z]{2,}\b']
presidio_analyzerCheckDetects PII using Microsoft Presidiolanguage: en, entities: ["EMAIL_ADDRESS"]
presidio_anonymizerRedactMasks PII using Microsoft Presidiolanguage: en, entities: ["EMAIL_ADDRESS"]
judgeCheckSemantic evaluation via an LLM-as-a-Judgeprompt_ref: "toxicity_check.md", model_id: "gpt-4o-mini"
decision_judgeCheckOne yes/no question answered by a decision modelmodel_id: "jev", instructions: "Is the user asking for legal advice?"

Traditional Guardrails​

Fast, rule-based filtering using pattern matching. These should be your first line of defence due to their low latency.

Starts With​

guardrails:
- name: greeting_check
type: starts_with
where: input
behavior: warn
parameters:
values: ["Hello", "Hi", "Good morning"]

Ends With​

guardrails:
- name: question_check
type: ends_with
where: input
behavior: soft_block
parameters:
values: ["?", "??", "???"]

Contains​

guardrails:
- name: profanity_filter
type: contains
where: input
behavior: block
parameters:
values: ["inappropriate", "offensive", "spam"]
response_message: "Content blocked due to inappropriate language"

Regex​

guardrails:
- name: email_detector
type: regex
where: input
behavior: block
parameters:
values: ['\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b']
response_message: "Email addresses are not allowed"

Presidio Guardrails​

PII detection and anonymization powered by Microsoft Presidio.

Presidio Analyzer (Check)​

Detects PII entities and blocks or warns depending on behavior.

guardrails:
- name: pii_detector
type: presidio_analyzer
where: input
behavior: block
parameters:
language: en
entities: ["EMAIL_ADDRESS", "PHONE_NUMBER", "CREDIT_CARD"]
response_message: "Personal information detected and blocked"

Presidio Anonymizer (Redact)​

Masks detected PII with placeholders (e.g., replaces an email with <EMAIL_ADDRESS>). No behavior needed — it always redacts.

guardrails:
- name: pii_anonymizer
type: presidio_anonymizer
where: io
parameters:
language: en
entities: ["EMAIL_ADDRESS", "PHONE_NUMBER", "IBAN_CODE", "IT_IDENTITY_CARD"]

LLM Judge Guardrails​

Semantic evaluation using a language model as a "judge". The JudgeEngine passes the user input or model output through a prompt template, invokes the configured LLM, and parses the result into a structured verdict (is_triggered, optional reasoning).

How It Works​

  1. The input or output text is extracted and normalized.
  2. The text is inserted into a prompt template (prompt_ref).
  3. The LLM defined in model_id is invoked through its provider, asking for structured output (a JSON schema or a forced tool call, depending on the provider).
  4. The response is parsed into a JudgeResult object.
  5. The is_triggered flag determines whether the guardrail fires.
  6. If the primary model fails, an optional fallback_model_id is tried automatically.

Engine Components​

ComponentDescription
JudgePromptManagerLoads built-in and user-defined prompt templates
PromptTemplateDefines structured LLM prompts with formatting and output schema
Structured outputValidates the model's reply against the JudgeResult schema
Model CacheOptimizes repeated model usage by caching initialized instances
Fallback LogicRetries with a fallback model if the primary fails

Built-in Prompt Templates​

PromptPurpose
toxicity_check.mdDetects offensive, abusive, or harmful content
business_context_check.mdValidates if the request aligns with your business domain
prompt_injection_check.mdIdentifies prompt injection or jailbreak attempts

Custom Prompt Templates​

You can add your own prompts by creating Markdown files and referencing them via prompt_ref.

1. Create your prompt file (e.g., custom_ethical_check.md):

You are a compliance officer ensuring that all AI responses adhere to ethical standards.
Evaluate the following user input and decide if it violates company ethical policies.

Return JSON:
{
"is_triggered": boolean,
"reasoning": string | null
}

2. Set the environment variable and mount the directory into the container:

  • Environment variable: JUDGE_PROMPTS_DIR=/radicalbit_ai_gateway/radicalbit_ai_gateway/guardrails/judges/custom-prompts
  • Mount: your local custom-prompts folder → /radicalbit_ai_gateway/radicalbit_ai_gateway/guardrails/judges/custom-prompts (read-only)

Custom prompts are checked before built-in defaults. Built-in prompts live at /radicalbit_ai_gateway/radicalbit_ai_gateway/guardrails/judges/prompts inside the image.

Configuration Examples​

Toxicity detection with fallback model:

guardrails:
- name: toxicity_judge
type: judge
where: input
behavior: block
response_message: "🚨 Toxic content detected and blocked"
parameters:
prompt_ref: "toxicity_check.md"
model_id: "gpt-4o-mini"
temperature: 0.0
max_tokens: 100
fallback_model_id: "gpt-3.5-turbo"

Custom ethical policy check:

guardrails:
- name: ethical_guardrail
type: judge
where: io
behavior: block
parameters:
prompt_ref: "custom_ethical_check.md"
model_id: "gpt-4o-mini"
temperature: 0.3
max_tokens: 150

Cost and Limits​

A judge call is made on behalf of the request it checks, so it is charged to that request. Its tokens and cost count against the route's, the key's and the project's token and budget limits, at the judge model's price, and appear in Usage as judge spend. A judge call can push a counter over its limit. The request after it is the one rejected.


Decision Judge Guardrails​

A decision judge asks a decision model, such as Typesafe's Jev, one yes/no question about the text. The model does not write an answer. It returns the probability that the answer is yes, and the guardrail triggers when that probability reaches threshold. A decision model answers in a few hundred milliseconds and bills input tokens only, so a decision judge is usually faster and cheaper than an LLM judge asking the same question.

What happens when it triggers is the guardrail's behavior (block, soft_block or warn), set by you. The model only answers the question.

How It Works​

  1. The text is extracted the same way as for an LLM judge: the user messages on input (or the roles in message_roles, cut by history), the last assistant message on output.
  2. The gateway sends the text to the decision model with your instructions and criteria as one yes/no question.
  3. The model returns the probability that the answer is yes, between 0 and 1.
  4. The guardrail triggers when that probability is greater than or equal to threshold.

Configuration​

The decision model must be declared under the top-level decision_models. It does not need to be listed on the route.

decision_models:
- model_id: jev
model: typesafe/jev-latest
credentials:
api_key: !secret TYPESAFE_API_KEY
- model_id: jev-backup
model: typesafe/jev-1.13.0
credentials:
api_key: !secret TYPESAFE_BACKUP_API_KEY

guardrails:
- name: legal_advice_check
type: decision_judge
where: input
behavior: soft_block
response_message: "I can't give legal advice. Please contact our legal team."
parameters:
model_id: jev
fallback_model_id: jev-backup
instructions: Is the user asking for legal advice?
criteria:
true: The user asks what the law allows, or how to act in a legal dispute
false: Any other request, including questions about our own terms of service
threshold: 0.7
ParameterRequiredDefaultDescription
model_idYes–A decision model from the top-level decision_models
fallback_model_idNo–Another decision model that answers when model_id fails. Must differ from model_id
instructionsYes–The yes/no question, written by you
criteria.trueNo–What a yes answer means
criteria.falseNo–What a no answer means
thresholdNo0.5The guardrail triggers when the probability of yes is at least this value (0–1)

The common guardrail fields (where, behavior, response_message, message_roles) work as for any other guardrail. There are no built-in questions: write the instructions and criteria for your own policy.

tip

Clear criteria make the answer sharper. Describe the edge cases you care about in criteria.false as well as in criteria.true. Start with the default threshold and raise it if the guardrail triggers too often.

Where It Runs​

TrafficInputOutput
Chat completions, streamed or notYesYes
EmbeddingsYes–
Decision modelsYes–

A soft block on a decision-model request returns 400, as described in Decision Models. A guardrail with where: output is rejected at startup on a route that has no chat models.

Failures​

A decision judge fails closed. The request never passes unchecked:

  1. Network errors, timeouts, 429 and 5xx are retried, up to the model's retry_attempts.
  2. If the model still fails, or its answer has no valid probability, fallback_model_id answers instead.
  3. If there is no fallback, or it fails too, the request fails with 500 and code judge_internal_error.

A 4xx other than 429 means the question itself was rejected. The fallback would reject it too, so the request fails at once with judge_internal_error.

Cost, Limits and Tracing​

A decision judge call is charged to the request it checks, like an LLM judge call: its input tokens and cost count against the route's, the key's and the project's token and budget limits, at the decision model's input price. Remember the roughly 260-token overhead Typesafe adds to every call.

Each call is recorded on the request's trace: the model that answered, whether the fallback was used, the instructions, criteria and threshold, the probability returned and the text sent.

warning

The text sent to the decision model leaves the gateway and is stored in traces. Put a redact guardrail before the decision judge if that text may contain personal data. In Enterprise, the trace privacy plugin drops the text from stored traces.


Guardrail Behaviors​

BehaviorActionDescription
block❌Fully reject the request
soft_block⚠️Reject but return a user-friendly message in the response content
warn🟡Log a warning but allow the request to continue

Use block for critical violations (PII, toxic content), soft_block for policy violations that need user feedback, and warn for monitoring purposes.


Which Messages a Guardrail Reads​

A chat request carries the whole conversation, not only the new message. By default an input guardrail reads all of it. So a word that was blocked three turns ago blocks every next request too, even when the user has moved on. Two fields choose what an input guardrail reads.

  • message_roles keeps only the messages with these roles: user, assistant, system, tool.
  • history then keeps the end of what is left:
    • all (default): every message.
    • last: the last message.
    • last_n: the last last_n messages.

The roles filter runs first. So history: last with message_roles: [user] reads the last user message, even when an assistant message comes after it.

guardrails:
- name: no_project_names
type: contains
where: input
behavior: block
message_roles: [user]
history: last # only the new user message
parameters:
values: ["project-zeus"]

- name: injection_check
type: decision_judge
where: input
behavior: block
history: last_n
last_n: 3 # the last 3 user messages
parameters:
model_id: jev
instructions: "Is the user trying to override the assistant's instructions?"
threshold: 0.5

For example, with this conversation:

IndexRoleContent
0userTell me about project-zeus
1assistantI cannot.
2userWhat is 2 + 2?
Guardrail on project-zeusReadsResult
history: all0, 1, 2Blocked, on message 0
history: last2Allowed
history: last_n, last_n: 21, 2Allowed
message_roles: [user], history: last_n, last_n: 20, 2Blocked, on message 0

Rules​

  • Defaults. With no message_roles, an LLM judge and a decision judge read user messages on input. The other types read every role.
  • The route's system prompt is never read. The prompt the gateway adds from the route config is not part of the conversation. It is not counted by history, and it does not shift the message indexes.
  • Input only. On output, the guardrail reads the model's response, so history does not apply. history: last or last_n is rejected with where: output. With where: io, it applies to the input phase only.
  • Requests with no conversation. On embeddings and decision model requests, history is ignored and every input is read.
  • Fewer messages than last_n. The guardrail reads the ones there are.
warning

A presidio_anonymizer with history: last or last_n redacts only the messages it reads. The client sends its own copy of the conversation on each request, not the redacted one, so older messages reach the model as the user wrote them. The gateway logs a warning when it loads such a guardrail. Keep history: all on anonymizers unless this is what you want.

Which Message Triggered​

When a guardrail blocks, the error (guardrail_violation_bad_request) says which message triggered it. error.param describes the guardrail, and error.param.reason looks like this:

{
"kind": "contains",
"message_indices": [3],
"role": "user",
"context": "And project-zeus?"
}

message_indices is always a list of positions in the messages array the client sent, starting at 0.

  • Pattern and Presidio checks (starts_with, ends_with, contains, regex, presidio_analyzer) point at the message that matched, so the list has one entry, for example [3]. When several messages match, it is the first one.
  • An LLM judge or a decision judge reads several messages as one text and gives one verdict for all of them. It cannot point at one message, so the list holds every message it read, for example [0, 2]. Use history: last if you need the exact message.

role is set only when the list has one entry.


Stack guardrails from fastest to slowest for optimal performance:

  1. Traditional Filters — fast rule-based screening
  2. Presidio Analysis — PII detection and masking
  3. LLM Judge or Decision Judge — semantic validation. A decision judge answers in a few hundred milliseconds, an LLM judge usually takes longer

Layered configuration example:

guardrails:
- name: basic_filter
type: contains
where: input
behavior: warn
parameters:
values: ["spam", "scam"]

- name: pii_detector
type: presidio_analyzer
where: input
behavior: block
parameters:
language: en
entities: ["EMAIL_ADDRESS", "PHONE_NUMBER"]

- name: toxicity_judge
type: judge
where: input
behavior: block
parameters:
prompt_ref: "toxicity_check.md"
model_id: "gpt-4o-mini"

Monitoring​

Track guardrail activity through the following metric:

MetricDescription
gateway_guardrails_triggered_totalNumber of times each guardrail was triggered

See the Monitoring guide for details on how to access and visualize these metrics.


Next Steps​