LLM guardrails
LLM guardrails are checks placed around the model. They are the first answer to the probabilistic risksProbabilistic risksThe model is probabilistic, so its failures are too: three threats, four things at stake.Opens in a new tab: instead of hoping the model refuses a bad request, something outside it looks at what comes in and what goes out.
Four kinds
| Guardrail | Watches | Typical job |
|---|---|---|
| Input | What comes in | Block prompt injection, filter content |
| Output | What goes out | Validate, check for hallucination, mask PII, keep to brand safety |
| Capability | What the agent can do | Limit tools and permissions to what the task needs |
| Process / flow | The path it takes | Fix the order of steps, cap loops, force approval at set points |
The first two guard the text. The last two guard the behavior, and they are the ones a clever prompt cannot talk its way past, because they are code, not instructions.
Rules or a model
LangChain’s docs split guardrails in two. Deterministic ones use regex, keywords and explicit checks: fast, predictable, cheap, but they miss nuance. Model-based ones use an LLM or a classifier: they catch what rules miss, but they are slower, costlier, and probabilistic themselves. Most setups use both.
In LangChain
An input guard is a hook that runs before the agent. If it matches, it ends the run before the model is called. The built-in PII middleware masks emails on the way out.
from typing import Any from langchain.agents import create_agentfrom langchain.agents.middleware import AgentState, PIIMiddleware, before_agentfrom langgraph.runtime import Runtime BLOCKED = ["ignore previous instructions", "reveal your system prompt"] @before_agent(can_jump_to=["end"])def input_guard(state: AgentState, runtime: Runtime) -> dict[str, Any] | None: text = str(state["messages"][-1].content).lower() if any(phrase in text for phrase in BLOCKED): return { "messages": [{"role": "assistant", "content": "I can't help with that."}], "jump_to": "end", } return None agent = create_agent( model=model, tools=[], middleware=[ input_guard, PIIMiddleware("email", strategy="mask", apply_to_output=True), ],)The keyword list is a deterministic check, easy to bypass, so it is a floor and not a wall. PII middleware also offers redact, hash and block strategies, and can check tool results.
Sources
- Guardrails, LangChain Docs · accessed October 5, 2026
- Built-in middleware, LangChain Docs · accessed October 5, 2026
- LLM01:2025 Prompt Injection, OWASP GenAI Security Project · accessed October 5, 2026