> ## Documentation Index
> Fetch the complete documentation index at: https://docs.orq.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Guardrails | AI Gateway

> Create LLM and Python guardrails in the AI Gateway to evaluate and block non-compliant requests and responses.

**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](/docs/ai-gateway/configuration/guardrail-rules) to block non-compliant generations before they reach the caller.

<CardGroup cols={2}>
  <Card title="LLM Guardrail" icon="robot" href="#llm-guardrail">
    Use a model to judge outputs against any criteria defined in a prompt.
  </Card>

  <Card title="Python Guardrail" icon="python" href="#python-guardrail">
    Write custom Python code for full flexibility: regex checks, length validation, HTTP calls, or JSON schema validation.
  </Card>
</CardGroup>

## 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 <kbd className="key">+ Guardrail</kbd>, 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](/docs/ai-gateway/using-the-router) 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](/docs/ai-studio/ai-engineering/knowledge-bases-memory-stores) 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.

<Tabs>
  <Tab title="Boolean" icon="bars">
    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.
  </Tab>

  <Tab title="Number" icon="hashtag">
    The model returns a numeric score. Use any scale that fits the use case (e.g. 1-5, 0-100).

    **Pass condition**: Enter a threshold in **Pass if greater or equal than**. The guardrail passes when the score meets or exceeds the threshold.
  </Tab>

  <Tab title="Categorical" icon="grid-2">
    The model classifies the output into one of the predefined labels.

    When **Categorical** is selected, a label editor appears. Add one label per row: enter a **Value** (the exact string the model must return) and an optional **Description** to guide the model.

    **Pass condition**: Select one or more values in **Pass if output is one of**. The guardrail passes when the model's output matches any selected label.
  </Tab>

  <Tab title="String" icon="font">
    The model returns a free-form string response. Not available as a guardrail pass condition.
  </Tab>
</Tabs>

### Testing

The **Playground** panel provides an **Editor** for testing. Fill the payload manually:

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "messages": [],
  "input": "",
  "retrievals": [],
  "output": "",
  "reference": ""
}
```

Click **Run test** to execute the guardrail. The result appears in the **Response** field.

<Note>
  The Dataset tab is not available for Guardrails.
</Note>

## Python Guardrail

Python Guardrails accept custom **Python code** for full evaluation flexibility. The UI has three panels: **Settings**, **Code**, and **Playground**.

<Note>
  Python code is limited to 1 MB (1,048,576 bytes) per guardrail: roughly 1 million characters, or about 20,000 lines of typical Python. Larger code returns a `Code exceeds maximum size` error and does not run.
</Note>

Navigate to **Guardrails** in the **AI Gateway** sidebar, click <kbd className="key">+ Guardrail</kbd>, 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](/docs/ai-studio/ai-engineering/knowledge-bases-memory-stores) retrievals

The function must return a **Boolean** or **Number**:

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
def evaluate(log):
    return True
```

<Info>
  Define multiple helper functions if needed. The last defined function is the entry point when the guardrail runs.
</Info>

### Environment and Libraries

The Python Guardrail runs in **Python 3.12** with the following preloaded libraries:

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
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.

<Note>
  The Dataset tab is not available for Guardrails.
</Note>

## Versions

Click <kbd className="key">Publish</kbd> 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

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](/docs/ai-gateway/configuration/guardrail-rules).
