AI Security

AI Security Monitoring: Real-Time ML Threat Detection

Nicu Constantin
••14 min read
#AI security#monitoring#observability#anomaly detection#MLOps

Petru Constantin is the founder of DeviDevs and designed the multi-agent platform the company runs on, with guardrails enforced in code. Code examples are written against the Python 3.12+, prometheus-client 0.26 and the current Prometheus alerting rules format documentation.

AI Security Monitoring and Observability: Real-Time Threat Detection for ML Systems

AI systems require specialized monitoring beyond traditional application observability. This guide covers security monitoring patterns for detecting attacks, drift, and anomalies in ML deployments.

AI-Specific Monitoring Architecture

Core Monitoring Framework

from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import Dict, List, Optional, Callable, Any
from enum import Enum
from collections import deque
import threading
import logging
import uuid
 
import numpy as np
 
logger = logging.getLogger(__name__)
 
 
def utc_now() -> datetime:
    return datetime.now(timezone.utc)
 
 
class AlertSeverity(Enum):
    INFO = "info"
    WARNING = "warning"
    CRITICAL = "critical"
 
class ThreatCategory(Enum):
    INPUT_MANIPULATION = "input_manipulation"
    MODEL_EXTRACTION = "model_extraction"
    DATA_POISONING = "data_poisoning"
    ADVERSARIAL_ATTACK = "adversarial_attack"
    PROMPT_INJECTION = "prompt_injection"
    UNUSUAL_BEHAVIOR = "unusual_behavior"
 
@dataclass
class SecurityAlert:
    id: str
    timestamp: datetime
    category: ThreatCategory
    severity: AlertSeverity
    description: str
    details: Dict[str, Any]
    source_ip: Optional[str] = None
    user_id: Optional[str] = None
    model_id: Optional[str] = None
    request_id: Optional[str] = None
    mitigated: bool = False
 
@dataclass
class ModelMetrics:
    model_id: str
    timestamp: datetime          # timezone-aware UTC
    latency_ms: float
    input_size: int
    output_confidence: float
    client_id: Optional[str] = None   # API key, user or IP: who sent the request
    prediction_class: Optional[str] = None
    input_hash: Optional[str] = None
    anomaly_score: float = 0.0
 
 
def new_alert(**kwargs) -> SecurityAlert:
    return SecurityAlert(id=f"alert_{uuid.uuid4().hex}", timestamp=utc_now(), **kwargs)
 
 
