All articles
January 25, 2025 8 min read

Guardrails: the line between an agent demo and one you can deploy

An agent with real tools and real users needs more than a clever prompt. Input and output guardrails are what keep it safe, on-topic, and out of trouble in production.

Written forEngineeringProduct
GuardrailsAI AgentsSafety

The gap between an agent that wows in a demo and one you'd actually put in front of customers is mostly guardrails. A demo trusts the model to behave. Production assumes it won't — and wraps every input and output in checks that keep a bad turn from becoming a bad incident.

User inputInput guardrailfailBlock / refusepassAgent · LLM + toolsOutput guardrailfailRedact / retrypassResponse
Guardrails wrap the agent on both sides: inputs are screened before the model sees them, outputs before the user does.

Two layers: input and output

  • Input guardrails — screen for prompt injection, PII, jailbreak attempts, and off-topic or abusive requests before the agent ever runs.
  • Output guardrails — validate the response against a schema, check for hallucinated facts and PII leakage, and catch toxic or off-brand content before it reaches the user.

Deterministic checks and model-based checks

Guardrails come in two kinds, and a serious system uses both. Deterministic guardrails are plain code — regex, schema validation, allowlists, length and rate caps. They're fast, free, and completely reliable for the things they cover. Model-based guardrails use a classifier or an LLM to judge the fuzzy things rules can't: a moderation model (such as Llama Guard or a provider's moderation endpoint) for unsafe content, or an LLM-as-judge for relevance and grounding. Reach for deterministic checks first — they're cheaper and surer — and add model-based ones only where the judgement is genuinely linguistic.

The checks worth running

  • Relevance — is the request, and the answer, actually on-topic for this product?
  • Safety / moderation — screen inputs and outputs for toxic, harmful, or disallowed content.
  • PII — detect and redact personal data on the way in and the way out.
  • Jailbreak & prompt injection — catch attempts to override the system prompt or smuggle instructions through content.
  • Grounding / hallucination — for RAG, check the answer is actually supported by the retrieved sources.
  • Format — validate the output against the schema your code expects to parse.

You rarely need all of them — pick the checks that match your app's real risks, and don't pay for the ones that don't.

Guardrail the tools, not just the text

The scariest thing an agent does isn't talk — it's act. Every tool is attack surface. Default tools to read-only, scope credentials narrowly, validate arguments before execution, and require human approval for anything irreversible. A guardrail on the language means nothing if the agent can still drop a table.

Fail safe, not silent

When a guardrail trips, it needs a defined next step — not a swallowed error. Depending on the case: block and refuse, redact and continue, regenerate with feedback, or hand off to a human. And log every trip, because your guardrail failures are your best early-warning signal.

The shape of a guarded agent
const clean = await inputGuards(userInput);   // injection, PII, topic
if (!clean.ok) return refuse(clean.reason);

const draft = await agent.run(clean.input, { tools: readOnlyByDefault });

const safe = await outputGuards(draft);        // schema, PII, grounding
return safe.ok ? safe.output : handoffToHuman(draft);

Mind the latency

Every guardrail is another check, and checks cost time. Run independent guardrails in parallel rather than in series, and keep the cheap deterministic ones in front of the expensive model-based ones so most requests never reach a second model. A useful pattern is optimistic execution: start streaming the answer while the output guardrails run, and halt or replace it only if a check trips — so you get safety without making every response feel slow. If you'd rather not build all this yourself, toolkits like NeMo Guardrails and Guardrails AI package many of these patterns.

You don't earn trust with a better prompt. You earn it by assuming the model will fail and being ready when it does.
Building something with LLMs?
I help teams ship GenAI that’s reliable and cost-efficient.
Let’s talk