---
title: Guardrails for model requests and replies
description: "Check chat requests before they reach a provider, and replies before they reach your app. Built-in rules mask secrets and personal data into placeholders and restore them in the reply, and HTTP guardrails run checks of your own."
url: https://kaeso.app/articles/guardrails-in-kaeso
markdown: https://kaeso.app/articles/guardrails-in-kaeso.md
language: en
alternates:
  de: https://kaeso.app/de/articles/guardrails-in-kaeso.md
  zh-CN: https://kaeso.app/zh-CN/articles/guardrails-in-kaeso.md
published: 2026-11-22
authors:
  - Kaeso Team
tags:
  - gateway
  - guardrails
  - security
  - privacy
  - guide
image: https://kaeso.app/articles/guardrails-in-kaeso/cover.jpg
index: https://kaeso.app/llms.txt
---

# Guardrails for model requests and replies

Check chat requests before they reach a provider, and replies before they reach your app. Built-in rules mask secrets and personal data into placeholders and restore them in the reply, and HTTP guardrails run checks of your own.

> Every page, article and docs page of Kaeso is listed in https://kaeso.app/llms.txt.

- Published: 22 November 2026
- Reading time: 4 min
- Author: Kaeso Team

Every team that puts an AI gateway in front of its apps eventually hits the same question: how do we stop an API key, a customer's IBAN or an internal order number from leaving the network in a prompt — without breaking the app that sent it?

Kaeso 0.7.0 answers that in the gateway with **guardrails**. An organization defines guardrails once under **Organization settings** › **Guardrails** and assigns them, in order, to the projects they govern. Each guardrail is either a set of **built-in rules** that run inside the gateway in linear time, or **your own HTTP service** signed like a Standard Webhook.

