Testing & reliability → Input guardrails

Input Guardrails for LLM Apps Explained: Cheapest Place to Stop Problems

What it is

Input guardrails inspect the user's request (and anything attached to it) before it reaches the expensive parts of the pipeline:

CheckPurposeTypical action
PII detection/maskingPrevent sending personal data to third-party models; comply with policy (PII)Mask with reversible tokens, or block
Topic classificationKeep the system in scope; route out-of-domain requestsRefuse with scope explanation (refusal / no-answer path)
Abuse/toxicityBlock harassment, illegal requestsRefuse, log, rate-limit repeat offenders
Injection screeningDetect obvious prompt-injection attempts in user textFlag (rarely block — legitimate users write odd things)
Size/rate limitsPrevent cost and DoS (capacity & limits)Reject, throttle
Language detectionRoute to the right model/index (model choice)Route or decline unsupported languages
Secrets detectionUsers paste API keys and passwords constantlyMask and warn

The design tension: guardrails add latency to every request and produce false positives that block legitimate use. The resolution is a tiered approach — cheap deterministic checks on everything, model-based classification only where the cheap checks are insufficient, and a bias toward flagging rather than blocking except for clear-cut categories.

How to implement

  1. Order by cost: size/rate limits → regex/pattern checks (secrets, obvious PII formats) → NER-based PII → classifier-based topic/abuse → (only if needed) an LLM check.
  2. PII: detect, then decide by policy per tenant and per data category: mask reversibly (keep a mapping so the answer can contain real values), redact irreversibly, or block the request.
  3. Reversible masking: replace with typed placeholders (<PERSON_1>, <EMAIL_2>), keep the mapping in the request context, and restore in the final answer — never store the mapping longer than the request (PII).
  4. Topic classification against your scope, with the out-of-scope path producing a specific refusal ("I cover HR policies; that's a billing question") rather than a generic one.
  5. Fail open or closed deliberately: if the guardrail service times out, decide per check — PII masking fails closed (block), topic classification fails open (proceed).
  6. Log every decision with the reason and the matched span; never log the raw sensitive value.
  7. Measure false positives: sample blocked requests for review. An over-aggressive guardrail is invisible in metrics and very visible to users.

Small example

import re
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine

analyzer, anonymizer = AnalyzerEngine(), AnonymizerEngine()

SECRETS = re.compile(r"(sk-[A-Za-z0-9]{20,}|AKIA[0-9A-Z]{16}|ghp_[A-Za-z0-9]{36}|-----BEGIN [A-Z ]*PRIVATE KEY-----)")
MAX_CHARS = 8000

class GuardrailResult(BaseModel):
    allow: bool
    reason: str | None = None
    masked_text: str | None = None
    mapping: dict = {}                     # placeholder -> original (request-scoped only)
    flags: list[str] = []

def input_guardrails(text: str, caller, policy) -> GuardrailResult:
    # 1. cheap, deterministic
    if len(text) > MAX_CHARS:
        return GuardrailResult(allow=False, reason="input_too_long")
    if not rate_limiter.allow(caller.id):
        return GuardrailResult(allow=False, reason="rate_limited")
    if SECRETS.search(text):
        text = SECRETS.sub("<REDACTED_SECRET>", text)                  # always redact, never store
        flags = ["secret_redacted"]
    else:
        flags = []

    # 2. PII detection with per-tenant policy
    findings = analyzer.analyze(text=text, language="en",
                                entities=policy["pii_entities"], score_threshold=0.6)
    mapping = {}
    if findings:
        if policy["pii_action"] == "block":
            log_guardrail(caller, "blocked", "pii", entities=[f.entity_type for f in findings])
            return GuardrailResult(allow=False, reason="pii_not_permitted", flags=flags + ["pii_blocked"])
        masked, mapping = mask_reversible(text, findings)              # <PERSON_1>, <EMAIL_1>, ...
        text, flags = masked, flags + ["pii_masked"]

    # 3. topic / abuse classification (fast tier, cached prompt)
    cls = classify_input(text)                                          # {"in_scope": bool, "abusive": bool, "topic": str}
    if cls["abusive"]:
        log_guardrail(caller, "blocked", "abuse", topic=cls["topic"])
        abuse_counter.increment(caller.id)
        return GuardrailResult(allow=False, reason="abusive_content", flags=flags)
    if not cls["in_scope"]:
        return GuardrailResult(allow=False, reason=f"out_of_scope:{cls['topic']}", flags=flags)

    # 4. injection screening — flag, don't block (users legitimately write strange things)
    if INJECTION_PATTERNS.search(text):
        flags.append("possible_injection")                              # downstream: no privileged actions (prompt-injection defence)

    log_guardrail(caller, "allowed", None, flags=flags)
    return GuardrailResult(allow=True, masked_text=text, mapping=mapping, flags=flags)