class AISecurityMonitor:
    def __init__(self, config: Dict[str, Any]):
        self.config = config
        self.alerts: deque = deque(maxlen=10000)
        self.metrics_buffer: deque = deque(maxlen=50000)
        self.alert_handlers: List[Callable[[SecurityAlert], None]] = []
        self.detection_rules: Dict[str, Callable] = {}
        self.baseline_stats: Dict[str, Dict] = {}
        self._lock = threading.Lock()
 
        self._register_default_rules()
 
    def _register_default_rules(self):
        """Register default security detection rules."""
        self.detection_rules = {
            "high_frequency_requests": self._detect_high_frequency,
            "anomalous_input": self._detect_anomalous_input,
            "confidence_drift": self._detect_confidence_drift,
            "latency_anomaly": self._detect_latency_anomaly,
            "repeated_queries": self._detect_repeated_queries,
            "input_perturbation": self._detect_input_perturbation
        }
 
    def _recent(self) -> List[ModelMetrics]:
        with self._lock:
            return list(self.metrics_buffer)
 
    def record_inference(self, metrics: ModelMetrics) -> List[SecurityAlert]:
        """Record model inference and check for security issues."""
        with self._lock:
            self.metrics_buffer.append(metrics)
 
        alerts = []
        for rule_name, rule_func in self.detection_rules.items():
            try:
                alert = rule_func(metrics)
                if alert:
                    alerts.append(alert)
                    self._handle_alert(alert)
            except Exception as e:
                logger.error(f"Detection rule {rule_name} failed: {e}")
 
        return alerts
 
    def _detect_high_frequency(self, metrics: ModelMetrics) -> Optional[SecurityAlert]:
        """Detect unusually high request frequency from one client (possible extraction)."""
        if not metrics.client_id:
            return None
 
        window_seconds = self.config.get("frequency_window", 60)
        threshold = self.config.get("frequency_threshold", 100)
 
        cutoff = utc_now() - timedelta(seconds=window_seconds)
        count = sum(
            1 for m in self._recent()
            if m.timestamp > cutoff and m.client_id == metrics.client_id
            and m.model_id == metrics.model_id
        )
 
        if count > threshold:
            return new_alert(
                category=ThreatCategory.MODEL_EXTRACTION,
                severity=AlertSeverity.WARNING,
                description=f"High frequency requests detected: {count} in {window_seconds}s",
                details={
                    "request_count": count,
                    "window_seconds": window_seconds,
                    "threshold": threshold,
                    "client_id": metrics.client_id
                },
                model_id=metrics.model_id
            )
        return None
 
    def _detect_anomalous_input(self, metrics: ModelMetrics) -> Optional[SecurityAlert]:
        """Detect statistically anomalous inputs."""
        model_stats = self.baseline_stats.get(metrics.model_id, {})
        if not model_stats:
            return None
 
        mean_size = model_stats.get("mean_input_size", 0)
        std_size = model_stats.get("std_input_size", 1)
 
        if std_size > 0:
            z_score = abs(metrics.input_size - mean_size) / std_size
 
            if z_score > self.config.get("input_anomaly_threshold", 3):
                return new_alert(
                    category=ThreatCategory.INPUT_MANIPULATION,
                    severity=AlertSeverity.WARNING,
                    description=f"Anomalous input size detected (z-score: {z_score:.2f})",
                    details={
                        "input_size": metrics.input_size,
                        "mean_size": mean_size,
                        "std_size": std_size,
                        "z_score": z_score
                    },
                    model_id=metrics.model_id,
                    request_id=metrics.input_hash
                )
        return None
 
    def _detect_confidence_drift(self, metrics: ModelMetrics) -> Optional[SecurityAlert]:
        """Detect unusual confidence score patterns."""
        if not self.baseline_stats.get(metrics.model_id):
            return None
 
        if metrics.output_confidence > 0.99 or metrics.output_confidence < 0.01:
            recent = [m for m in self._recent() if m.model_id == metrics.model_id][-100:]
 
            extreme_count = sum(1 for m in recent
                                if m.output_confidence > 0.99 or m.output_confidence < 0.01)
 
            # Threshold is a starting point; tune it against your baseline
            if extreme_count > len(recent) * 0.3:
                return new_alert(
                    category=ThreatCategory.ADVERSARIAL_ATTACK,
                    severity=AlertSeverity.WARNING,
                    description="Unusual confidence distribution detected",
                    details={
                        "extreme_confidence_ratio": extreme_count / len(recent),
                        "current_confidence": metrics.output_confidence,
                        "sample_size": len(recent)
                    },
                    model_id=metrics.model_id
                )
        return None
 
    def _detect_latency_anomaly(self, metrics: ModelMetrics) -> Optional[SecurityAlert]:
        """Detect latency anomalies that might indicate attacks."""
        model_stats = self.baseline_stats.get(metrics.model_id, {})
        if not model_stats:
            return None
 
        mean_latency = model_stats.get("mean_latency", 0)
        std_latency = model_stats.get("std_latency", 1)
 
        if std_latency > 0:
            z_score = (metrics.latency_ms - mean_latency) / std_latency
 
            if z_score > self.config.get("latency_anomaly_threshold", 4):
                return new_alert(
                    category=ThreatCategory.ADVERSARIAL_ATTACK,
                    severity=AlertSeverity.INFO,
                    description=f"Latency anomaly detected (z-score: {z_score:.2f})",
                    details={
                        "latency_ms": metrics.latency_ms,
                        "mean_latency": mean_latency,
                        "z_score": z_score
                    },
                    model_id=metrics.model_id
                )
        return None
 
    def _detect_repeated_queries(self, metrics: ModelMetrics) -> Optional[SecurityAlert]:
        """Detect repeated identical queries (probing behavior)."""
        if not metrics.input_hash:
            return None
 
        window_minutes = self.config.get("repeat_window_minutes", 5)
        threshold = self.config.get("repeat_threshold", 10)
 
        cutoff = utc_now() - timedelta(minutes=window_minutes)
        repeat_count = sum(
            1 for m in self._recent()
            if m.timestamp > cutoff and m.input_hash == metrics.input_hash
        )
 
        if repeat_count > threshold:
            return new_alert(
                category=ThreatCategory.MODEL_EXTRACTION,
                severity=AlertSeverity.WARNING,
                description="Repeated identical queries detected",
                details={
                    "input_hash": metrics.input_hash,
                    "repeat_count": repeat_count,
                    "window_minutes": window_minutes
                },
                model_id=metrics.model_id
            )
        return None
 
    def _detect_input_perturbation(self, metrics: ModelMetrics) -> Optional[SecurityAlert]:
        """Detect systematic input perturbation patterns (weak, size-based signal)."""
        recent = [m for m in self._recent() if m.model_id == metrics.model_id][-50:]
        if len(recent) < 10:
            return None
 
        sizes = [m.input_size for m in recent]
        size_diffs = [abs(sizes[i] - sizes[i - 1]) for i in range(1, len(sizes))]
 
        avg_diff = float(np.mean(size_diffs))
        std_diff = float(np.std(size_diffs))
 
        # Very consistent small changes can indicate probing
        if avg_diff < 10 and std_diff < 2:
            return new_alert(
                category=ThreatCategory.ADVERSARIAL_ATTACK,
                severity=AlertSeverity.WARNING,
                description="Systematic input perturbation pattern detected",
                details={
                    "avg_size_diff": avg_diff,
                    "std_size_diff": std_diff,
                    "sample_count": len(recent)
                },
                model_id=metrics.model_id
            )
        return None
 
    def update_baseline(self, model_id: str, metrics_window: List[ModelMetrics]):
        """Update baseline statistics for a model."""
        if not metrics_window:
            return
 
        input_sizes = [m.input_size for m in metrics_window]
        latencies = [m.latency_ms for m in metrics_window]
        confidences = [m.output_confidence for m in metrics_window]
 
        self.baseline_stats[model_id] = {
            "mean_input_size": float(np.mean(input_sizes)),
            "std_input_size": float(np.std(input_sizes)),
            "mean_latency": float(np.mean(latencies)),
            "std_latency": float(np.std(latencies)),
            "mean_confidence": float(np.mean(confidences)),
            "std_confidence": float(np.std(confidences)),
            "updated_at": utc_now().isoformat()
        }
 
    def register_alert_handler(self, handler: Callable[[SecurityAlert], None]):
        """Register a handler for security alerts."""
        self.alert_handlers.append(handler)
 
    def _handle_alert(self, alert: SecurityAlert):
        """Process and distribute alert to handlers."""
        with self._lock:
            self.alerts.append(alert)
 
        for handler in self.alert_handlers:
            try:
                handler(alert)
            except Exception as e:
                logger.error(f"Alert handler failed: {e}")
 
    def get_alerts(self,
                   severity: Optional[AlertSeverity] = None,
                   category: Optional[ThreatCategory] = None,
                   since: Optional[datetime] = None) -> List[SecurityAlert]:
        """Query alerts with optional filters."""
        with self._lock:
            alerts = list(self.alerts)
 
        if severity:
            alerts = [a for a in alerts if a.severity == severity]
        if category:
            alerts = [a for a in alerts if a.category == category]
        if since:
            alerts = [a for a in alerts if a.timestamp > since]
 
        return alerts

