# Guardrails

> The Guardrails are rules and checks that limit what an AI system can receive, do or output.

## What are guardrails in LLM applications?

The checks placed around a model to validate inputs and outputs, restrict available tools and actions, and filter unsafe or incorrect content. Make AI applications more predictable and secure.

- Topic: Quality & Operations
- Type: technique

## Example

Wrong:

```js
const reply = await llm(prompt);
await db.orders.update(JSON.parse(reply));
```

Right:

```js
const reply = await llm(prompt);
const result = OrderUpdate.safeParse(JSON.parse(reply));
if (!result.success || !canEdit(user, result.data.orderId)) {
  log.warn('guardrail_blocked', { userId: user.id });
  return 'Sorry, I cannot make that change.';
}
await db.orders.update(result.data);
```

Validating the output with a schema and checking permissions in code stops bad or injected output from reaching the database.

## Common mistakes

- Relying only on 'never do X' in the system prompt as the security layer.

## Key points

- Input and output checks run in application code, not only as instructions in the system prompt.
- Model output is validated against a schema before the app acts on it.
- Blocked requests return a safe message and are logged for review.

## Interview questions

### Are guardrails the same as content moderation?

Moderation is one kind of guardrail. Guardrails also include schema validation, tool restrictions, rate limits and permission checks.

### Can a system prompt act as a guardrail?

It helps, but it is not reliable on its own, because prompts can be bypassed. Enforce critical rules in application code.

### Do guardrails slow down responses?

Some do, especially extra model calls. Fast checks like schemas and regex add little delay, and slower checks can run in parallel.

## Related cards

- [Prompt Injection](https://wonderium.dev/ai/prompt-injection.md)
- [Structured Output](https://wonderium.dev/ai/structured-output.md)
- [Jailbreak](https://wonderium.dev/ai/jailbreak.md)
- [Human-in-the-Loop](https://wonderium.dev/ai/human-in-the-loop.md)
- [Responsible AI](https://wonderium.dev/ai/responsible-ai.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/guardrails
- Last updated: 2026-11-25