![Trying built-in guardrail rules on sample text in the console: an order number, an email address and an API key are rewritten to placeholders before reaching a model](https://kaeso.app/articles/kaeso-0-7/still-guardrails.png "Built-in guardrail rules mask secrets and personal data into placeholders and restore them in the reply.")

## Built-in rules: mask, restore in the reply, block or count

Built-in rules need no external service. They scan the text of the system prompt, user and assistant messages, tool call arguments and tool results for six built-in detectors plus up to 20 custom regular expressions of your own:

| Detector | What it matches | Placeholder |
| --- | --- | --- |
| Secrets and API keys | Keys for the providers Kaeso supports, Kaeso `sk-kso-…` keys, AWS access key IDs, GitHub and GitLab tokens, private-key PEM blocks and JSON Web Tokens | `[API_KEY_1]`, `[PRIVATE_KEY_1]`, `[TOKEN_1]` |
| Email addresses | Addresses such as `ada@example.com` | `[EMAIL_1]` |
| Phone numbers | Numbers in E.164 format, such as `+4930123456` | `[PHONE_1]` |
| IBANs | International bank account numbers whose mod-97 checksum holds | `[IBAN_1]` |
| Payment card numbers | 13 to 19 digits (with spaces or hyphens) whose Luhn checksum holds | `[CARD_1]` |
| IP addresses | IPv4 and IPv6 addresses | `[IP_1]` |
| Custom patterns (up to 20) | A lowercase name (`order_id`) and a regular expression up to 500 characters evaluated in linear time by Rust's `regex` engine | `[ORDER_ID_1]` |

For each detector you choose one action:

- **Mask** replaces each match with a numbered placeholder before the request leaves Kaeso. Identical values inside the same request receive the same placeholder (`[EMAIL_1]` every time `ada@example.com` appears), so the model can still follow who is who.
- **Block** refuses the request immediately with HTTP `400` in the caller's API format and names the detector that fired — never the matched secret itself.
- **Count only** lets the request through unchanged and increments `kaeso_masked_values_total{detector, action}` and the request log's detector counts.

Two switches make masking practical for real applications:

1. **Restore in the reply.** When the model refers to `[EMAIL_1]` or `[ORDER_ID_1]` in its answer, Kaeso puts the original value back before the reply reaches your app — in whole replies (including tool call arguments) and in live streams. While streaming, the gateway holds back at most the length of one placeholder so a placeholder split across two chunks is still restored cleanly.
2. **Mask in the request log.** Stores the placeholders in ClickHouse instead of the raw values — even for detectors set to **Count only**, so prompts can go to an approved model intact while your request log keeps no personal data or secrets.

Before saving, **Try rules** in the console runs your detectors and custom patterns against sample text and shows the rewritten output without storing anything.

## Your own HTTP guardrail service

Built-in rules catch structured secrets and identifiers. When you need a classifier, a policy engine or a small local language model to check free-form text, add a guardrail of the kind **Your service**.

Kaeso sends an HTTP `POST` to your endpoint (for example `https://guardrail.example.com/check`) during the **request** phase, the **reply** phase, or both:

```json
{
  "id": "req_01KA2Z8M3C0Y6V1W9T4H5J7Q2E.request",
  "phase": "request",
  "request_id": "req_01KA2Z8M3C0Y6V1W9T4H5J7Q2E",
  "organization": "org_01K9ZB2M7VQ6CG3N8R4T5W1X2Y",
  "project": "proj_01K9ZB3D1F8H2J4K6M8N0P2Q4R",
  "key": "key_01K9ZB4E2G9J3K5M7P9R1T3V5X",
  "member": "usr_01K9ZB1A6C8E0G2J4L6N8Q0S2U",
  "service_account": null,
  "model": "claude-haiku-5-5",
  "format": "anthropic_messages",
  "source": "api",
  "messages": [
    { "role": "system", "content": [{ "type": "text", "text": "Answer in one sentence." }] },
    { "role": "user", "content": [{ "type": "text", "text": "Summarize order ORD-48291." }] }
  ]
}
```

Media parts (`image`, `audio`, `video`, `document`) appear with their `media_type` and `size` in bytes rather than raw binary payloads.

### Verifying the signature

Every call is signed using the [Standard Webhooks](https://www.standardwebhooks.com) specification. Kaeso sends `webhook-id`, `webhook-timestamp` (Unix seconds) and `webhook-signature` (`v1,` followed by the Base64 HMAC-SHA256 of `{id}.{timestamp}.{body}`, keyed with the signing secret decoded from Base64 after stripping `whsec_`).

### Returning a verdict

Your service answers within the configured timeout (2,000 ms by default, up to 10,000 ms) with a `2xx` status and one of three JSON actions:

```json
{ "action": "allow" }
```

```json
{ "action": "block", "message": "Customer records cannot be sent to external models.", "category": "pii" }
```

```json
{
  "action": "replace",
  "category": "pii",
  "messages": [
    { "role": "system", "content": [{ "type": "text", "text": "Answer in one sentence." }] },
    { "role": "user", "content": [{ "type": "text", "text": "Summarize order [REDACTED]." }] }
  ]
}
```

Each guardrail sets its own failure policy (**Refuse the request** with `503` — or `500` in OpenAI formats — or **Let it through**) and, for reply checks, whether streamed responses **Stream unchecked** or **Hold and check** the full reply before streaming it to the client.

If your guardrail service runs on the same server, a private LAN or an internal Kubernetes Service, allow its network in `KAESO_GUARDRAILS_ALLOWED_NETWORKS` (`guardrails.allowed_networks` in `kaeso.toml`); see the [Linux server guide](https://kaeso.app/articles/deploy-kaeso-on-a-linux-server#what-0-7-adds-for-a-server) or [Kubernetes guide](https://kaeso.app/articles/deploy-kaeso-on-kubernetes#settings).

## Where guardrails run in the request flow

Guardrails check chat requests across all four chat formats — OpenAI Chat Completions, OpenAI Responses, Anthropic Messages and Gemini — as well as the Playground. Embeddings, rerank, decisions and media endpoints pass unchecked.

The request phase runs **once per request**:

1. After API key authentication, request-rate checks and model resolution. (A caller over a request-rate limit gets `429` before any guardrail is called.)
2. **Before** the response cache and **before** the first step of an [alias route](https://kaeso.app/articles/alias-routes).

That ordering guarantees two things: a blocked request is never answered from the response cache, and when an alias route falls back from one model to another ([retries and fallbacks](https://kaeso.app/articles/retries-and-fallbacks)), every step in the route receives the already-sanitized text without re-running the request guardrails. A request blocked by a guardrail never reaches a provider, never triggers a fallback, and costs `$0`.

## Governance and observability

Only organization members with `guardrails.manage` can create guardrails or assign them to a project under the project's **Settings** › **Guardrails**. Project admins can see which guardrails apply to their project and in what order, but cannot remove them.

Every checked response includes a structured `kaeso-guardrails` header reporting each guardrail's name, phase, action, category and duration in milliseconds:

```http
kaeso-guardrails: "Built-in PII";phase=request;action=replace;dur=1, "Policy service";phase=request;action=allow;dur=14
```

In the console, the [request log](https://kaeso.app/articles/request-logs-and-privacy) filters by **Guardrail** verdict (allowed, blocked, rewritten) and shows each guardrail's outcome and detector counts in the request inspector. Prometheus exposes `kaeso_guardrail_runs_total{phase, action}`, `kaeso_guardrail_duration_seconds{phase}` and `kaeso_masked_values_total{detector, action}` on port 9100.

## Next steps

- [Guardrails in the docs](https://docs.kaeso.app/console/guardrails): full JSON schema, detectors and permissions.
- [Request logs and privacy](https://kaeso.app/articles/request-logs-and-privacy): how content retention, `kaeso-log-content: off` and log redaction work together.
- [Request flow](https://docs.kaeso.app/concepts/request-flow): every check a request passes from key lookup to reply.

Product names in this article belong to their owners and are used only to name the software and services involved. Kaeso is not affiliated with or endorsed by them.

By Kaeso Team

## More articles

- [Run Kaeso on a Raspberry Pi](https://kaeso.app/articles/deploy-kaeso-on-a-raspberry-pi.md): Run Kaeso 0.7.0 on a 64-bit Raspberry Pi 5 or 4 at home. Hardware and NVMe storage, Raspberry Pi OS (64-bit), Docker, reaching home behind DS-Lite or CGNAT, backups and updates.
- [Retries, cooldowns and fallbacks on model requests](https://kaeso.app/articles/retries-and-fallbacks.md): How Kaeso 0.7 keeps model requests running when a provider drops a connection, returns 5xx or hits a rate limit — and why timeouts and 4xx refusals are never retried on the same model.

[See all articles](https://kaeso.app/articles.md)

## Own the gateway. Own the record

```bash
curl -fsSL https://kaeso.app/install.sh | sh
```

[Install Kaeso](https://docs.kaeso.app/self-hosting/installation) · [Open the live demo](https://demo.kaeso.app)
