Skip to main content
Guardrails in the AI Gateway are LLM-as-a-Judge and Python evaluators that validate requests and responses passing through the gateway. Once created, they can be attached to Guardrail Rules and Policies to block non-compliant generations before they reach the caller.

LLM Guardrail

Use a model to judge outputs against any criteria defined in a prompt.

Python Guardrail

Write custom Python code for full flexibility: regex checks, length validation, HTTP calls, or JSON schema validation.

LLM Guardrail

LLM Guardrails use a model to judge requests or responses against criteria defined in a prompt. Navigate to Guardrails in the AI Gateway sidebar, click + Guardrail, and select LLM. Fill in the following fields:
FieldDescription
KeyUnique identifier for the guardrail
DescriptionOptional context
ModelThe model used as judge. Any model enabled in the AI Gateway is available

Configure Prompt

The prompt has access to the following string variables:
  • {{log.input}}: the last message sent to the model
  • {{log.output}}: the output response generated by the evaluated model
  • {{log.messages}}: all messages sent to the model, excluding the last message
  • {{log.retrievals}}: Knowledge Base retrievals
  • {{log.reference}}: the reference used to compare output

Output and Guardrail Configuration

Select the output type and set the pass condition. The Guardrail configuration panel is visible directly in the settings.
The model returns a True or False response. Use for binary pass/fail checks.Pass condition: Select True or False. The guardrail passes when the model returns the selected value.

Testing

The Playground panel provides an Editor for testing. Fill the payload manually:
{
  "messages": [],
  "input": "",
  "retrievals": [],
  "output": "",
  "reference": ""
}
Click Run test to execute the guardrail. The result appears in the Response field.
The Dataset tab is not available for Guardrails.

Python Guardrail

Python Guardrails accept custom Python code for full evaluation flexibility. The UI has three panels: Settings, Code, and Playground. Navigate to Guardrails in the AI Gateway sidebar, click + Guardrail, and select Python. The evaluation function receives a log object with the following fields:
  • log["input"] <str>: the last message sent to generate the output
  • log["output"] <str>: the generated response from the model
  • log["reference"] <str>: the reference used to compare the output
  • log["messages"] list<str>: all previous messages sent to the model
  • log["retrievals"] list<str>: all Knowledge Base retrievals
The function must return a Boolean or Number:
def evaluate(log):
    return True
Define multiple helper functions if needed. The last defined function is the entry point when the guardrail runs.

Environment and Libraries

The Python Guardrail runs in Python 3.12 with the following preloaded libraries:
numpy==1.26.4
nltk==3.9.1
requests
pydantic
json
re

Guardrail Configuration

Set the pass condition based on the return type:
  • Boolean: select True or False. The guardrail passes when the function returns the selected value.
  • Number: enter a score threshold. The guardrail passes when the return value is greater than or equal to the threshold.

Testing

The Playground panel provides an Editor for testing. Fill the payload manually and click Run test to execute the guardrail.
The Dataset tab is not available for Guardrails.

Versions

Click Publish to save changes. Choose a version bump:
  • Patch (e.g. v1.0.0v1.0.1): small fixes, no behavior change
  • Minor (e.g. v1.0.0v1.1.0): new functionality, backwards compatible
  • Major (e.g. v1.0.0v2.0.0): breaking change or significant rework
The Versions tab shows the full history with author and publish timestamp for each version.

Using Guardrails

Guardrails created here are available for selection when configuring Guardrail Rules and Policies.