Skip to content

Founder-led. Limited engagements.Talk to Us →

Part 22 of 30 · 30 Days of AI Agents

AI Agent Safety & Guardrails | 30 Days of AI Agents, Day 22

A practical guide to AI agent guardrails: input, action, output, context and workflow layers, prompt injection defense, least privilege, and human escalation.

By CodeITronics6 min readAI Agents · AI Safety · Guardrails · Security
AI Agent Safety & Guardrails | 30 Days of AI Agents, Day 22

Yesterday was about evaluating agents: test sets, metrics, judges, and the loop that keeps quality from drifting. Evaluation tells you how an agent behaves. Day 22 is about constraining that behavior: the guardrails that keep an agent inside safe boundaries even when its input is hostile or its reasoning goes sideways.

AI Agent Safety & Guardrails — CodeITronics infographic

Save this sheet. The rest of this note walks through it section by section.

What agent safety means

Agent safety means the agent behaves as intended, avoids harm, follows your rules, and operates within boundaries you defined. In practice that covers five goals: prevent harmful outputs, reduce hallucinations, protect users and their data, stay compliant, and earn trust.

Agents raise the stakes compared to chatbots because they act. A chatbot that hallucinates gives a wrong answer. An agent that hallucinates can issue a refund, delete a record, or email a customer. So the question is not only "what will it say?" but "what is it allowed to do?" The sheet's phrasing is a good design rule: safety first, intelligence second, trust always.

How guardrails wrap the agent

The flow on the sheet places a checkpoint at each stage of a request:

User input
  -> Input guardrails    (validate, filter, sanitize)
  -> Agent reasoning     (plan the next steps)
  -> Action guardrails   (check tools, API calls, permissions)
  -> Generate output
  -> Output guardrails   (safety, quality, policy)
  -> Safe response
Across every step: monitoring and logging, plus human oversight for risky cases

The key idea is defense in depth. No single layer is reliable on its own. Prompt injection detection misses novel attacks, so the action layer must still block tools the agent should never call. Output checks miss subtle errors, so high-impact actions still go to a human. Each layer assumes the one before it can fail.

The five types of guardrails

TypeWhat it checksExamples
InputWhat comes inPrompt injection, jailbreaks, PII detection, profanity, relevance
ActionWhat the agent doesTool and API allowlists, permission checks, rate limits, data access control, cost limits
OutputWhat goes outToxicity, bias, hallucination checks, quality and formatting rules
ContextWhat the agent sees and remembersData filtering, memory limits, sensitive data handling, retention policy
WorkflowHow the run progressesStep validation, loop protection, failure handling, escalation rules

If you only build one layer well, make it the action layer. Input and output checks are probabilistic: they classify text and sometimes get it wrong. Action guardrails can be deterministic. An allowlist either contains a tool or it does not. A permission check either passes or it does not. Code that runs outside the model cannot be talked out of its rules.

Worked example with a travel booking agent

The sheet walks a booking agent through each layer:

StageWhat it checksExample
Input guardrailsMalicious prompts or sensitive dataBlock "Ignore previous instructions..."
Action guardrailsAllowed tools and permissionsAllow the Flight API, block the Delete Booking API
Agent reasoningPlans stay within scopeSearch flights, check availability, calculate price
Output guardrailsSafety and accuracyNo PII leakage, no hallucinated prices
MonitoringErrors and anomaliesAlert on high error rates or unusual tool usage
Human oversightUncertain or high-risk casesRefund requests over $300 go to a human

Notice that "no hallucinated prices" is enforceable in code: every price in the response must match a price returned by a tool in this run. Wherever you can turn a policy into a comparison against tool output, do it. That is far more reliable than asking a second model whether the answer looks right.

A small guardrail layer in code

The sheet uses the Guardrails AI library. Library APIs change, so here is the same idea in plain Python you can adapt to any framework.

import re

class GuardrailViolation(Exception):
    pass

INJECTION_PATTERNS = [r"ignore (all |any )?previous instructions", r"reveal .*system prompt"]
ALLOWED_TOOLS = {"search_flights", "get_booking", "create_refund"}
REFUND_LIMIT = 300  # above this, a human approves

