> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Guardrail

> Subclass BaseGuardrail and raise InputCheckError to block security-abuse prompts as a pre-hook.

```python custom_guardrail.py theme={null}
"""
Custom Guardrail
=============================

Custom Guardrail.
"""

from agno.agent import Agent
from agno.exceptions import CheckTrigger, InputCheckError
from agno.guardrails.base import BaseGuardrail
from agno.models.openai import OpenAIResponses


class TopicGuardrail(BaseGuardrail):
    """Blocks requests that ask for dangerous instructions."""

    def check(self, run_input) -> None:
        content = (run_input.input_content or "").lower()
        blocked_terms = ["build malware", "phishing template", "exploit"]
        if any(term in content for term in blocked_terms):
            raise InputCheckError(
                "Input contains blocked security-abuse content.",
                check_trigger=CheckTrigger.INPUT_NOT_ALLOWED,
            )

    async def async_check(self, run_input) -> None:
        self.check(run_input)


# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
    name="Guarded Agent",
    model=OpenAIResponses(id="gpt-5.2"),
    pre_hooks=[TopicGuardrail()],
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    agent.print_response(
        "Explain secure password management best practices.", stream=True
    )
```

## Run the Example

<Snippet file="create-venv-step.mdx" />

**1. Install dependencies**

```bash theme={null}
uv pip install -U agno openai
```

**2. Export your OpenAI API key**

```bash Mac/Linux theme={null}
export OPENAI_API_KEY="your_openai_api_key_here"
```

```bash Windows theme={null}
$Env:OPENAI_API_KEY="your_openai_api_key_here"
```

**3. Run the example**

Save the code above as `custom_guardrail.py`, then run:

```bash theme={null}
python custom_guardrail.py
```

Full source: [cookbook/02\_agents/08\_guardrails/custom\_guardrail.py](https://github.com/agno-agi/agno/blob/v3.0.4/cookbook/02_agents/08_guardrails/custom_guardrail.py)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.