Guardrails
Guardrails are a comprehensive set of checks that run around every agent action to enforce safety, security, and quality standards.
8 min•By Priygop Team•Updated 2026
Guardrail Stack
Guardrail Stack
# Comprehensive guardrail stack for agent actions
class AgentGuardrails:
"""
Enforces all safety, security, and quality checks for agent actions.
Every tool call passes through this stack before execution.
"""
def __init__(self, agent_role: str, agent_permissions: set,
user_email: str, loop_detector):
self.role = agent_role
self.permissions = agent_permissions
self.user_email = user_email
self.loop_detector = loop_detector
def validate(self, tool_name: str, args: dict, state: dict) -> dict:
"""
Run all guardrails. Returns 'allow', 'block', or 'requires_approval'.
"""
checks = []
# 1. Permission check
from_tool_perms = TOOL_PERMISSIONS if 'TOOL_PERMISSIONS' in dir() else {}
required_perm = {"issue_refund": "issue_refund", "delete_record": "delete_record",
"send_email": "send_email"}.get(tool_name)
if required_perm and required_perm not in self.permissions:
return {"decision": "block", "reason": f"Missing permission: {required_perm}"}
checks.append("permission ✓")
# 2. Unsafe call check
if tool_name in {"exec_shell", "eval_code"}:
return {"decision": "block", "reason": "Blocked tool"}
checks.append("unsafe_call ✓")
# 3. Action restriction check
if tool_name == "issue_refund":
amount = args.get("amount", 0)
if amount > 500:
return {"decision": "requires_approval",
"reason": f"Refund $" + str(amount) + " requires manager approval"}
checks.append("restrictions ✓")
# 4. Loop detection
loop = self.loop_detector.check(tool_name, args)
if loop["should_stop"]:
return {"decision": "block", "reason": f"Loop detected: {loop['reason']}"}
checks.append("loop_detection ✓")
return {"decision": "allow", "checks_passed": checks}
# Create and test guardrails
class SimpleLoopDetector:
def check(self, action, args): return {"should_stop": False}
guardrails = AgentGuardrails(
agent_role="support_agent",
agent_permissions={"read_orders", "issue_refund", "send_email"},
user_email="alice@example.com",
loop_detector=SimpleLoopDetector()
)
print(guardrails.validate("issue_refund", {"amount": 50}, {})) # allow
print(guardrails.validate("issue_refund", {"amount": 600}, {})) # requires_approval
print(guardrails.validate("delete_record", {"id": "X"}, {})) # block
print(guardrails.validate("exec_shell", {"cmd": "ls"}, {})) # block