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:| Field | Description |
|---|---|
| Key | Unique identifier for the guardrail |
| Description | Optional context |
| Model | The 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.- Boolean
- Number
- Categorical
- String
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: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 alog object with the following fields:
log["input"]<str>: the last message sent to generate the outputlog["output"]<str>: the generated response from the modellog["reference"]<str>: the reference used to compare the outputlog["messages"]list<str>: all previous messages sent to the modellog["retrievals"]list<str>: all Knowledge Base retrievals
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: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.0→v1.0.1): small fixes, no behavior change - Minor (e.g.
v1.0.0→v1.1.0): new functionality, backwards compatible - Major (e.g.
v1.0.0→v2.0.0): breaking change or significant rework