Skip to content

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.

Published
Reading time
4 min
Author
Kaeso Team
A bright gradient of green and teal, folding diagonally downward, with a warm light in one corner

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
Fig. 01Built-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:

  • Secrets and API keys
    What it matches
    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
    Placeholder
    [API_KEY_1], [PRIVATE_KEY_1], [TOKEN_1]
  • Email addresses
    What it matches
    Addresses such as ada@example.com
    Placeholder
    [EMAIL_1]
  • Phone numbers
    What it matches
    Numbers in E.164 format, such as +4930123456
    Placeholder
    [PHONE_1]
  • IBANs
    What it matches
    International bank account numbers whose mod-97 checksum holds
    Placeholder
    [IBAN_1]
  • Payment card numbers
    What it matches
    13 to 19 digits (with spaces or hyphens) whose Luhn checksum holds
    Placeholder
    [CARD_1]
  • IP addresses
    What it matches
    IPv4 and IPv6 addresses
    Placeholder
    [IP_1]
  • Custom patterns (up to 20)
    What it matches
    A lowercase name (order_id) and a regular expression up to 500 characters evaluated in linear time by Rust's regex engine
    Placeholder
    [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:

{  "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 (opens in a new tab) 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:

{ "action": "allow" }
{ "action": "block", "message": "Customer records cannot be sent to external models.", "category": "pii" }
{  "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 or Kubernetes guide.

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.

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), 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:

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 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

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

On this page

Own the gateway.Own the record.

curl -fsSLhttps://kaeso.app/install.sh | sh