Cookies

With your permission, we use Google Analytics cookies to measure visits and improve the site. Necessary cookies are always on. You can change your choice or withdraw consent anytime in the footer, under Cookie settings. Cookie Policy

Skip to content

Advanced Agents ·Intermediate

AI Agent Governance - Policy-Based Sandboxing

About this app

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

  • Complete open-source code
  • Step-by-step tutorial

Technical sheet

Category
Advanced Agents
Difficulty
Intermediate
Models
OpenAI
Frameworks
LangChain
Runs locally?
Needs cloud APIs
Required keys
OPENAI_API_KEY

License Apache-2.0 · content curated and maintained by Doutor AI.

Full tutorial

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

  1. Policy Definition: Define your security policies in YAML format
  2. Action Wrapping: Wrap your agent's tools with the governance layer
  3. Interception: Before any tool executes, the policy engine validates the action
  4. Decision: Actions are allowed, denied, or require human approval
  5. 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

  1. Clone this repository:
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
cd advanced_ai_agents/single_agent_apps/ai_agent_governance
  1. Install the required packages:
pip install -r requirements.txt

Usage

  1. Set your API key:
export OPENAI_API_KEY=your-openai-api-key
  1. Run the governance demo:
python ai_agent_governance.py
  1. Try different actions and see how the policy engine handles them.

Example Policy Configuration

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:

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.ALLOW

Action Interception

Tools are wrapped with governance checks:

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 wrapper

Audit Logging

All actions are logged with full context:

{
    "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

  1. Deterministic vs Probabilistic Safety: Why policy enforcement is more reliable than prompt engineering
  2. Defense in Depth: Multiple layers of validation for robust security
  3. Audit Trails: Importance of logging for compliance and debugging
  4. 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
  • LangChain - LLM application framework
  • Guardrails AI - Input/output validation for LLMs

From the same category

Advanced AgentsIntermediate

AI Consultant Agent with Google ADK

A powerful business consultant powered by Google's Agent Development Kit that provides comprehensive market analysis, strategic planning, and actionab…

  • Gemini
Advanced AgentsIntermediate

AI Customer Support Agent

This Streamlit app implements an AI-powered customer support agent for synthetic data generated using GPT-4o. The agent uses OpenAI's GPT-4o model and…

  • OpenAI

Need a tailor-made solution?

Not sure where to start with AI in your business? Get a free assessment and a tailored plan - no commitment.

Save hours every week with AI, no hassle

Doutor AI gives you 1,859 ready-to-copy AI prompts, by industry and in plain English - plus a complete technical library for those who want to go further.

See plans