๐ก๏ธ AI Agent Governance - Policy-Based Sandboxing โ
Learn how to build a governance layer that enforces deterministic policies on AI agents, preventing dangerous actions before they execute.
Features โ
- Policy-Based Sandboxing: Define what your AI agent can and cannot do using declarative policies
- Action Interception: Catch and validate agent actions before execution
- Audit Logging: Full trail of agent actions for compliance and debugging
- File System Guards: Restrict read/write to specific directories
- Network Guards: Allowlist-only external API access
- Rate Limiting: Prevent runaway agents with configurable limits
How It Works โ
- Policy Definition: Define your security policies in YAML format
- Action Wrapping: Wrap your agent's tools with the governance layer
- Interception: Before any tool executes, the policy engine validates the action
- Decision: Actions are allowed, denied, or require human approval
- Audit: All decisions are logged for compliance and debugging
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Agent โโโโโโถโ Governance โโโโโโถโ Tool โ
โ (LLM) โ โ Layer โ โ Execution โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ
โโโโโโโโผโโโโโโโ
โ Policy โ
โ Engine โ
โโโโโโโโโโโโโโโRequirements โ
- Python 3.8+
- OpenAI API key (or any LLM provider)
- Required Python packages (see
requirements.txt)
Installation โ
Clone this repository:
bashgit clone https://github.com/Shubhamsaboo/awesome-llm-apps.git cd advanced_ai_agents/single_agent_apps/ai_agent_governanceInstall the required packages:
bashpip install -r requirements.txt
Usage โ
Set your API key:
bashexport OPENAI_API_KEY=your-openai-api-keyRun the governance demo:
bashpython ai_agent_governance.pyTry different actions and see how the policy engine handles them.
Example Policy Configuration โ
yaml
policies:
filesystem:
allowed_paths: ["/workspace", "/tmp"]
denied_paths: ["/etc", "/home", "~/.ssh"]
network:
allowed_domains: ["api.openai.com", "api.github.com"]
block_all_others: true
execution:
max_actions_per_minute: 60
require_approval_for: ["delete_file", "execute_shell"]
tools:
allowed: ["read_file", "write_file", "web_search"]
denied: ["execute_code", "send_email"]Example Output โ
๐ก๏ธ AI Agent Governance Demo
============================
๐ Loading policy: workspace_sandbox.yaml
๐ค Agent request: "Read the contents of /etc/passwd"
โ DENIED: Path '/etc/passwd' is outside allowed directories
๐ค Agent request: "Write analysis to /workspace/report.md"
โ
ALLOWED: Action permitted by policy
๐ค Agent request: "Make HTTP request to unknown-api.com"
โ DENIED: Domain 'unknown-api.com' not in allowlist
๐ค Agent request: "Delete /workspace/temp.txt"
โธ๏ธ PENDING: Action requires human approval
[Y/n]:Technical Details โ
Policy Engine โ
The policy engine evaluates actions against a set of rules:
python
class PolicyEngine:
def evaluate(self, action: Action) -> Decision:
# Check each policy rule
for rule in self.rules:
result = rule.evaluate(action)
if result.is_terminal:
return result
return Decision.ALLOWAction Interception โ
Tools are wrapped with governance checks:
python
def governed_tool(func):
def wrapper(*args, **kwargs):
action = Action(name=func.__name__, args=args, kwargs=kwargs)
decision = policy_engine.evaluate(action)
if decision == Decision.DENY:
raise PolicyViolation(decision.reason)
elif decision == Decision.REQUIRE_APPROVAL:
if not get_human_approval(action):
raise PolicyViolation("Human denied the action")
# Log the action
audit_log.record(action, decision)
return func(*args, **kwargs)
return wrapperAudit Logging โ
All actions are logged with full context:
python
{
"timestamp": "2024-01-15T10:30:00Z",
"action": "write_file",
"args": {"path": "/workspace/report.md"},
"decision": "ALLOW",
"policy_matched": "filesystem.allowed_paths",
"agent_id": "research-agent-001"
}Key Concepts Learned โ
- Deterministic vs Probabilistic Safety: Why policy enforcement is more reliable than prompt engineering
- Defense in Depth: Multiple layers of validation for robust security
- Audit Trails: Importance of logging for compliance and debugging
- Principle of Least Privilege: Only grant the permissions agents actually need
Extending the Tutorial โ
- Add custom policy rules for your use case
- Implement human-in-the-loop approval workflows
- Connect to external policy management systems
- Add real-time monitoring and alerting