How to Add Guardrails to Production AI Agents
You add guardrails to an AI agent in layers, wrapped around three points where things go wrong: the input, the output, and the actions. On the input, validate and sanitize what reaches the model and screen for prompt injection and off-topic or abusive requests. On the output, check what the model produced before anyone sees it: schema and format checks, policy and safety filters, and grounding checks against your sources. On the actions, control what the agent is allowed to do: allowlist its tools, scope its permissions, cap cost and rate, and require human approval for anything risky or irreversible. Guardrails are not one filter you switch on; they are defense in depth, deterministic code wrapped around a probabilistic model. The model decides what it wants to do, and your guardrails decide what it is permitted to do.
I'm Mahmoud Zalt, an independent AI architect with 16 years building production software. I run Sista AI, where I help teams ship agents that stay safe under real-world load.
The Three Places Guardrails Live
Every effective guardrail sits at one of three checkpoints. Naming them makes it obvious where a given risk should be handled.
| Checkpoint | Guards against | Typical controls |
|---|---|---|
| Input | Prompt injection, abuse, off-topic or unsafe requests | Validation, sanitization, injection screening, topic limits |
| Output | Malformed data, policy violations, hallucinated claims | Schema checks, safety and policy filters, grounding checks |
| Action | Unauthorized, costly, or irreversible operations | Tool allowlists, scoped permissions, rate and cost caps, human approval |
The reason to separate them is that the same failure can be caught cheaply at one checkpoint and expensively at another. Blocking a malicious instruction at the input costs you a validation call. Catching it only after the agent has already sent an email costs you a customer incident. Push each guardrail as early as it can reasonably go.
Layer by Layer: What to Actually Add
A practical build order, from the cheapest and most universal to the most situational:
- Input validation and injection screening. Treat user and retrieved content as untrusted. Strip or neutralize instructions hiding in data, and reject inputs that fall outside the agent's job.
- Structured output contracts. Ask the model for a defined structure and validate the response against a schema. If it does not conform, reject and retry rather than passing malformed data downstream.
- Policy and safety filtering. Run outputs through content and policy checks before they reach a user, sized to your domain's real risk, not a token gesture.
- Grounding checks. For factual answers, verify claims trace back to retrieved sources, so the agent cites rather than invents.
- Action controls. Allowlist the tools the agent may call, scope each tool's permissions to the minimum, and cap how often and how expensively it can act.
- Human-in-the-loop approval. For irreversible or high-stakes actions, insert an explicit human checkpoint before execution.
You will not need every layer on every agent. A read-only internal assistant needs far less than one that can move money. Match the depth of the guardrails to the blast radius of a mistake.
Guardrails Without Grinding the Agent to a Halt
Every guardrail adds latency, cost, or friction, so the goal is not maximum guarding but the right guarding. Over-guard a low-risk agent and you get something slow, expensive, and annoying that people route around. Under-guard a high-risk one and you get an incident. The balance comes from a simple question asked per action: what is the worst thing that happens if this goes wrong?
- Cheap, deterministic checks first. Schema validation and allowlists cost almost nothing and catch a lot. Reserve model-based checks for where they earn their latency.
- Fail closed on the risky path, open on the safe one. When a guardrail is unsure about a high-stakes action, block and escalate. For low-stakes output, degrade gracefully instead of erroring out.
- Log every trip. A guardrail that blocks silently teaches you nothing. Record what was caught so you can tune thresholds and spot new attack patterns.
Frequently Asked Questions
What are guardrails for AI agents?
Guardrails are the controls that constrain what an agent can receive, produce, and do. They sit at three checkpoints, input, output, and action, and are built mostly from deterministic code around the model: validation, filters, permission scoping, and human approval steps.
Can I just use the prompt to add guardrails?
Prompt instructions help but are not guardrails on their own, because a prompt can be overridden by clever input or simply ignored. Real guardrails are enforced in code that runs regardless of what the model decides, so a jailbroken prompt still cannot trigger a forbidden action.
What is prompt injection and how do guardrails help?
Prompt injection is when malicious instructions hidden in user input or retrieved content try to hijack the agent. Guardrails help by treating all such content as untrusted, screening inputs, and, crucially, enforcing permissions on the action side so even a hijacked model cannot do anything it is not allowed to.
Do guardrails slow the agent down?
Some do. Deterministic checks like schema validation and allowlists are nearly free, while model-based safety checks add latency and cost. Match the depth of guarding to the blast radius of a mistake so you spend that budget only where the risk justifies it.
Ship Agents That Stay Inside the Lines
Guardrails are what separate a promising demo from an agent you can safely put in front of customers. The pattern is always the same: layer deterministic checks around a probabilistic model at the input, output, and action, and size each layer to the damage a mistake could do. Do that and the agent stays useful without becoming a liability.
If you want to design a guardrail layer for your own agent, injection screening, output contracts, permission scoping, and human-in-the-loop approval tuned to your risk, that is core to my hands-on AI Agents for Engineers masterclass, alongside agent architecture, tools and function calling, memory and retrieval, orchestration, and evals. It is always private, one-on-one or with your team, from $120 for a single technical session, $420 for a four-session Engineering track, or $780 for a private team workshop.
Design your guardrails in the AI Agents for Engineers masterclass