def check_input(text):
    for pattern in INJECTION_PATTERNS:
        if re.search(pattern, text, re.IGNORECASE):
            raise GuardrailViolation("possible prompt injection")

def check_action(tool, args, user):
    if tool not in ALLOWED_TOOLS:
        raise GuardrailViolation(f"tool not allowed: {tool}")
    if "booking_id" in args and not user.owns(args["booking_id"]):
        raise GuardrailViolation("booking belongs to another user")
    if tool == "create_refund" and args["amount"] > REFUND_LIMIT:
        return "escalate_to_human"
    return "allow"

def check_output(text, prices_from_tools):
    for price in re.findall(r"\$\d+(?:\.\d{2})?", text):
        if price not in prices_from_tools:
            raise GuardrailViolation(f"unverified price: {price}")

The regex input check is deliberately simple and will miss things; treat it as a cheap first filter and pair it with a classifier. The action and output checks are the ones doing real work, because they do not depend on the model's judgment.

Risks worth designing against

The sheet lists the usual threats: prompt injection and jailbreaks, data leakage and PII exposure, hallucinations, toxic or biased outputs, unauthorized actions and tool misuse, over-reliance on agent decisions, infinite loops and runaway agents, insecure integrations, and cost abuse.

Two get underestimated. Indirect prompt injection arrives through content the agent reads, such as a web page, an email, or a document, not through the user's message. Anything retrieved should be treated as untrusted data. Runaway loops are a cost and safety problem at once: cap steps, tool calls, and spend per run, and fail closed when a cap is hit.

The best practices follow from this: defense in depth, least-privilege access for tools and data, human review for critical decisions, regular red-team testing, design for failure with timeouts and safe defaults, and honesty with users about what the agent cannot do.

Tools and the safety checklist

The sheet lists Guardrails AI, NVIDIA NeMo Guardrails, Meta's Llama Guard, Azure AI Content Safety, the OpenAI Moderation API, and LangChain integrations. Use them for classification problems like toxicity, harmful content, and PII. Keep authorization and business rules in your own code.

Then work the checklist: define safety goals, identify risks, implement input, action, and output guardrails, set up monitoring, define escalation rules, test with red-team prompts, review false positives, update policies, and document decisions. The challenges are real too. Overly strict guardrails block legitimate users, and threats evolve, so review blocked requests regularly.

Key Takeaways

  • Agents act, so safety is about what they are allowed to do, not only what they say.
  • Layer input, action, output, context, and workflow guardrails; assume each layer can fail.
  • Put your strongest, deterministic checks on actions: allowlists, ownership checks, limits.
  • Verify facts like prices against tool outputs in code instead of asking another model.
  • Cap steps and spend per run, and route high-impact actions to a human.

FAQs

Is a strong system prompt enough to keep an agent safe?

No. Instructions in a prompt can be overridden by injected text. Enforce critical rules in code that runs outside the model.

Where should human approval sit?

On irreversible or high-value actions such as refunds, deletions, and external messages, and on cases where the agent's confidence is low.

How do I avoid guardrails that annoy legitimate users?

Log every block, review false positives weekly, and tune thresholds against real traffic rather than guesses.

Up Next

Day 23 covers Advanced RAG Techniques: hybrid search, query rewriting, reranking, contextual compression, and agentic retrieval that plans and verifies its own lookups. If you want guardrails designed into an agent your team is building, talk to us.

Keep reading

Evaluating AI Agents | 30 Days of AI Agents, Day 21

How to evaluate AI agents in practice: test sets, metrics by category, LLM-as-a-judge, human review, benchmarks, and a continuous evaluation loop that sticks.

Multi-Agent Collaboration Patterns | 30 Days of AI Agents, Day 20

Six multi-agent collaboration patterns explained: supervisor, peer-to-peer, hierarchical, pipeline, blackboard, swarm, and how to choose the right one.

AI Agent Planning & Reasoning | 30 Days of AI Agents, Day 19

How AI agents plan and reason: task decomposition, reasoning types, ReAct and Reflexion, a travel planner example, and a plan-execute-replan loop.

Subscribe to the edition

One useful systems note a week from real Salesforce and automation builds. Unsubscribe in one click.

Letters

Letters to the editor

No letters yet. Questions and counterpoints are welcome.