LLM-Specific Monitoring

Prompt Injection Detection

import hashlib
import re
from typing import List, Tuple
 
 
class LLMSecurityMonitor:
    """
    Pattern-based telemetry for LLM prompts and responses.
 
    Treat matches as a weak signal for dashboards and alerts, not as a control:
    paraphrases, other languages and encodings get past regexes, and OWASP
    (LLM01:2025) notes it is unclear whether fool-proof prevention of prompt
    injection exists. Containment comes from architecture: least privilege for
    tools, untrusted content kept apart from instructions, and human approval
    for high-risk actions.
    """
 
    def __init__(self):
        self.prompt_patterns = self._load_injection_patterns()
        self.seen_prompts: dict = {}  # For repeat detection
        self.jailbreak_signatures = self._load_jailbreak_signatures()
 
    def _load_injection_patterns(self) -> List[Tuple[str, str, float]]:
        """Patterns matched against the lower-cased prompt."""
        return [
            # (pattern, description, severity_score)
            (r"ignore\s+(previous|all|above)\s+instructions?",
             "Instruction override attempt", 0.9),
            (r"disregard\s+(the\s+)?(system|previous)\s+(prompt|instructions?)",
             "System prompt override", 0.95),
            (r"you\s+are\s+now\s+(a|an|in)\s+",
             "Role reassignment attempt", 0.7),
            (r"pretend\s+(you('re|\s+are)|to\s+be)",
             "Role pretending attempt", 0.6),
            (r"act\s+as\s+(if|though|a|an)",
             "Role acting attempt", 0.5),
            (r"(new|different|alternate)\s+persona",
             "Persona switch attempt", 0.7),
            (r"output\s+(the\s+)?(system|initial|original)\s+prompt",
             "Prompt extraction attempt (LLM07)", 0.85),
            (r"reveal\s+(your|the)\s+(instructions?|prompt|programming)",
             "Instruction reveal attempt (LLM07)", 0.85),
            (r"what\s+(are|were)\s+your\s+(initial|system|original)\s+instructions?",
             "System prompt query (LLM07)", 0.8),
            (r"developer\s+mode|sudo\s+mode|admin\s+mode",
             "Privilege escalation attempt", 0.9),
            (r"<\|?(system|end|im_start|im_end)\|?>",
             "Token injection attempt", 0.95),
            (r"\n\s*\n\s*(human|assistant)\s*:",
             "Conversation injection", 0.9),
        ]
 
    def _load_jailbreak_signatures(self) -> List[dict]:
        """Known jailbreak signatures; each pattern carries its own regex flags."""
        return [
            {
                "name": "DAN",
                # Case-sensitive with word boundaries, so "Daniel" or "dance" do not match
                "patterns": [(r"\bDAN\b", 0), (r"do\s+anything\s+now", re.IGNORECASE)],
                "severity": 0.95
            },
            {
                "name": "Developer Mode",
                "patterns": [(r"developer\s+mode\s+(enabled|on|active)", re.IGNORECASE)],
                "severity": 0.9
            },
            {
                "name": "Evil Twin",
                "patterns": [(r"evil\s+(twin|version|mode)", re.IGNORECASE),
                             (r"opposite\s+day", re.IGNORECASE)],
                "severity": 0.85
            },
            {
                "name": "Token Smuggling",
                "patterns": [(r"<\|.*?\|>", 0), (r"\[INST\]", 0), (r"\[/INST\]", 0)],
                "severity": 0.95
            }
        ]
 
    def analyze_prompt(self, prompt: str, context: dict = None) -> dict:
        """Analyze prompt; 'flagged' means a pattern matched, not that the rest is safe."""
        results = {
            "flagged": False,
            "risk_score": 0.0,
            "detections": [],
            "recommendations": []
        }
 
        normalized = prompt.lower().strip()
 
        for pattern, description, severity in self.prompt_patterns:
            if re.search(pattern, normalized):
                results["detections"].append({
                    "type": "prompt_injection",
                    "pattern": pattern,
                    "description": description,
                    "severity": severity
                })
                results["risk_score"] = max(results["risk_score"], severity)
 
        for signature in self.jailbreak_signatures:
            for pattern, flags in signature["patterns"]:
                if re.search(pattern, prompt, flags):
                    results["detections"].append({
                        "type": "jailbreak_attempt",
                        "name": signature["name"],
                        "severity": signature["severity"]
                    })
                    results["risk_score"] = max(results["risk_score"], signature["severity"])
                    break
 
        encoding_issues = self._check_encoding_attacks(prompt)
        if encoding_issues:
            results["detections"].extend(encoding_issues)
            results["risk_score"] = max(
                results["risk_score"],
                max(i["severity"] for i in encoding_issues)
            )
 
        # Repeated identical prompts (probing)
        prompt_hash = hashlib.sha256(normalized.encode()).hexdigest()
        self.seen_prompts[prompt_hash] = self.seen_prompts.get(prompt_hash, 0) + 1
        if self.seen_prompts[prompt_hash] > 5:
            results["detections"].append({
                "type": "repeated_prompt",
                "count": self.seen_prompts[prompt_hash],
                "severity": 0.6
            })
 
        results["flagged"] = results["risk_score"] >= 0.7
 
        if results["detections"]:
            results["recommendations"] = self._generate_recommendations(results["detections"])
 
        return results
 
    def _check_encoding_attacks(self, prompt: str) -> List[dict]:
        """Check for encoding-based tricks."""
        issues = []
 
        if any(ord(c) > 127 for c in prompt):
            confusables = self._find_confusable_chars(prompt)
            if confusables:
                issues.append({
                    "type": "unicode_confusion",
                    "description": "Potentially confusable Unicode characters",
                    "characters": confusables,
                    "severity": 0.6
                })
 
        invisible_chars = [c for c in prompt if ord(c) in
                           [0x200B, 0x200C, 0x200D, 0xFEFF, 0x2060]]
        if invisible_chars:
            issues.append({
                "type": "invisible_characters",
                "description": "Invisible Unicode characters detected",
                "count": len(invisible_chars),
                "severity": 0.8
            })
 
        if re.search(r"[A-Za-z0-9+/]{40,}={0,2}", prompt):
            issues.append({
                "type": "encoded_payload",
                "description": "Potential base64 encoded content",
                "severity": 0.5
            })
 
        return issues
 
    def _find_confusable_chars(self, text: str) -> List[dict]:
        """Find a few common Cyrillic look-alikes of Latin letters."""
        confusable_map = {
            'а': 'a',  # Cyrillic а
            'е': 'e',  # Cyrillic е
            'о': 'o',  # Cyrillic о
            'р': 'p',  # Cyrillic р
            'с': 'c',  # Cyrillic с
            'у': 'y',  # Cyrillic у
            'х': 'x',  # Cyrillic х
        }
 
        return [
            {"position": i, "char": char, "looks_like": confusable_map[char]}
            for i, char in enumerate(text)
            if char in confusable_map
        ]
 
    def _generate_recommendations(self, detections: List[dict]) -> List[str]:
        """Map detections to architectural controls (OWASP LLM01:2025 mitigations)."""
        recommendations = []
        detection_types = set(d["type"] for d in detections)
 
        if detection_types & {"prompt_injection", "jailbreak_attempt"}:
            recommendations.append(
                "Enforce least privilege on the tools and data this model can reach"
            )
            recommendations.append(
                "Keep untrusted content separate from instructions and mark it as data"
            )
            recommendations.append(
                "Require human approval for high-risk actions"
            )
 
        if "invisible_characters" in detection_types:
            recommendations.append("Strip invisible Unicode characters from input")
 
        if "unicode_confusion" in detection_types:
            recommendations.append("Apply Unicode normalization before other checks")
 
        if "repeated_prompt" in detection_types:
            recommendations.append("Rate limit per client (OWASP LLM10 Unbounded Consumption)")
 
        return recommendations
 
    def analyze_response(self,
                         prompt: str,
                         response: str,
                         expected_behavior: dict = None) -> dict:
        """Analyze LLM response for leakage and signs of a followed injection."""
        results = {
            "flagged": False,
            "issues": [],
            "leaked_info": []
        }
 
        leakage_patterns = [
            (r"(api[_-]?key|secret|password|token)\s*[:=]\s*['\"]?[\w-]+",
             "Potential credential leakage"),
            (r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b",
             "Email address in response"),
            (r"\b\d{3}[-.]?\d{3}[-.]?\d{4}\b",
             "Phone number in response"),
            (r"\b\d{3}-?\d{2}-?\d{4}\b",
             "Potential SSN pattern"),
        ]
 
        for pattern, description in leakage_patterns:
            matches = re.findall(pattern, response, re.IGNORECASE)
            if matches:
                results["leaked_info"].append({
                    "type": description,
                    "matches": matches[:5]
                })
                results["flagged"] = True
 
        injection_success_indicators = [
            "I am now in",
            "DAN mode",
            "developer mode enabled",
            "I will ignore",
            "I no longer have",
            "my original instructions",
        ]
 
        for indicator in injection_success_indicators:
            if indicator.lower() in response.lower():
                results["issues"].append({
                    "type": "injection_success",
                    "indicator": indicator
                })
                results["flagged"] = True
 
        if expected_behavior:
            expected_length = expected_behavior.get("expected_max_length", 10000)
            if len(response) > expected_length * 1.5:
                results["issues"].append({
                    "type": "response_length_anomaly",
                    "length": len(response),
                    "expected_max": expected_length
                })
 
        return results

