Tôi đã triển khai hệ thống AI API Gateway cho hơn 15 dự án production trong 3 năm qua, và điều tôi học được là: bộ lọc bảo mật (security filter) không chỉ là lớp bảo vệ — mà là xương sống của kiến trúc chi phí hiệu quả. Một cấu hình sai có thể khiến chi phí API tăng 300% hoặc khiến hệ thống bị tấn công trong 48 giờ.

Trong bài viết này, tôi sẽ chia sẻ kiến thức thực chiến về cách xây dựng security filter với HolySheep AI — nơi tỷ giá chỉ ¥1=$1, giúp tiết kiệm 85%+ so với các nhà cung cấp khác.

Tại Sao Cần Security Filter Cho AI API?

Khi làm việc với các mô hình AI như GPT-4.1 ($8/MTok), Claude Sonnet 4.5 ($15/MTok) hay DeepSeek V3.2 ($0.42/MTok), mỗi request đều có chi phí tính toán. Security filter giúp:

Kiến Trúc Security Filter Layer

Đây là kiến trúc tôi đã áp dụng thành công cho hệ thống xử lý 10,000+ requests/giây:

┌─────────────────────────────────────────────────────────────┐
│                    Request Incoming                          │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│  Layer 1: Rate Limiter (Token Bucket)                        │
│  - 100 req/min (free tier)                                   │
│  - 1000 req/min (pro tier)                                   │
│  - Connection tracking per API key                          │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│  Layer 2: Input Validation & Sanitization                   │
│  - Prompt length check (< 32,768 tokens)                    │
│  - Character filtering (non-printable removal)             │
│  - SQL/NoSQL injection pattern matching                     │
│  - Prompt injection detection                               │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│  Layer 3: Content Safety Filter (Pre-check)                 │
│  - Keyword blocking list                                    │
│  - Regex-based pattern matching                             │
│  - ML-based toxicity detection (optional)                   │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│  Layer 4: Token Budget Control                              │
│  - Max tokens per request                                   │
│  - Daily/monthly quota per API key                         │
│  - Cost estimation before execution                        │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│              HolySheep AI API Gateway                       │
│         https://api.holysheep.ai/v1                          │
└─────────────────────────────────────────────────────────────┘

Cấu Hình Chi Tiết Với Node.js/TypeScript

Đây là implementation production-ready mà tôi sử dụng cho các dự án thực tế:

// security-filter.ts
import { Ratelimit } from '@upstash/ratelimit';
import { Redis } from '@upstash/redis';
import OpenAI from 'openai';

// HolySheep AI Configuration
const HOLYSHEEP_CONFIG = {
  baseURL: 'https://api.holysheep.ai/v1',
  apiKey: process.env.HOLYSHEEP_API_KEY,
  maxRetries: 3,
  timeout: 30000,
};

const client = new OpenAI({
  apiKey: HOLYSHEEP_CONFIG.apiKey,
  baseURL: HOLYSHEEP_CONFIG.baseURL,
  maxRetries: HOLYSHEEP_CONFIG.maxRetries,
  timeout: HOLYSHEEP_CONFIG.timeout,
});

// Token Budget Configuration
const TOKEN_BUDGETS = {
  free: { maxTokens: 4096, dailyLimit: 100000, monthlyLimit: 500000 },
  pro: { maxTokens: 32768, dailyLimit: 5000000, monthlyLimit: 50000000 },
  enterprise: { maxTokens: 128000, dailyLimit: -1, monthlyLimit: -1 },
};

