Tool Misuse Detector

Medium
Agents

Implement tool misuse detection for agent safety:

  1. add_pattern(name, check_fn): Register pattern checker
    • check_fn(tool_name, params, context, history) -> alert dict or None
  2. check_usage(tool, params, context): Run all pattern checks
  3. record_usage(tool, params, result): Store for history-based checks
  4. check_rate_limit(tool, max, window): Check call frequency

Built-in Patterns:

  • SQL injection keywords in query params
  • File path traversal (../)
  • Excessive frequency (rate limiting)
  • Sensitive data exposure (passwords in logs)

Severity Levels: low/medium/high based on potential impact

Examples

Example 1:
Input: detector = ToolMisuseDetector(); detector.record_usage('sql', {'query': 'SELECT * FROM users'}, 'ok'); detector.check_rate_limit('sql', 5, 60)
Output: True
Explanation: Only 1 call recorded, under limit of 5

Starter Code

class ToolMisuseDetector:
    """
    Detect potentially harmful or incorrect tool usage patterns.
    """
    
    def __init__(self):
        self.suspicious_patterns = []
        self.usage_history = []
    
    def add_pattern(self, pattern_name, check_fn):
        """Register misuse pattern checker"""
        # Your implementation here
        pass
    
    def check_usage(self, tool_name, params, context):
        """
        Check tool usage against all registered patterns.
        Returns list of {'pattern': name, 'severity': 'low|medium|high', 'reason': str}
        """
        # Your implementation here
        pass
    
    def record_usage(self, tool_name, params, result):
        """Record usage for pattern detection across history"""
        # Your implementation here
        pass
    
    def check_rate_limit(self, tool_name, max_calls, window_seconds):
        """Check if tool exceeded rate limit in window"""
        # Your implementation here
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews