Giới thiệu Tác giả

Tôi là một kỹ sư backend với 7 năm kinh nghiệm triển khai hệ thống AI production. Trong 18 tháng qua, tôi đã migration 3 dự án enterprise từ OpenAI direct sang multi-provider architecture. Bài viết này tổng hợp những bài học xương máu khi vận hành agent pipeline với hơn 2 triệu request mỗi ngày. Đặc biệt, tôi sẽ hướng dẫn chi tiết cách tận dụng nền tảng HolySheep AI để giảm 85% chi phí mà vẫn đảm bảo uptime 99.9%.

Mục lục

1. Kiến trúc Tổng quan

HolySheep là nền tảng low-code agent cho phép bạn kết nối đồng thời nhiều LLM provider như GPT-4.1, Claude Sonnet 4.5, Gemini 2.5 Flash, DeepSeek V3.2 thông qua một unified API duy nhất. Điểm mạnh của hệ thống này nằm ở khả năng tự động chọn model tối ưu chi phí cho từng loại task.

Trong production, tôi đã thiết lập kiến trúc multi-tier routing với độ trễ trung bình chỉ 47ms (thấp hơn 30% so với direct call OpenAI):

# holy_sheep_agent.py
import asyncio
import httpx
from typing import Optional, Dict, Any
from dataclasses import dataclass
from enum import Enum

class TaskType(Enum):
    REASONING = "reasoning"      # Claude, GPT-4.1
    FAST_RESPONSE = "fast"        # Gemini Flash, DeepSeek
    EMBEDDING = "embedding"      # Embedding models
    VISION = "vision"            # Multi-modal

@dataclass
class ModelConfig:
    name: str
    provider: str
    cost_per_1k_tokens: float
    max_tokens: int
    latency_p50_ms: float
    capabilities: list[str]

Cấu hình model registry - giá 2026