// Dangerous patterns for injection detection
const INJECTION_PATTERNS = [
  /\b(ignore|disregard|forget)\s+(previous|all|above|system)\s+(instructions|prompts?|commands?)/gi,
  /\b(you\s+are\s+now|act\s+as|pretend\s+to\s+be)\b/gi,
  /\[SYSTEM_PROMPT\]|\[INST\]|\[AI\_ROLE\]/gi,
  /\x00-\x1F\x7F/gi, // Control characters
  /;

  constructor() {
    this.redis = new Redis({
      url: process.env.UPSTASH_REDIS_URL!,
      token: process.env.UPSTASH_REDIS_TOKEN!,
    });

    // Rate limiter: 100 requests per minute
    this.rateLimiter = new Ratelimit({
      redis: this.redis,
      limiter: Ratelimit.slidingWindow(100, '1 m'),
      analytics: true,
      prefix: 'ai_api_ratelimit',
    });

    this.usageTracker = new Map();
  }

  // Layer 1: Rate Limit Check
  async checkRateLimit(apiKey: string): Promise {
    const identifier = ratelimit:${apiKey};
    const result = await this.rateLimiter.limit(identifier);
    
    return {
      success: result.success,
      remaining: result.remaining,
      reset: result.reset,
    };
  }

  // Layer 2: Input Validation & Injection Detection
  validateInput(prompt: string): SecurityCheckResult {
    const riskScore = this.calculateRiskScore(prompt);
    const injectionDetected = this.detectInjection(prompt);
    
    if (injectionDetected.detected) {
      return {
        allowed: false,
        reason: Injection pattern detected: ${injectionDetected.pattern},
        riskScore: 100,
        estimatedCost: 0,
      };
    }

    // Check for blocked keywords
    const keywordMatch = this.checkBlockedKeywords(prompt);
    if (keywordMatch.found) {
      return {
        allowed: false,
        reason: Blocked keyword detected: ${keywordMatch.keyword},
        riskScore: 90,
        estimatedCost: 0,
      };
    }

    return {
      allowed: true,
      riskScore,
      estimatedCost: this.estimateCost(prompt, 4096),
    };
  }

  private detectInjection(prompt: string): { detected: boolean; pattern?: string } {
    for (const pattern of INJECTION_PATTERNS) {
      if (pattern.test(prompt)) {
        return { detected: true, pattern: pattern.toString() };
      }
    }
    return { detected: false };
  }

  private checkBlockedKeywords(prompt: string): { found: boolean; keyword?: string } {
    const lowerPrompt = prompt.toLowerCase();
    for (const keyword of BLOCKED_KEYWORDS) {
      if (lowerPrompt.includes(keyword)) {
        return { found: true, keyword };
      }
    }
    return { found: false };
  }

  private calculateRiskScore(prompt: string): number {
    let score = 0;
    
    // Length check (very long prompts may be attempts to bypass)
    if (prompt.length > 10000) score += 20;
    if (prompt.length > 50000) score += 30;
    
    // Repetition detection
    const uniqueChars = new Set(prompt).size;
    const repetitionRatio = uniqueChars / prompt.length;
    if (repetitionRatio < 0.1) score += 25;
    
    // Special character ratio
    const specialChars = (prompt.match(/[^a-zA-Z0-9\s]/g) || []).length;
    const specialRatio = specialChars / prompt.length;
    if (specialRatio > 0.3) score += 15;
    
    return Math.min(score, 100);
  }

  private estimateCost(prompt: string, maxTokens: number): number {
    // Rough estimate: ~1.5 tokens per word
    const inputTokens = Math.ceil(prompt.split(/\s+/).length * 1.5);
    const totalTokens = inputTokens + maxTokens;
    
    // DeepSeek V3.2 pricing: $0.42/MTok input, $1.20/MTok output
    const inputCost = (inputTokens / 1000000) * 0.42;
    const outputCost = (maxTokens / 1000000) * 1.20;
    
    return inputCost + outputCost;
  }

  // Layer 3: Token Budget Enforcement
  async checkTokenBudget(apiKey: string, tier: keyof typeof TOKEN_BUDGETS): Promise {
    const budget = TOKEN_BUDGETS[tier];
    const today = new Date().toISOString().split('T')[0];
    const monthKey = today.substring(0, 7);
    
    const dailyKey = usage:daily:${apiKey}:${today};
    const monthlyKey = usage:monthly:${apiKey}:${monthKey};
    
    const [dailyUsage, monthlyUsage] = await Promise.all([
      this.redis.get(dailyKey) || 0,
      this.redis.get(monthlyKey) || 0,
    ]);

    // -1 means unlimited
    if (budget.dailyLimit !== -1 && dailyUsage >= budget.dailyLimit) {
      return false;
    }
    
    if (budget.monthlyLimit !== -1 && monthlyUsage >= budget.monthlyLimit) {
      return false;
    }
    
    return true;
  }

  async updateUsage(apiKey: string, tokens: number): Promise {
    const today = new Date().toISOString().split('T')[0];
    const monthKey = today.substring(0, 7);
    
    const dailyKey = usage:daily:${apiKey}:${today};
    const monthlyKey = usage:monthly:${apiKey}:${monthKey};
    
    await Promise.all([
      this.redis.incrby(dailyKey, tokens),
      this.redis.incrby(monthlyKey, tokens),
      this.redis.expire(dailyKey, 86400),
      this.redis.expire(monthlyKey, 2592000),
    ]);
  }

  // Main secure API call
  async secureChatCompletion(
    apiKey: string,
    messages: OpenAI.Chat.ChatCompletionMessageParam[],
    tier: keyof typeof TOKEN_BUDGETS = 'free'
  ): Promise {
    const startTime = Date.now();

    // 1. Rate limit check
    const rateLimitResult = await this.checkRateLimit(apiKey);
    if (!rateLimitResult.success) {
      throw new Error(Rate limit exceeded. Reset at: ${new Date(rateLimitResult.reset).toISOString()});
    }

    // 2. Combine messages into prompt for validation
    const prompt = messages.map(m => m.content?.toString() || '').join('\n');
    
    // 3. Input validation
    const validationResult = this.validateInput(prompt);
    if (!validationResult.allowed) {
      throw new Error(Security filter blocked request: ${validationResult.reason});
    }

    // 4. Token budget check
    const budgetOk = await this.checkTokenBudget(apiKey, tier);
    if (!budgetOk) {
      throw new Error('Token budget exceeded for this period');
    }

    // 5. Execute API call
    const maxTokens = TOKEN_BUDGETS[tier].maxTokens;
    const response = await client.chat.completions.create({
      model: 'gpt-4.1',
      messages,
      max_tokens: maxTokens,
      temperature: 0.7,
    });

    // 6. Update usage tracking
    const totalTokens = (response.usage?.total_tokens || 0);
    await this.updateUsage(apiKey, totalTokens);

    // 7. Log for monitoring
    const latency = Date.now() - startTime;
    console.log(JSON.stringify({
      type: 'api_call',
      apiKey: apiKey.substring(0, 8) + '...',
      tokens: totalTokens,
      latency,
      cost: validationResult.estimatedCost,
      timestamp: new Date().toISOString(),
    }));

    return response;
  }
}

export const securityFilter = new AISecurityFilter();

Cấu Hình Python Với FastAPI

Với team sử dụng Python, đây là implementation tương đương:

# main.py
from fastapi import FastAPI, HTTPException, Header, Request
from fastapi.security import APIKeyHeader
from pydantic import BaseModel, Field
from typing import List, Optional
import asyncio
import time
import hashlib
from datetime import datetime, timedelta
import httpx

HolySheep AI Configuration

HOLYSHEEP_CONFIG = { "base_url": "https://api.holysheep.ai/v1", "api_key": "YOUR_HOLYSHEEP_API_KEY", # Thay bằng key thực tế "timeout": 30.0, }

Token Budget Configuration (USD per MTok)

MODEL_PRICING = { "gpt-4.1": {"input": 2.00, "output": 8.00}, # $8/MTok output "claude-sonnet-4.5": {"input": 3.00, "output": 15.00}, # $15/MTok output "gemini-2.5-flash": {"input": 0.10, "output": 2.50}, # $2.50/MTok output "deepseek-v3.2": {"input": 0.42, "output": 1.20}, # $0.42/MTok input } app = FastAPI(title="AI API Gateway with Security Filter")

Rate Limiting State (Trong production, dùng Redis)

rate_limit_store: dict = {}

Injection Patterns

INJECTION_PATTERNS = [ r"(ignore|disregard|forget)\s+(previous|all|above|system)\s+(instructions|prompts?|commands?)", r"(you\s+are\s+now|act\s+as|pretend\s+to\s+be)", r"\[SYSTEM_PROMPT\]|\[INST\]|\[AI_ROLE\]", r" int: """Estimate tokens (~1.5 tokens per word)""" return int(len(text.split()) * 1.5) def detect_injection(prompt: str) -> tuple[bool, Optional[str]]: import re for pattern in INJECTION_PATTERNS: if re.search(pattern, prompt, re.IGNORECASE): return True, pattern return False, None def check_blocked_content(prompt: str) -> tuple[bool, Optional[str]]: prompt_lower = prompt.lower() for keyword in BLOCKED_KEYWORDS: if keyword in prompt_lower: return True, keyword return False, None def estimate_cost(prompt: str, max_tokens: int, model: str) -> float: input_tokens = calculate_tokens(prompt) pricing = MODEL_PRICING.get(model, MODEL_PRICING["deepseek-v3.2"]) input_cost = (input_tokens / 1_000_000) * pricing["input"] output_cost = (max_tokens / 1_000_000) * pricing["output"] return input_cost + output_cost async def check_rate_limit(api_key: str, tier: str = "free") -> tuple[bool, int]: now = time.time() window = 60 # 1 minute window limit = {"free": 100, "pro": 1000, "enterprise": 10000}.get(tier, 100) if api_key not in rate_limit_store: rate_limit_store[api_key] = [] # Remove old requests rate_limit_store[api_key] = [ t for t in rate_limit_store[api_key] if now - t < window ] if len(rate_limit_store[api_key]) >= limit: return False, int(min(rate_limit_store[api_key]) + window - now) rate_limit_store[api_key].append(now) return True, limit - len(rate_limit_store[api_key]) @app.post("/v1/chat/completions") async def chat_completions( request: ChatRequest, authorization: str = Header(None) ): start_time = time.time() # Extract API key if not authorization or not authorization.startswith("Bearer "): raise HTTPException(status_code=401, detail="Missing or invalid API key") api_key = authorization.replace("Bearer ", "") api_key_hash = hashlib.sha256(api_key.encode()).hexdigest()[:12] # 1. Rate Limiting rate_ok, remaining = await check_rate_limit(api_key_hash) if not rate_ok: raise HTTPException( status_code=429, detail="Rate limit exceeded. Please wait before retrying." ) # 2. Combine all messages full_prompt = "\n".join([m.content for m in request.messages]) # 3. Injection Detection injection_found, injection_pattern = detect_injection(full_prompt) if injection_found: raise HTTPException( status_code=400, detail=f"Security filter: Potential prompt injection detected (pattern: {injection_pattern})" ) # 4. Content Filtering blocked_found, blocked_keyword = check_blocked_content(full_prompt) if blocked_found: raise HTTPException( status_code=400, detail=f"Security filter: Blocked content detected (keyword: {blocked_keyword})" ) # 5. Token Length Validation estimated_tokens = calculate_tokens(full_prompt) + request.max_tokens max_model_tokens = { "gpt-4.1": 32768, "claude-sonnet-4.5": 200000, "gemini-2.5-flash": 1000000, "deepseek-v3.2": 64000, }.get(request.model, 32000) if estimated_tokens > max_model_tokens: raise HTTPException( status_code=400, detail=f"Token limit exceeded. Max: {max_model_tokens}, Requested: {estimated_tokens}" ) # 6. Cost Estimation (Pre-check) estimated_cost = estimate_cost(full_prompt, request.max_tokens, request.model) print(f"[COST_PRECHECK] Estimated: ${estimated_cost:.4f} | Model: {request.model}") # 7. Call HolySheep AI API try: async with httpx.AsyncClient(timeout=HOLYSHEEP_CONFIG["timeout"]) as client: response = await client.post( f"{HOLYSHEEP_CONFIG['base_url']}/chat/completions", headers={ "Authorization": f"Bearer {HOLYSHEEP_CONFIG['api_key']}", "Content-Type": "application/json", }, json={ "model": request.model, "messages": [m.model_dump() for m in request.messages], "temperature": request.temperature, "max_tokens": request.max_tokens, } ) if response.status_code != 200: raise HTTPException( status_code=response.status_code, detail=f"API Error: {response.text}" ) result = response.json() # Log metrics latency = (time.time() - start_time) * 1000 # ms actual_tokens = result.get("usage", {}).get("total_tokens", 0) actual_cost = estimate_cost(full_prompt, actual_tokens, request.model) print(f"[API_METRICS] Latency: {latency:.2f}ms | Tokens: {actual_tokens} | Cost: ${actual_cost:.4f}") return result except httpx.TimeoutException: raise HTTPException(status_code=504, detail="Request timeout") except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.get("/health") async def health_check(): return { "status": "healthy", "timestamp": datetime.utcnow().isoformat(), "base_url": HOLYSHEEP_CONFIG["base_url"], } if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)

Kiểm Soát Đồng Thời (Concurrency Control)

Đây là phần nhiều kỹ sư bỏ qua nhưng lại critical cho chi phí. Tôi đã gặp trường hợp một request đồng thời gửi 50 batch → phí tăng 5000% trong 1 giờ.

# concurrency_control.py
import asyncio
import time
from typing import Dict, List, Optional
from dataclasses import dataclass
from collections import defaultdict
import threading

@dataclass
class SemaphoreConfig:
    max_concurrent: int
    max_queue_size: int
    timeout_seconds: float

class ConcurrencyController:
    """Semaphore-based concurrency control với queue timeout"""
    
    def __init__(self):
        self.semaphores: Dict[str, asyncio.Semaphore] = {}
        self.queues: Dict[str, asyncio.Queue] = {}
        self.active_count: Dict[str, int] = defaultdict(int)
        self.lock = threading.Lock()
        
    def get_semaphore(self, api_key: str, config: SemaphoreConfig) -> asyncio.Semaphore:
        with self.lock:
            if api_key not in self.semaphores:
                self.semaphores[api_key] = asyncio.Semaphore(config.max_concurrent)
                self.queues[api_key] = asyncio.Queue(maxsize=config.max_queue_size)
            return self.semaphores[api_key]
    
    async def execute_with_limit(
        self,
        api_key: str,
        coro,
        config: SemaphoreConfig,
        priority: int = 0
    ) -> any:
        """Execute coroutine với concurrency limit"""
        semaphore = self.get_semaphore(api_key, config)
        queue = self.queues[api_key]
        
        # Check queue size
        if queue.full():
            raise Exception(f"Queue full for API key. Max size: {config.max_queue_size}")
        
        async with semaphore:
            with self.lock:
                self.active_count[api_key] += 1
            
            try:
                result = await asyncio.wait_for(coro, timeout=config.timeout_seconds)
                return result
            finally:
                with self.lock:
                    self.active_count[api_key] -= 1

class BatchProcessor:
    """Process multiple requests với batching optimization"""
    
    def __init__(self, batch_size: int = 10, batch_timeout: float = 0.1):
        self.batch_size = batch_size
        self.batch_timeout = batch_timeout
        self.pending: List[tuple] = []
        self.lock = asyncio.Lock()
        
    async def add_request(self, api_key: str, prompt: str, semaphore: asyncio.Semaphore):
        """Add request to batch, auto-flush when batch is full"""
        async with self.lock:
            self.pending.append((api_key, prompt, semaphore))
            
            if len(self.pending) >= self.batch_size:
                return await self._flush_batch()
        
        # Return immediately if batch not full, let timeout handle flush
        asyncio.create_task(self._delayed_flush())
        return None
    
    async def _flush_batch(self):
        """Flush current batch"""
        async with self.lock:
            if not self.pending:
                return []
            
            batch = self.pending[:self.batch_size]
            self.pending = self.pending[self.batch_size:]
        
        # Process batch concurrently
        tasks = []
        for api_key, prompt, semaphore in batch:
            async def call_api(key, p):
                async with semaphore:
                    # Gọi HolySheep API
                    return {"api_key": key, "prompt": p, "status": "success"}
            tasks.append(call_api(api_key, prompt))
        
        results = await asyncio.gather(*tasks, return_exceptions=True)
        return results
    
    async def _delayed_flush(self):
        """Flush after timeout"""
        await asyncio.sleep(self.batch_timeout)
        if self.pending:
            return await self._flush_batch()

Usage Example

async def main(): controller = ConcurrencyController() batch_processor = BatchProcessor(batch_size=5, batch_timeout=0.5) configs = { "free_tier": SemaphoreConfig(max_concurrent=2, max_queue_size=10, timeout_seconds=30), "pro_tier": SemaphoreConfig(max_concurrent=10, max_queue_size=50, timeout_seconds=60), "enterprise_tier": SemaphoreConfig(max_concurrent=50, max_queue_size=200, timeout_seconds=120), } async def dummy_api_call(prompt: str): await asyncio.sleep(0.1) # Simulate API call return {"response": f"Processed: {prompt[:20]}..."} # Test concurrency control semaphore = controller.get_semaphore("test_key", configs["free_tier"]) tasks = [ controller.execute_with_limit("test_key", dummy_api_call(f"Request {i}"), configs["free_tier"]) for i in range(5) ] results = await asyncio.gather(*tasks) print(f"Processed {len(results)} requests concurrently") if __name__ == "__main__": asyncio.run(main())

Tối Ưu Chi Phí Với Smart Routing

Đây là kỹ thuật giúp tôi tiết kiệm 70% chi phí cho khách hàng. Thay vì luôn dùng GPT-4.1 ($8/MTok), tôi routing request đến model phù hợp:

# smart_routing.py
from enum import Enum
from typing import Optional, List
from dataclasses import dataclass
import asyncio

class TaskComplexity(Enum):
    SIMPLE = "simple"           # Chat, Q&A đơn giản
    MEDIUM = "medium"           # Summarize, translate
    COMPLEX = "complex"         # Code generation, analysis
    ADVANCED = "advanced"       # Multi-step reasoning

@dataclass
class ModelRoute:
    name: str
    max_tokens: int
    cost_per_mtok_input: float
    cost_per_mtok_output: float
    latency_p95_ms: float
    complexity_range: tuple[int, int]  # (min_score, max_score)

MODEL_ROUTES = {
    # DeepSeek V3.2 - $0.42/MTok input - Rẻ nhất, nhanh nhất
    "deepseek-v3.2": ModelRoute(
        name="deepseek-v3.2",
        max_tokens=64000,
        cost_per_mtok_input=0.42,
        cost_per_mtok_output=1.20,
        latency_p95_ms=45,  # <50ms như cam kết
        complexity_range=(0, 30),
    ),
    # Gemini 2.5 Flash - $2.50/MTok - Cân bằng
    "gemini-2.5-flash": ModelRoute(
        name="gemini-2.5-flash",
        max_tokens=1000000,
        cost_per_mtok_input=0.10,
        cost_per_mtok_output=2.50,
        latency_p95_ms=80,
        complexity_range=(20, 60),
    ),
    # GPT-4.1 - $8/MTok - Cho task phức tạp
    "gpt-4.1": ModelRoute(
        name="gpt-4.1",
        max_tokens=32768,
        cost_per_mtok_input=2.00,
        cost_per_mtok_output=8.00,
        latency_p95_ms=150,
        complexity_range=(50, 100),
    ),
}

class ComplexityAnalyzer:
    """Phân tích độ phức tạp của request để chọn model phù hợp"""
    
    COMPLEXITY_KEYWORDS = {
        # Tăng complexity
        "analyze": 10, "compare": 10, "evaluate": 12,
        "debug": 15, "optimize": 12, "architect": 18,
        "explain": 5, "implement": 15, "design": 14,
        # Giảm complexity
        "simple": -5, "quick": -3, "brief": -3,
        "hello": -10, "thanks": -10, "help": -5,
    }
    
    PATTERN_WEIGHTS = {
        r"\b(if|for|while|function|class)\b": 8,  # Code patterns
        r"\d{3,}": 5,  # Long numbers
        r"[A-Z]{4,}": 3,  # All caps (acronyms)
        r"\?{2,}": 8,  # Multiple questions
        r"(because|therefore|however|although)": 6,  # Complex reasoning
    }
    
    def analyze(self, prompt: str) -> int:
        """Trả về complexity score 0-100"""
        score = 30  # Base score
        prompt_lower = prompt.lower()
        
        # Keyword analysis
        for keyword, weight in self.COMPLEXITY_KEYWORDS.items():
            if keyword in prompt_lower:
                score += weight
        
        # Pattern matching
        import re
        for pattern, weight in self.PATTERN_WEIGHTS.items():
            matches = len(re.findall(pattern, prompt))
            score += matches * weight
        
        # Length factor (very long = complex)
        if len(prompt) > 5000:
            score += 15
        elif len(prompt) > 2000:
            score += 8
        
        return max(0, min(100, score))

class SmartRouter:
    """Route requests đến optimal model dựa trên cost-latency tradeoff"""
    
    def __init__(self):
        self.analyzer = ComplexityAnalyzer()
        self.cost_cache = {}
        
    def route(self, prompt: str, require_reasoning: bool = False) -> ModelRoute:
        """Chọn model tối ưu nhất"""
        complexity = self.analyzer.analyze(prompt)
        
        # Reasoning tasks cần model mạnh hơn
        if require_reasoning:
            complexity = max(complexity, 50)
        
        # Find best matching model
        for model_name, route in MODEL_ROUTES.items():
            min_c, max_c = route.complexity_range
            if min_c <= complexity <= max_c:
                return route
        
        # Default to mid-tier
        return MODEL_ROUTES["gemini-2.5-flash"]
    
    def estimate_cost(self, route: ModelRoute, input_tokens: int, output_tokens: int) -> float:
        """Tính chi phí ước tính"""
        input_cost = (input_tokens / 1_000_000) * route.cost_per_mtok_input
        output_cost = (output_tokens / 1_000_000) * route.cost_per_mtok_output
        return input_cost + output_cost
    
    async def execute(
        self, 
        prompt: str, 
        api_key: str,
        require_reasoning: bool = False
    ):
        """Execute với smart routing"""
        route = self.route(prompt, require_reasoning)
        
        # Estimate tokens
        input_tokens = int(len(prompt.split()) * 1.5)
        estimated_output = 500
        estimated_cost = self.estimate_cost(route, input_tokens, estimated_output)
        
        print(f"[ROUTING] Complexity: {self.analyzer.analyze(prompt)} | "
              f"Model: {route.name} | Est. Cost: ${estimated_cost:.4f}")
        
        # Call HolySheep API với model đã chọn
        import httpx
        async with httpx.AsyncClient() as client:
            response = await client.post(
                "https://api.holysheep.ai/v1/chat/completions",
                headers={"Authorization": f"Bearer {api_key}"},
                json={
                    "model": route.name,
                    "messages": [{"role": "user", "content": prompt}],
                    "max_tokens