Multi-Layer Guardrail System

Medium
Agents

Implement multi-layer guardrail system:

Layers:

  1. Input: Validate user queries before processing
  2. Output: Validate agent responses before delivery
  3. Tool: Validate tool calls before execution

Guard Function: guard_fn(data) -> {'violation': bool, 'reason': str, 'severity': 'low|medium|high'}

Methods:

  1. add_guard(layer, guard_fn, severity): Register guard
  2. validate_*: Run appropriate layer guards
  3. process_violation: Handle violations
    • low: log only
    • medium: warn + log
    • high: block + alert

Return Format: {'allowed': bool, 'violations': [...], 'action_taken': str}

Examples

Example 1:
Input: g = MultiLayerGuardrail(); g.add_guard('input', lambda x: {'violation': 'test' in x, 'reason': 'test', 'severity': 'high'}, 'high'); g.validate_input('hello')
Output: {'allowed': True, 'violations': [], 'action_taken': 'none'}
Explanation: No violations, input allowed

Starter Code

class MultiLayerGuardrail:
    """
    Multi-layer guardrail with input, output, and tool validation.
    """
    
    def __init__(self):
        self.layers = {
            'input': [],
            'output': [],
            'tool': []
        }
        self.violation_log = []
    
    def add_guard(self, layer, guard_fn, severity='medium'):
        """Add guard function to specified layer"""
        # Your implementation here
        pass
    
    def validate_input(self, user_input):
        """Validate user input through input guards"""
        # Your implementation here
        pass
    
    def validate_output(self, agent_output, context):
        """Validate agent output through output guards"""
        # Your implementation here
        pass
    
    def validate_tool_call(self, tool_name, params):
        """Validate tool call through tool guards"""
        # Your implementation here
        pass
    
    def process_violation(self, violation, layer):
        """Handle guard violation based on severity"""
        # Your implementation here
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews