# Getting started

## Running the guardrail backend

You can start the guardrails backend by running:

```bash
./opik.sh --guardrails
```

## Using the Python SDK

<Frame>
  <img src="https://files.buildwithfern.com/https://opik.docs.buildwithfern.com/docs/opik/2026-01-30T17:33:07.444Z/img/production/guardrails_editor.gif" />
</Frame>

```python
from opik.guardrails import Guardrail, PII, Topic
from opik import exceptions

guardrail = Guardrail(
    guards=[
        Topic(restricted_topics=["finance", "health"], threshold=0.9),
        PII(blocked_entities=["CREDIT_CARD", "PERSON"]),
    ]
)

llm_response = "You should buy some NVIDIA stocks!"

try:
    guardrail.validate(llm_response)
except exceptions.GuardrailValidationFailed as e:
    print(e)
```

The immediate result of a guardrail failure is an exception, and your application code will need to handle it.

The call is blocking, since the main purpose of the guardrail is to prevent the application from proceeding with a potentially undesirable response.

### Guarding streaming responses and long inputs

You can call `guardrail.validate` repeatedly to validate the response chunk by chunk, or their parts or combinations.
The results will be added as additional spans to the same trace.

```python
for chunk in response:
    try:
        guardrail.validate(chunk)
    except exceptions.GuardrailValidationFailed as e:
        print(e)
```

## Working with the results

### Examining specific traces

When a guardrail fails on an LLM call, Opik automatically adds the information to the trace.
You can filter the traces in your project to only view those that have failed the guardrails.

<Frame>
  <img src="https://files.buildwithfern.com/https://opik.docs.buildwithfern.com/docs/opik/2026-01-30T17:33:07.444Z/img/production/guardrails_example.png" />
</Frame>

### Analyzing trends

You can also view how often each guardrail is failing in the Metrics section of the project.

## Performance and limit considerations

The guardrails backend will use a GPU automatically if there is one available.
For production use, running the guardrails backend on a GPU node is strongly recommended.

Current limits:

* Topic guardrail: the maximum input size is 1024 tokens
* Both Topic and PII guardrails support English language