def restore_pii(answer: str, mapping: dict) -> str:
    """Put real values back only in the response to the user who supplied them."""
    for placeholder, original in mapping.items():
        answer = answer.replace(placeholder, original)
    return answer

def handle(request, caller, policy):
    g = input_guardrails(request.text, caller, policy)
    if not g.allow:
        return guardrail_refusal(g.reason)                              # specific, not generic (refusal / no-answer path)
    out = pipeline.answer(g.masked_text, caller, flags=g.flags)
    return out | {"answer": restore_pii(out["answer"], g.mapping)}

Pros

  • Cheapest place to stop a problem — before retrieval, before generation, before tokens are spent.
  • PII masking enables third-party model use in environments that would otherwise forbid it (PII).
  • Scope enforcement improves UX: a specific out-of-scope refusal is more useful than a hallucinated answer.
  • Deterministic checks are fast and reliable — secrets, size, rate.
  • Abuse blocking protects the service and other users.

Cons

  • Latency on every request (10–200 ms depending on tiers).
  • False positives are costly and invisible: a legitimate request blocked as "out of scope" looks like a broken product to that user.
  • PII detection is imperfect — NER misses names in unusual formats and over-flags common words; thresholds need tuning per language and domain.
  • Masking can break meaning: a question about "Jane's contract" becomes about "<PERSON_1>'s contract", which may harm retrieval (mask after retrieval where possible).
  • Injection screening on user input has limited value — the real injection risk is in retrieved content (prompt-injection defence).

When to use

  • PII masking: whenever data flows to a third-party model and policy requires it, or for regulated tenants.
  • Topic/abuse: public-facing systems, and internal ones where scope confusion is common.
  • Size/rate/secrets: always — they're free.

When NOT to use

  • Heavy classification on trusted internal traffic where scope confusion is rare — measure the block rate first; if it's 0.1%, the latency isn't buying much.
  • Blocking on injection-pattern matches in user input: too many false positives, and the user is usually attacking only themselves.
  • PII masking that breaks retrieval quality — consider masking at the model boundary instead of at ingress.

Production at scale

Mask at the model boundary, not necessarily at ingress. Retrieval often needs the real names to find the right documents. A better pattern for many systems: retrieve with real values inside your trust boundary, then mask only what crosses to a third-party model, and restore on the way back. This preserves quality and satisfies the policy.

Per-tenant policy, not global. Regulated tenants block; others mask; internal tools may do neither. Encode as configuration, and make the policy visible in the trace (data residency & retention).

Tier the checks and short-circuit. Most requests pass everything; run the free checks first and only invoke classifiers when needed. Cache classification by normalised query for repeated inputs.

Fail-open/fail-closed decided per check and documented. A PII masker that fails open sends personal data to a third party; a topic classifier that fails closed blocks legitimate work. Both defaults are wrong in the other's context.

False-positive review is mandatory. Sample blocked requests weekly. Guardrail false positives don't appear in quality metrics — the request never reached the pipeline — so they're invisible unless you look.

Don't log the sensitive values. Log entity types and offsets, never the matched text. The guardrail log is itself a PII risk (PII).

Rate limits per user and per tenant (capacity & limits), with abuse counters that escalate: warn, throttle, block, notify an admin.

Multilingual: PII and abuse classifiers are much weaker outside English. Detect language and either route to a language-appropriate model or apply a conservative policy, and know your coverage gaps.

Prod checklist

  • Tiered checks: size/rate → patterns (secrets) → PII NER → classifiers, short-circuiting early
  • Per-tenant PII policy (mask / block / allow) in config; visible in the trace
  • Reversible masking with request-scoped mapping; restoration in the response only
  • Masking applied at the third-party boundary where retrieval quality would suffer at ingress
  • Fail-open/fail-closed decided and documented per check
  • Specific out-of-scope refusals naming the actual scope
  • Injection patterns flag (not block) and suppress privileged actions downstream
  • Guardrail decisions logged with entity types and reasons, never raw values
  • Weekly false-positive review of blocked requests
  • Language detection with known coverage gaps documented

Metrics to watch

MetricWhy
Block rate by reasonWhether guardrails are earning their latency
False-positive rate (review sample)Invisible user harm
PII detection rate and entity mixPolicy calibration
Guardrail p95 latencyCost on every request
Abuse escalations per tenantService protection