Metrics Dashboard Integration

from prometheus_client import Counter, Histogram, Gauge, CollectorRegistry
 
class AIMetricsExporter:
    def __init__(self, registry: CollectorRegistry = None):
        self.registry = registry or CollectorRegistry()
 
        # Inference metrics
        self.inference_total = Counter(
            'ai_inference_total',
            'Total number of model inferences',
            ['model_id', 'status'],
            registry=self.registry
        )
 
        self.inference_latency = Histogram(
            'ai_inference_latency_seconds',
            'Model inference latency',
            ['model_id'],
            buckets=[.01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10],
            registry=self.registry
        )
 
        self.confidence_score = Histogram(
            'ai_confidence_score',
            'Model output confidence scores',
            ['model_id'],
            buckets=[.1, .2, .3, .4, .5, .6, .7, .8, .9, .95, .99],
            registry=self.registry
        )
 
        # Security metrics
        self.security_alerts = Counter(
            'ai_security_alerts_total',
            'Total security alerts',
            ['model_id', 'category', 'severity'],
            registry=self.registry
        )
 
        self.blocked_requests = Counter(
            'ai_blocked_requests_total',
            'Requests blocked by security rules',
            ['model_id', 'reason'],
            registry=self.registry
        )
 
        self.prompt_injection_attempts = Counter(
            'ai_prompt_injection_attempts_total',
            'Prompt injection pattern matches',
            ['model_id', 'injection_type'],
            registry=self.registry
        )
 
        # Model health metrics
        self.model_drift_score = Gauge(
            'ai_model_drift_score',
            'Current model drift score',
            ['model_id'],
            registry=self.registry
        )
 
        self.anomaly_score = Gauge(
            'ai_anomaly_score',
            'Current anomaly detection score',
            ['model_id'],
            registry=self.registry
        )
 
    def record_inference(self, model_id: str, latency: float,
                         confidence: float, status: str = "success"):
        """Record inference metrics."""
        self.inference_total.labels(model_id=model_id, status=status).inc()
        self.inference_latency.labels(model_id=model_id).observe(latency)
        self.confidence_score.labels(model_id=model_id).observe(confidence)
 
    def record_security_alert(self, model_id: str,
                              category: str, severity: str):
        """Record security alert."""
        self.security_alerts.labels(
            model_id=model_id,
            category=category,
            severity=severity
        ).inc()
 
    def record_blocked_request(self, model_id: str, reason: str):
        """Record blocked request."""
        self.blocked_requests.labels(model_id=model_id, reason=reason).inc()
 
    def record_prompt_injection(self, model_id: str, injection_type: str):
        """Record a prompt injection pattern match."""
        self.prompt_injection_attempts.labels(
            model_id=model_id,
            injection_type=injection_type
        ).inc()
 
    def update_drift_score(self, model_id: str, score: float):
        """Update model drift score."""
        self.model_drift_score.labels(model_id=model_id).set(score)
 
    def update_anomaly_score(self, model_id: str, score: float):
        """Update anomaly score."""
        self.anomaly_score.labels(model_id=model_id).set(score)
 
 
