AI Agent SLA Design: Key Metrics for Service Quality

๐Ÿ“˜ Tutorials 2026-07-19 2 min read

AI Agent SLA Design: Key Metrics for Service Quality

💡 What You Will Learn

AI Agent SLA Design: Key Metrics for Service Quality

P50 P95 P99
>99.5% >98% >95%
>90% >85% >80%
100% 99.9% 99.5%
import time
import json
from datetime import datetime
from collections import deque

class SLAMonitor:
    def __init__(self, window_minutes=60):
        self.window = window_minutes * 60
        self.latencies = deque()
        self.success_count = 0
        self.total_count = 0
        self.errors = {}

    def record(self, latency_ms: float, success: bool, error_type: str = None):
        now = time.time()
        self.latencies.append((now, latency_ms))
        self.total_count += 1
        if success:
            self.success_count += 1
        else:
            self.errors[error_type] = self.errors.get(error_type, 0) + 1
        while self.latencies and now - self.latencies[0][0] > self.window:
            self.latencies.popleft()

    def report(self) -> dict:
        latencies = [l[1] for _, l in self.latencies]
        latencies.sort()
        n = len(latencies)
        success_rate = self.success_count / max(self.total_count, 1)
        p50 = latencies[int(n * 0.5)] if n else 0
        p95 = latencies[int(n * 0.95)] if n else 0
        p99 = latencies[int(n * 0.99)] if n else 0
        return {
            "timestamp": datetime.now().isoformat(),
            "total_requests": self.total_count,
            "success_rate": round(success_rate * 100, 2),
            "latency_ms": {"p50": p50, "p95": p95, "p99": p99},
            "error_breakdown": dict(sorted(self.errors.items(), key=lambda x: -x[1])[:5])
        }

monitor = SLAMonitor(window_minutes=60)

def agent_call(user_input: str) -> str:
    start = time.time()
    try:
        result = agent.invoke(user_input)
        elapsed = (time.time() - start) * 1000
        monitor.record(elapsed, success=True)
        return result
    except Exception as e:
        elapsed = (time.time() - start) * 1000
        monitor.record(elapsed, success=False, error_type=type(e).__name__)
        raise

print(json.dumps(monitor.report(), ensure_ascii=False, indent=2))
Related Articles
2026-07-20
Fine Tuning Best Practices 2026: Optimize Open Source LLMs for Your Specific Task
2026-07-26
AI Pair Programming vs Vibe Coding: Key Differences
2026-07-19
AI Agent Fault Tolerance: Keep Running When Things Fail

Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only โ€” no paid placements.

๐Ÿ’ฌ Comments (0)

No comments yet. Be the first!

Login to comment