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

# Guardrail

> Validate agent outputs with LLM-based guardrails

The `--guardrail` flag enables LLM-based output validation to ensure agent responses meet specific criteria.

## Quick Start

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai "Write code" --guardrail "Ensure code is secure and follows best practices"
```

<img src="https://mintcdn.com/praisonai/SX0Y8_-DRBjzOTnt/docs/cli/guardrail-guardrail-validates-output-qua.gif?s=f9ab9d41d87023007242a4258978baa5" alt="Guardrail Validates Output Quality" width="1497" height="1104" data-path="docs/cli/guardrail-guardrail-validates-output-qua.gif" />

## Usage

### Basic Guardrail

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai "Generate SQL query" --guardrail "No DROP or DELETE statements allowed"
```

**Expected Output:**

```
🛡️ Guardrail enabled: No DROP or DELETE statements allowed

╭─ Agent Info ─────────────────────────────────────────────────────────────────╮
│  👤 Agent: DirectAgent                                                       │
│  Role: Assistant                                                             │
╰──────────────────────────────────────────────────────────────────────────────╯

╭────────────────────────────────── Response ──────────────────────────────────╮
│ SELECT * FROM users WHERE status = 'active';                                 │
╰──────────────────────────────────────────────────────────────────────────────╯

✅ Guardrail passed: Output meets criteria
```

### Combine with Other Flags

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Guardrail with save
praisonai "Write API documentation" --guardrail "Include all endpoints" --save

# Guardrail with metrics
praisonai "Generate report" --guardrail "Must include sources" --metrics

# Guardrail with planning
praisonai "Create security audit" --guardrail "Follow OWASP guidelines" --planning
```

## Common Guardrail Criteria

<CardGroup cols={2}>
  <Card title="Security">
    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    --guardrail "No sensitive data exposure"
    --guardrail "Follow security best practices"
    --guardrail "Sanitize all inputs"
    ```
  </Card>

  <Card title="Quality">
    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    --guardrail "Include error handling"
    --guardrail "Add type hints"
    --guardrail "Follow PEP 8 style"
    ```
  </Card>

  <Card title="Content">
    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    --guardrail "Professional tone only"
    --guardrail "Include citations"
    --guardrail "No speculation"
    ```
  </Card>

  <Card title="Format">
    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    --guardrail "Output as JSON"
    --guardrail "Include headers"
    --guardrail "Maximum 500 words"
    ```
  </Card>
</CardGroup>

## How It Works

1. **Agent Execution**: The agent processes your prompt normally
2. **Validation**: The guardrail LLM evaluates the output against your criteria
3. **Result**: Pass/fail status is displayed with feedback

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
flowchart LR
    A[Prompt] --> B[Agent]
    B --> C[Output]
    C --> D{Guardrail}
    D -->|Pass| E[✅ Return Output]
    D -->|Fail| F[⚠️ Warning + Output]
```

## Examples

### Code Quality Guardrail

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai "Write a Python function to parse JSON" \
  --guardrail "Must include docstring, type hints, and error handling"
```

**Expected Output:**

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
def parse_json(json_string: str) -> dict:
    """
    Parse a JSON string into a Python dictionary.
    
    Args:
        json_string: A valid JSON formatted string
        
    Returns:
        Parsed dictionary from the JSON string
        
    Raises:
        json.JSONDecodeError: If the string is not valid JSON
    """
    import json
    try:
        return json.loads(json_string)
    except json.JSONDecodeError as e:
        raise json.JSONDecodeError(f"Invalid JSON: {e.msg}", e.doc, e.pos)
```

### Content Guardrail

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai "Write a product description" \
  --guardrail "No exaggerated claims, include specifications"
```

### SQL Safety Guardrail

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
praisonai "Generate database queries for user management" \
  --guardrail "Read-only queries, no modifications allowed"
```

## Best Practices

<Tip>
  Be specific with your guardrail criteria. Vague criteria may lead to inconsistent validation.
</Tip>

<Warning>
  Guardrails add an additional LLM call, which increases latency and token usage. Use `--metrics` to monitor costs.
</Warning>

| Do                                          | Don't           |
| ------------------------------------------- | --------------- |
| "Include error handling for all edge cases" | "Make it good"  |
| "No SQL injection vulnerabilities"          | "Be secure"     |
| "Output must be valid JSON"                 | "Format nicely" |
| "Maximum 3 paragraphs"                      | "Keep it short" |

## Related

* [Guardrails Concept](/concepts/guardrails)
* [Metrics CLI](/docs/cli/metrics)
* [Planning Mode](/features/planning-mode)