# Panel plan for a Grafana (or any PromQL) dashboard. This is not Grafana
# dashboard JSON: create the panels in Grafana with these queries.
DASHBOARD_PANELS = [
    {
        "title": "Inference Rate",
        "visualization": "time series",
        "promql": "sum by (model_id) (rate(ai_inference_total[5m]))"
    },
    {
        "title": "Security Alerts by Category (24h)",
        "visualization": "pie chart",
        "promql": "sum by (category) (increase(ai_security_alerts_total[24h]))"
    },
    {
        "title": "Prompt Injection Pattern Matches (1h)",
        "visualization": "stat",
        "promql": "sum(increase(ai_prompt_injection_attempts_total[1h]))"
    },
    {
        "title": "Model Drift Score",
        "visualization": "gauge",
        "promql": "ai_model_drift_score",
        "thresholds": [0.3, 0.7]
    },
    {
        "title": "P99 Latency",
        "visualization": "time series",
        "promql": "histogram_quantile(0.99, sum by (le, model_id) (rate(ai_inference_latency_seconds_bucket[5m])))"
    },
    {
        "title": "Blocked Requests (24h)",
        "visualization": "table",
        "promql": "topk(10, sum by (reason) (increase(ai_blocked_requests_total[24h])))"
    }
]

Alerting Configuration

