> For the complete documentation index, see [llms.txt](https://docs.mozilla.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mozilla.ai/any-guardrail/cookbook/any_llm_as_a_guardrail.md).

# Any LLM as a Guardrail

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/mozilla-ai/any-guardrail/blob/main/docs/cookbook/any_llm_as_a_guardrail.ipynb)

## Install dependencies

```python
import nest_asyncio

nest_asyncio.apply()
```

We will be using a model from `openai` by default, but you can check the different providers supported in `any-llm`:

<https://docs.mozilla.ai/providers>

```python
import os
from getpass import getpass

if "OPENAI_API_KEY" not in os.environ:
    print("OPENAI_API_KEY not found in environment!")
    api_key = getpass("Please enter your OPENAI_API_KEY: ")
    os.environ["OPENAI_API_KEY"] = api_key
    print("OPENAI_API_KEY set for this session!")
else:
    print("OPENAI_API_KEY found in environment.")
```

## Create the guardrail

```python
from any_guardrail import AnyGuardrail, GuardrailName
```

```python
guardrail = AnyGuardrail.create(GuardrailName.ANYLLM)
```

## Try it with different models / policies / inputs

```python
MODEL_ID = "openai/gpt-5-nano"

POLICY = """
You hate Mondays.
You must reject any request related with planning activities on Mondays.
"""
```

```python
guardrail.validate("Can you suggest me some restaurants for lunch on Monday?", policy=POLICY, model_id=MODEL_ID)
```

```python
guardrail.validate("Can you suggest me some restaurants for lunch on Friday?", policy=POLICY, model_id=MODEL_ID)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mozilla.ai/any-guardrail/cookbook/any_llm_as_a_guardrail.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `build a script that syncs our docs to a CMS` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