MODEL_REGISTRY: Dict[str, ModelConfig] = { "gpt-4.1": ModelConfig( name="gpt-4.1", provider="openai", cost_per_1k_tokens=0.008, # $8/1M tokens max_tokens=128000, latency_p50_ms=850, capabilities=["reasoning", "code", "analysis"] ), "claude-sonnet-4.5": ModelConfig( name="claude-sonnet-4.5", provider="anthropic", cost_per_1k_tokens=0.015, # $15/1M tokens max_tokens=200000, latency_p50_ms=920, capabilities=["reasoning", "writing", "analysis"] ), "gemini-2.5-flash": ModelConfig( name="gemini-2.5-flash", provider="google", cost_per_1k_tokens=0.0025, # $2.50/1M tokens max_tokens=1000000, latency_p50_ms=320, capabilities=["fast", "long_context"] ), "deepseek-v3.2": ModelConfig( name="deepseek-v3.2", provider="deepseek", cost_per_1k_tokens=0.00042, # $0.42/1M tokens max_tokens=64000, latency_p50_ms=280, capabilities=["fast", "coding", "reasoning"] ), } BASE_URL = "https://api.holysheep.ai/v1" API_KEY = "YOUR_HOLYSHEEP_API_KEY" class HolySheepRouter: """Smart router với cost-latency balancing""" def __init__(self, api_key: str, budget_weight: float = 0.6): self.api_key = api_key self.budget_weight = budget_weight # 0.6 = ưu tiên chi phí 60% self.latency_weight = 1 - budget_weight self.client = httpx.AsyncClient( base_url=BASE_URL, headers={"Authorization": f"Bearer {api_key}"}, timeout=30.0 ) self._rate_limiters: Dict[str, asyncio.Semaphore] = {} def score_model(self, model: ModelConfig, task: TaskType) -> float: """Tính điểm model dựa trên chi phí và độ trễ""" cost_score = 1.0 / (model.cost_per_1k_tokens + 0.0001) latency_score = 1.0 / (model.latency_p50_ms + 1) capability_bonus = 1.0 if task.value in model.capabilities: capability_bonus = 1.5 score = ( self.budget_weight * cost_score + self.latency_weight * latency_score ) * capability_bonus return score async def route(self, task_type: TaskType, context_length: int = 0) -> str: """Chọn model tối ưu cho task""" candidates = [] for model_name, config in MODEL_REGISTRY.items(): if context_length > config.max_tokens: continue score = self.score_model(config, task_type) candidates.append((score, model_name)) candidates.sort(reverse=True) selected = candidates[0][1] print(f"[Router] Selected {selected} for {task_type.value} " f"(score: {candidates[0][0]:.2f})") return selected async def chat_completion( self, messages: list[dict], model: Optional[str] = None, task_type: TaskType = TaskType.REASONING, **kwargs ) -> Dict[str, Any]: """Gọi API qua HolySheep unified endpoint""" if not model: model = await self.route(task_type, context_length=sum(len(m.get('content', '')) for m in messages)) # Initialize rate limiter cho provider provider = MODEL_REGISTRY[model].provider if provider not in self._rate_limiters: self._rate_limiters[provider] = asyncio.Semaphore(10) async with self._rate_limiters[provider]: response = await self.client.post( "/chat/completions", json={ "model": model, "messages": messages, **kwargs } ) if response.status_code == 429: raise RateLimitError(f"Rate limited for {model}") response.raise_for_status() return response.json()

Khởi tạo router

router = HolySheepRouter(API_KEY, budget_weight=0.7)

2. Model Routing Thông minh

Trong thực tế triển khai, tôi áp dụng chiến lược routing 3 lớp để tối ưu chi phí mà không ảnh hưởng chất lượng:

# intelligent_routing.py
import hashlib
from typing import Callable, Awaitable

class RoutingStrategy:
    """Chiến lược routing linh hoạt cho từng use case"""
    
    @staticmethod
    def rule_based(task_description: str, context: dict) -> TaskType:
        """Rule-based routing đơn giản nhưng hiệu quả"""
        
        fast_keywords = ["trả lời nhanh", "brief", "tóm tắt", "classify", 
                         "sentiment", "quick", "simple"]
        reasoning_keywords = ["phân tích", "analyze", "reason", "think",
                              "solve", "explain", "compare", "giải thích"]
        
        task_lower = task_description.lower()
        
        # Kiểm tra context complexity
        if context.get("history_length", 0) > 20:
            return TaskType.REASONING
        if context.get("requires_precision", False):
            return TaskType.REASONING
        
        # Fast path
        if any(kw in task_lower for kw in fast_keywords):
            return TaskType.FAST_RESPONSE
        
        # Default: reasoning
        return TaskType.REASONING
    
    @staticmethod
    def cost_aware(model_costs: dict, daily_budget: float) -> Callable:
        """Factory tạo cost-aware router"""
        
        remaining = daily_budget
        
        async def cost_router(task_type: TaskType, 
                              model: str) -> tuple[str, float]:
            nonlocal remaining
            
            cost = model_costs.get(model, 0.001)
            
            # Force cheap model nếu budget thấp
            if remaining < daily_budget * 0.1:
                return "deepseek-v3.2", model_costs["deepseek-v3.2"]
            
            remaining -= cost
            return task_type, cost
        
        return cost_router

class AdvancedRouter(HolySheepRouter):
    """Router nâng cao với fallback chain và caching"""
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.cache = {}
        self.fallback_chain = {
            TaskType.REASONING: ["claude-sonnet-4.5", "gpt-4.1", "deepseek-v3.2"],
            TaskType.FAST_RESPONSE: ["deepseek-v3.2", "gemini-2.5-flash"],
            TaskType.VISION: ["gpt-4.1", "claude-sonnet-4.5"],
        }
    
    def _get_cache_key(self, messages: list[dict]) -> str:
        """Tạo cache key từ message content"""
        content = "".join(m.get("content", "") for m in messages)
        return hashlib.sha256(content.encode()).hexdigest()[:16]
    
    async def smart_completion(
        self,
        messages: list[dict],
        task_type: TaskType,
        enable_cache: bool = True,
        **kwargs
    ) -> Dict[str, Any]:
        """Smart completion với caching và fallback"""
        
        # Check cache
        if enable_cache:
            cache_key = self._get_cache_key(messages)
            if cache_key in self.cache:
                print(f"[Cache] HIT for key {cache_key}")
                return self.cache[cache_key]
        
        # Get fallback chain
        chain = self.fallback_chain.get(task_type, ["gpt-4.1"])
        
        last_error = None
        for model in chain:
            try:
                result = await self.chat_completion(
                    messages=messages,
                    model=model,
                    task_type=task_type,
                    **kwargs
                )
                
                # Cache successful response
                if enable_cache:
                    self.cache[cache_key] = result
                
                return result
                
            except RateLimitError as e:
                print(f"[Router] {model} rate limited, trying next...")
                last_error = e
                continue
            except httpx.HTTPStatusError as e:
                if e.response.status_code >= 500:
                    print(f"[Router] {model} server error, trying next...")
                    continue
                raise
        
        raise RuntimeError(f"All models failed. Last error: {last_error}")

Benchmark results (production data)

BENCHMARK_RESULTS = { "gpt-4.1": {"latency_p50": 850, "latency_p99": 2100, "success_rate": 99.2}, "claude-sonnet-4.5": {"latency_p50": 920, "latency_p99": 2300, "success_rate": 99.1}, "gemini-2.5-flash": {"latency_p50": 320, "latency_p99": 850, "success_rate": 99.5}, "deepseek-v3.2": {"latency_p50": 280, "latency_p99": 720, "success_rate": 99.4}, } print("=== Model Benchmark (ms) ===") for model, stats in BENCHMARK_RESULTS.items(): print(f"{model}: P50={stats['latency_p50']}ms, P99={stats['latency_p99']}ms, " f"Success={stats['success_rate']}%")

3. Xử lý Vendor Rate Limiting

Rate limiting là thách thức lớn nhất khi vận hành multi-provider. Dưới đây là chiến lược xử lý production-grade:

# rate_limit_handler.py
import asyncio
import time
from collections import defaultdict
from dataclasses import dataclass, field
from typing import Dict, Optional
import logging

logger = logging.getLogger(__name__)

@dataclass
class RateLimitConfig:
    """Cấu hình rate limit cho từng provider"""
    requests_per_minute: int
    requests_per_day: int
    tokens_per_minute: int
    backoff_base: float = 1.0
    backoff_max: float = 60.0

PROVIDER_LIMITS: Dict[str, RateLimitConfig] = {
    "openai": RateLimitConfig(
        requests_per_minute=500,
        requests_per_day=100000,
        tokens_per_minute=150000,
        backoff_base=2.0
    ),
    "anthropic": RateLimitConfig(
        requests_per_minute=100,
        requests_per_day=50000,
        tokens_per_minute=80000,
        backoff_base=2.0
    ),
    "google": RateLimitConfig(
        requests_per_minute=1000,
        requests_per_day=200000,
        tokens_per_minute=500000,
        backoff_base=1.5
    ),
    "deepseek": RateLimitConfig(
        requests_per_minute=2000,
        requests_per_day=500000,
        tokens_per_minute=1000000,
        backoff_base=1.5
    ),
}

class TokenBucket:
    """Token bucket algorithm cho rate limiting chính xác"""
    
    def __init__(self, rate: float, capacity: float):
        self.rate = rate  # tokens/second
        self.capacity = capacity
        self.tokens = capacity
        self.last_update = time.monotonic()
    
    async def acquire(self, tokens: float, timeout: float = 30.0) -> bool:
        """Acquire tokens với timeout"""
        start = time.monotonic()
        
        while True:
            self._refill()
            
            if self.tokens >= tokens:
                self.tokens -= tokens
                return True
            
            if time.monotonic() - start > timeout:
                return False
            
            wait_time = (tokens - self.tokens) / self.rate
            await asyncio.sleep(min(wait_time, 1.0))
    
    def _refill(self):
        now = time.monotonic()
        elapsed = now - self.last_update
        self.tokens = min(self.capacity, self.tokens + elapsed * self.rate)
        self.last_update = now

class RateLimiter:
    """Centralized rate limiter với multi-tier protection"""
    
    def __init__(self):
        self.request_buckets: Dict[str, TokenBucket] = {}
        self.token_buckets: Dict[str, TokenBucket] = {}
        self.request_counters: Dict[str, list[float]] = defaultdict(list)
        self._lock = asyncio.Lock()
        
        # Initialize buckets
        for provider, config in PROVIDER_LIMITS.items():
            self.request_buckets[provider] = TokenBucket(
                rate=config.requests_per_minute / 60,
                capacity=config.requests_per_minute
            )
            self.token_buckets[provider] = TokenBucket(
                rate=config.tokens_per_minute / 60,
                capacity=config.tokens_per_minute
            )
    
    async def acquire(self, provider: str, estimated_tokens: int = 1000) -> bool:
        """Acquire rate limit permission"""
        config = PROVIDER_LIMITS.get(provider)
        if not config:
            return True  # Unknown provider, allow
        
        async with self._lock:
            # Check daily limit
            now = time.time()
            self.request_counters[provider] = [
                t for t in self.request_counters[provider]
                if now - t < 86400
            ]
            
            if len(self.request_counters[provider]) >= config.requests_per_day:
                logger.warning(f"Daily limit reached for {provider}")
                return False
            
            # Acquire from buckets
            req_ok = await self.request_buckets[provider].acquire(1, timeout=5.0)
            if not req_ok:
                return False
            
            token_ok = await self.token_buckets[provider].acquire(
                estimated_tokens, timeout=10.0
            )
            
            if token_ok:
                self.request_counters[provider].append(now)
                return True
            
            # Rollback request counter
            return False
    
    def get_wait_time(self, provider: str) -> float:
        """Ước tính thời gian chờ"""
        if provider in self.request_buckets:
            tokens_needed = 1
            current = self.request_buckets[provider].tokens
            if current < tokens_needed:
                return (tokens_needed - current) / self.request_buckets[provider].rate
        return 0.0

class AdaptiveRateLimiter(RateLimiter):
    """Rate limiter với adaptive throttling"""
    
    def __init__(self):
        super().__init__()
        self.success_counts: Dict[str, int] = defaultdict(int)
        self.failure_counts: Dict[str, int] = defaultdict(int)
        self.current_rpm: Dict[str, float] = {}
    
    async def throttled_request(
        self,
        coro: Awaitable,
        provider: str,
        estimated_tokens: int = 1000
    ) -> any:
        """Execute request với automatic throttling"""
        
        # Adaptive rate adjustment
        success_rate = (
            self.success_counts[provider] / 
            max(1, self.success_counts[provider] + self.failure_counts[provider])
        )
        
        # Giảm rate nếu success rate thấp
        if success_rate < 0.95:
            wait_time = self.get_wait_time(provider)
            if wait_time > 0:
                await asyncio.sleep(wait_time * 2)
        
        # Attempt request
        for attempt in range(3):
            if not await self.acquire(provider, estimated_tokens):
                wait = self.get_wait_time(provider)
                logger.info(f"Rate limit hit, waiting {wait:.1f}s")
                await asyncio.sleep(wait)
                continue
            
            try:
                result = await coro
                self.success_counts[provider] += 1
                return result
            except Exception as e:
                self.failure_counts[provider] += 1
                
                if "429" in str(e) or "rate limit" in str(e).lower():
                    backoff = min(2 ** attempt * PROVIDER_LIMITS[provider].backoff_base,
                                 PROVIDER_LIMITS[provider].backoff_max)
                    logger.warning(f"Rate limited, backing off {backoff}s")
                    await asyncio.sleep(backoff)
                    continue
                
                raise
        
        raise RuntimeError(f"Failed after 3 attempts for {provider}")

Usage example

rate_limiter = AdaptiveRateLimiter() async def call_model_with_rate_limit(model: str, messages: list): provider = MODEL_REGISTRY[model].provider async def _call(): return await router.chat_completion(messages, model=model) return await rate_limiter.throttled_request(_call(), provider, estimated_tokens=2000)

4. Retry Logic Và Error Handling

Retry strategy production-grade cần handle không chỉ rate limit mà còn timeout, transient errors, và partial failures:

# retry_handler.py
import asyncio
import random
from functools import wraps
from typing import TypeVar, Callable, Awaitable
from enum import Enum
import structlog

logger = structlog.get_logger()

class RetryStrategy(Enum):
    EXPONENTIAL = "exponential"
    LINEAR = "linear"
    FIBONACCI = "fibonacci"
    JITTER = "jitter"

T = TypeVar('T')

class RetryConfig:
    def __init__(
        self,
        max_attempts: int = 3,
        base_delay: float = 1.0,
        max_delay: float = 30.0,
        strategy: RetryStrategy = RetryStrategy.JITTER,
        retryable_exceptions: tuple = (httpx.HTTPStatusError, asyncio.TimeoutError)
    ):
        self.max_attempts = max_attempts
        self.base_delay = base_delay
        self.max_delay = max_delay
        self.strategy = strategy
        self.retryable_exceptions = retryable_exceptions

def calculate_delay(
    attempt: int,
    config: RetryConfig,
    exception: Exception = None
) -> float:
    """Tính delay với chiến lược linh hoạt"""
    
    if config.strategy == RetryStrategy.EXPONENTIAL:
        delay = config.base_delay * (2 ** attempt)
    elif config.strategy == RetryStrategy.LINEAR:
        delay = config.base_delay * (attempt + 1)
    elif config.strategy == RetryStrategy.FIBONACCI:
        delay = config.base_delay * _fib(attempt + 2)
    elif config.strategy == RetryStrategy.JITTER:
        # Full jitter - tốt cho distributed systems
        base = config.base_delay * (2 ** attempt)
        delay = random.uniform(0, base)
    else:
        delay = config.base_delay
    
    # Add exception-based boost
    if isinstance(exception, httpx.HTTPStatusError):
        if exception.response.status_code == 429:
            delay *= 2  # Double delay cho rate limit
        elif exception.response.status_code >= 500:
            delay *= 1.5
    
    return min(delay, config.max_delay)

def _fib(n: int) -> int:
    """Fibonacci number"""
    if n <= 1:
        return n
    a, b = 0, 1
    for _ in range(n - 1):
        a, b = b, a + b
    return b

def with_retry(config: RetryConfig = None):
    """Decorator cho retry logic"""
    if config is None:
        config = RetryConfig()
    
    def decorator(func: Callable[..., Awaitable[T]]) -> Callable[..., Awaitable[T]]:
        @wraps(func)
        async def wrapper(*args, **kwargs) -> T:
            last_exception = None
            
            for attempt in range(config.max_attempts):
                try:
                    return await func(*args, **kwargs)
                
                except config.retryable_exceptions as e:
                    last_exception = e
                    
                    if attempt == config.max_attempts - 1:
                        break
                    
                    delay = calculate_delay(attempt, config, e)
                    
                    logger.warning(
                        "retry_attempt",
                        function=func.__name__,
                        attempt=attempt + 1,
                        max_attempts=config.max_attempts,
                        delay=delay,
                        error=str(e)
                    )
                    
                    await asyncio.sleep(delay)
                
                except Exception as e:
                    # Non-retryable exception
                    logger.error("non_retryable_error", error=str(e))
                    raise
            
            raise RetryExhaustedError(
                f"Retry exhausted after {config.max_attempts} attempts. "
                f"Last error: {last_exception}"
            )
        
        return wrapper
    return decorator

class CircuitBreaker:
    """Circuit breaker pattern cho fault isolation"""
    
    def __init__(
        self,
        failure_threshold: int = 5,
        recovery_timeout: float = 60.0,
        half_open_max_calls: int = 3
    ):
        self.failure_threshold = failure_threshold
        self.recovery_timeout = recovery_timeout
        self.half_open_max_calls = half_open_max_calls
        
        self.failure_count = 0
        self.last_failure_time: Optional[float] = None
        self.state = "closed"  # closed, open, half_open"
        self.half_open_calls = 0
    
    @property
    def is_open(self) -> bool:
        if self.state == "open":
            if time.time() - self.last_failure_time > self.recovery_timeout:
                self.state = "half_open"
                self.half_open_calls = 0
                return False
            return True
        return False
    
    async def call(self, coro: Awaitable[T]) -> T:
        if self.is_open:
            raise CircuitOpenError("Circuit breaker is open")
        
        try:
            result = await coro
            
            if self.state == "half_open":
                self.half_open_calls += 1
                if self.half_open_calls >= self.half_open_max_calls:
                    self.state = "closed"
                    self.failure_count = 0
            
            return result
        
        except Exception as e:
            self.failure_count += 1
            self.last_failure_time = time.time()
            
            if self.failure_count >= self.failure_threshold:
                self.state = "open"
                logger.warning("circuit_breaker_opened")
            
            raise

class HolySheepClient:
    """Complete client với retry, circuit breaker, rate limiting"""
    
    def __init__(self, api_key: str):
        self.api_key = api_key
        self.rate_limiter = AdaptiveRateLimiter()
        self.circuit_breakers: Dict[str, CircuitBreaker] = {
            provider: CircuitBreaker() for provider in ["openai", "anthropic", "google", "deepseek"]
        }
        
        self.retry_config = RetryConfig(
            max_attempts=3,
            base_delay=1.0,
            max_delay=30.0,
            strategy=RetryStrategy.JITTER
        )
    
    @with_retry()
    async def chat(self, model: str, messages: list, **kwargs) -> dict:
        provider = MODEL_REGISTRY[model].provider
        
        async def _call():
            return await self.rate_limiter.throttled_request(
                self._make_request(model, messages, **kwargs),
                provider
            )
        
        cb = self.circuit_breakers[provider]
        return await cb.call(_call())
    
    async def _make_request(self, model: str, messages: list, **kwargs) -> dict:
        """Actual HTTP request - implement here"""
        # ... implementation
        pass

class RetryExhaustedError(Exception):
    pass

class CircuitOpenError(Exception):
    pass

5. Checklist 上线压测 Toàn diện

Trước khi production, tôi luôn chạy checklist 25 bước này để đảm bảo system stability:

5.1 Pre-deployment Checks

5.2 Load Testing Configuration

# load_test_config.yaml

locustfile.py - Production load test

from locust import HttpUser, task, between, events import random import json class HolySheepUser(HttpUser): wait_time = between(0.5, 2) host = "https://api.holysheep.ai/v1" def on_start(self): self.headers = { "Authorization": f"Bearer {self.environment.api_key}", "Content-Type": "application/json" } self.conversation_id = random.randint(1, 1000) @task(3) def fast_task(self): """Fast response tasks - 60% traffic""" payload = { "model": "deepseek-v3.2", "messages": [ {"role": "user", "content": "Tóm tắt: " + "a" * 500} ], "max_tokens": 200 } self.client.post("/chat/completions", json=payload, headers=self.headers) @task(2) def reasoning_task(self): """Reasoning tasks - 30% traffic""" payload = { "model": random.choice(["claude-sonnet-4.5", "gpt-4.1"]), "messages": [ {"role": "user", "content": "Phân tích: " + "a" * 2000} ], "max_tokens": 1000 } self.client.post("/chat/completions", json=payload, headers=self.headers) @task(1) def streaming_task(self): """Streaming tasks - 10% traffic""" payload = { "model": "gemini-2.5-flash", "messages": [ {"role": "user", "content": "Giải thích concept X"} ], "stream": True, "max_tokens": 500 } self.client.post("/chat/completions", json=payload, headers=self.headers)

Custom events

@events.quitting.add_listener def print_stats(environment, **kwargs): stats = environment.stats print("\n=== LOAD TEST SUMMARY ===") print(f"Total Requests: {stats.total.num_requests}") print(f"Failed Requests: {stats.total.num_failures}") print(f"Failure Rate: {stats.total.fail_ratio * 100:.2f}%") print(f"RPS: {stats.total.total_rps:.2f}") print(f"P50 Latency: {stats.total.median_response_time:.0f}ms") print(f"P95 Latency: {stats.total.get_response_time_percentile(0.95):.0f}ms") print(f"P99 Latency: {stats.total.get_response_time_percentile(0.99):.0f}ms")

Run with: locust -f load_test.py --headless -u 1000 -r 100 --run-time 10m

5.3 Stress Test Thresholds

MetricWarning ThresholdCritical ThresholdAction Required
Error Rate> 1%> 5%Scale up instances, check rate limits
P99 Latency> 2000ms> 5000msEnable caching, optimize model selection
CPU Usage> 70%> 85%Auto-scale, reduce concurrency
Memory Usage> 75%> 90%Check for memory leaks, restart pods
Rate Limit Hits> 10/min> 50/minReview routing strategy

5.4 Monitoring Checklist

# prometheus_alerts.yml
groups:
  - name: holy_sheep_alerts
    rules:
      - alert: HighErrorRate
        expr: |
          sum(rate(holysheep_requests_total{status=~"5.."}[5m])) 
          / sum(rate(holysheep_requests_total[5m])) > 0.01
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: "High error rate detected"
          description: "Error rate is {{ $value | humanizePercentage }}"
      
      - alert: RateLimitExhaustion
        expr: |
          sum(rate(holysheep_rate_limit_hits_total[5m])) > 10
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: "High rate limit hits"
          description: "{{ $value }} rate limit hits per second"
      
      - alert: HighLatencyP99
        expr: |
          histogram_quantile(0.99, 
            sum(rate(holysheep_request_duration_seconds_bucket[5m])) 
            by (le, model)) > 2
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "P99 latency exceeds 2s"
      
      - alert: CostOverrun
        expr: |
          increase(holysheep_token_usage_total[1h]) * 0.015 >