# prometheus-alert-rules.yaml
# Prometheus alerting rules (load via rule_files in prometheus.yml).
# Prometheus evaluates them; Alertmanager only routes the notifications.
groups:
  - name: ai-security-alerts
    rules:
      - alert: HighPromptInjectionRate
        expr: sum by (model_id) (rate(ai_prompt_injection_attempts_total[5m])) > 10
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: "High rate of prompt injection pattern matches"
          description: "Model {{ $labels.model_id }}: more than 10 matches per second over 5m"
 
      - alert: ModelDriftDetected
        expr: ai_model_drift_score > 0.7
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "Model drift detected"
          description: "Model {{ $labels.model_id }} drift score is {{ $value }}"
 
      - alert: AnomalousInferencePattern
        expr: ai_anomaly_score > 0.8
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Anomalous inference pattern detected"
          description: "Model {{ $labels.model_id }} showing anomalous behavior"
 
      - alert: HighBlockedRequestRate
        expr: sum by (model_id) (rate(ai_blocked_requests_total[5m])) > 50
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "High rate of blocked requests"
          description: "Model {{ $labels.model_id }}: possible attack in progress"
 
      - alert: InferenceLatencyHigh
        expr: histogram_quantile(0.99, sum by (le, model_id) (rate(ai_inference_latency_seconds_bucket[5m]))) > 5
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High inference latency"
          description: "P99 latency is {{ $value }}s for model {{ $labels.model_id }}"

Best Practices

Monitoring Strategy

  1. Baseline establishment: Collect normal behavior metrics before enabling alerting
  2. Multi-layer detection: Combine statistical, rule-based, and ML-based detection
  3. Context preservation: Log full request context for forensic analysis
  4. Tunable thresholds: Allow adjustment based on model and traffic patterns

Alert Management

  • Use severity levels appropriately
  • Implement alert aggregation to prevent fatigue
  • Create runbooks for each alert type
  • Regular review and tuning of alert thresholds

From production

In JARVIS, the multi-agent platform DeviDevs runs on, the security rules are code: hooks that, in the main sessions, run before every shell command, file write, web request and agent start (file reads are not covered). The injection detector has its own tests: a regex narrowing that looked clean stopped catching a "SYSTEM PROMPT: You are..." payload, a measurement caught it, and a test now pins it; an exception on a substring would have switched the detector off exactly when a command also carried a real vector. Both changes were rejected. Between 7 September and 15 November 2026 all hook rules, security and workflow together, produced about 2,200 blocks in working sessions, and as of 15 November 2026 the tests for the security rules pass. One lesson applies to any monitoring stack: until September 2026 nobody ran the hook tests (no CI, no pre-commit), and tests that do not run protect nothing. They now have a runner.

Official references

Related on this site: Every AI Security Startup Got Acquired. Now What?, Your AI Agent Framework Is Probably Compromised Right Now, AI API Security: Auth, Authorization & Rate Limiting. More articles in the AI Security category, and the engineering work behind them is on the services page.

Need help building AI monitoring infrastructure? DeviDevs implements production MLOps platforms with comprehensive security monitoring. See how a DeviDevs Sprint works →

Want AI agents and automations your business can actually run on?

We ship production AI in fixed two-week sprints, then secure them. Book a 30-minute call to scope yours.

Book a Call

Before you book, you can see what we build, how a sprint works, how our pricing works, what we have already built, or write to us directly.

Weekly AI Agents, Automation & Security Digest

How we build AI agents and automations with Claude Code, plus practical AI security, integrations, and platform notes, delivered weekly.

No spam. Unsubscribe anytime.