Trong thế giới AI API, việc monitor performance không chỉ là best practice — mà là yếu tố sống còn quyết định chi phí vận hành và trải nghiệm người dùng. Tôi đã từng để một production system chạy không có monitoring suốt 3 ngày, và khi phát hiện, chi phí đã vượt ngân sách tháng đó. Bài viết này sẽ hướng dẫn bạn xây dựng hệ thống metrics collection hoàn chỉnh cho AI API.

Bắt Đầu với một Kịch Bản Lỗi Thực Tế

Đêm hôm đó, Slack alert reo liên tục lúc 2h sáng: ConnectionError: timeout after 30s - ai-service-prod-03. Tôi mở dashboard lên và nhìn thấy một con số khiến tôi bủn rủn — 15,000 requests bị timeout trong 1 giờ, mỗi request retry 3 lần, chi phí burn rate tăng 400%. Nguyên nhân? Một upstream service thay đổi response format không backward compatible.

Nếu lúc đó tôi có metrics latency histogram và error rate tracking chuẩn, tôi đã phát hiện ngay từ phút thứ 5 thay vì 60 phút. Đó là lý do bạn cần đọc bài viết này.

Tại Sao Metrics Collection Quan Trọng với HolySheep AI

Đăng ký tại đây để trải nghiệm HolySheep AI — nền tảng với chi phí chỉ từ $0.42/MTok (DeepSeek V3.2), tiết kiệm 85%+ so với các provider khác. Với mức giá này, việc track performance trở nên cực kỳ quan trọng để tối ưu chi phí.

Kiến Trúc Metrics Collection

Trước khi code, hãy hiểu kiến trúc tổng thể:

┌─────────────┐    ┌──────────────────┐    ┌─────────────────┐
│  Client App  │───▶│  Middleware/     │───▶│  HolySheep API  │
│  (Your Code) │    │  Proxy (Capture) │    │  api.holysheep  │
└─────────────┘    └──────────────────┘    └─────────────────┘
                          │
                          ▼
                   ┌──────────────────┐
                   │  Metrics Store   │
                   │  (Prometheus/    │
                   │   InfluxDB)      │
                   └──────────────────┘
                          │
                          ▼
                   ┌──────────────────┐
                   │  Dashboard       │
                   │  (Grafana)       │
                   └──────────────────┘

Triển Khai Metrics Collection với Python

Đây là code production-ready sử dụng prometheus_client để capture metrics tự động:

# metrics_collector.py
from prometheus_client import Counter, Histogram, Gauge, CollectorRegistry, generate_latest
import time
import functools
from typing import Callable, Any
import json
import httpx

Tạo custom registry để tránh conflict

REGISTRY = CollectorRegistry()

Định nghĩa metrics

REQUEST_COUNT = Counter( 'ai_api_requests_total', 'Total AI API requests', ['endpoint', 'model', 'status'], registry=REGISTRY ) REQUEST_LATENCY = Histogram( 'ai_api_request_duration_seconds', 'AI API request latency in seconds', ['endpoint', 'model'], buckets=[0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0], registry=REGISTRY ) TOKEN_USAGE = Counter( 'ai_api_tokens_total', 'Total tokens consumed', ['model', 'token_type'], # token_type: prompt/completion registry=REGISTRY ) ERROR_COUNT = Counter( 'ai_api_errors_total', 'Total API errors', ['endpoint', 'error_type', 'status_code'], registry=REGISTRY ) ACTIVE_REQUESTS = Gauge( 'ai_api_active_requests', 'Number of currently active requests', ['endpoint'], registry=REGISTRY ) class HolySheepMetricsClient: """Enhanced client với metrics collection tự động""" BASE_URL = "https://api.holysheep.ai/v1" def __init__(self, api_key: str): self.api_key = api_key self.client = httpx.Client( timeout=httpx.Timeout(60.0, connect=10.0), headers={ "Authorization": f"Bearer {api_key}", "Content-Type": "application/json" } ) def _extract_model_from_response(self, response_data: dict) -> str: """Extract model name từ response""" # HolySheep trả về model trong response return response_data.get('model', 'unknown') def _extract_tokens_from_response(self, response_data: dict) -> tuple: """Extract prompt và completion tokens""" usage = response_data.get('usage', {}) prompt_tokens = usage.get('prompt_tokens', 0) completion_tokens = usage.get('completion_tokens', 0) return prompt_tokens, completion_tokens def chat_completions(self, messages: list, model: str = "gpt-4.1", temperature: float = 0.7, max_tokens: int = 1000) -> dict: """Gửi chat completion request với full metrics tracking""" endpoint = "chat/completions" ACTIVE_REQUESTS.labels(endpoint=endpoint).inc() start_time = time.perf_counter() status = "success" error_type = None status_code = 200 try: payload = { "model": model, "messages": messages, "temperature": temperature, "max_tokens": max_tokens } response = self.client.post( f"{self.BASE_URL}/{endpoint}", json=payload ) status_code = response.status_code if response.status_code == 200: data = response.json() # Record token usage prompt_tokens, completion_tokens = self._extract_tokens_from_response(data) actual_model = self._extract_model_from_response(data) TOKEN_USAGE.labels(model=actual_model, token_type='prompt').inc(prompt_tokens) TOKEN_USAGE.labels(model=actual_model, token_type='completion').inc(completion_tokens) return data else: status = "error" error_type = f"http_{status_code}" response.raise_for_status() except httpx.TimeoutException as e: status = "error" error_type = "timeout" raise RuntimeError(f"Request timeout after 60s: {e}") except httpx.HTTPStatusError as e: status = "error" error_type = f"http_{e.response.status_code}" raise RuntimeError(f"HTTP {e.response.status_code}: {e.response.text}") except Exception as e: status = "error" error_type = type(e).__name__ raise finally: duration = time.perf_counter() - start_time REQUEST_COUNT.labels( endpoint=endpoint, model=model, status=status ).inc() REQUEST_LATENCY.labels( endpoint=endpoint, model=model ).observe(duration) if status == "error": ERROR_COUNT.labels( endpoint=endpoint, error_type=error_type, status_code=str(status_code) ).inc() ACTIVE_REQUESTS.labels(endpoint=endpoint).dec() def get_metrics(self) -> bytes: """Export metrics cho Prometheus scraping""" return generate_latest(REGISTRY) def close(self): self.client.close()

Decorator cho hàm async

def track_metrics(registry=REGISTRY): """Decorator để track metrics cho bất kỳ async function nào""" def decorator(func: Callable) -> Callable: @functools.wraps(func) async def wrapper(*args, **kwargs) -> Any: func_name = func.__name__ ACTIVE_REQUESTS.labels(endpoint=func_name).inc() start_time = time.perf_counter() status = "success" error_type = None try: result = await func(*args, **kwargs) return result except Exception as e: status = "error" error_type = type(e).__name__ raise finally: duration = time.perf_counter() - start_time REQUEST_COUNT.labels( endpoint=func_name, model="decorated", status=status ).inc() REQUEST_LATENCY.labels( endpoint=func_name, model="decorated" ).observe(duration) if status == "error": ERROR_COUNT.labels( endpoint=func_name, error_type=error_type, status_code="decorator" ).inc() ACTIVE_REQUESTS.labels(endpoint=func_name).dec() return wrapper return decorator

Async Client với Full Observability

Với các ứng dụng high-throughput, bạn cần async client để handle concurrent requests:

# async_metrics_client.py
import asyncio
import httpx
import time
from prometheus_client import Counter, Histogram, Gauge, CollectorRegistry, generate_latest
from dataclasses import dataclass, field
from typing import Optional
from datetime import datetime

REGISTRY = CollectorRegistry()

Real-time cost tracking

COST_USD = Counter( 'ai_api_cost_usd_total', 'Total API cost in USD', ['model', 'operation'], registry=REGISTRY )

Request size tracking

REQUEST_SIZE = Histogram( 'ai_api_request_size_bytes', 'Request payload size', ['endpoint'], registry=REGISTRY ) RESPONSE_SIZE = Histogram( 'ai_api_response_size_bytes', 'Response payload size', ['endpoint'], registry=REGISTRY )

HolySheep pricing (updated 2026)

HOLYSHEEP_PRICING = { 'gpt-4.1': {'input': 8.0, 'output': 8.0}, # $8/MTok 'claude-sonnet-4.5': {'input': 15.0, 'output': 15.0}, # $15/MTok 'gemini-2.5-flash': {'input': 2.50, 'output': 2.50}, # $2.50/MTok 'deepseek-v3.2': {'input': 0.42, 'output': 0.42}, # $0.42/MTok } @dataclass class RequestMetrics: """Structured metrics cho mỗi request""" request_id: str start_time: float end_time: Optional[float] = None model: str = "" tokens_prompt: int = 0 tokens_completion: int = 0 status_code: int = 0 error: Optional[str] = None cost_usd: float = 0.0 class AsyncMetricsClient: """Production-ready async client với comprehensive metrics""" BASE_URL = "https://api.holysheep.ai/v1" def __init__(self, api_key: str, metrics_store: list = None): self.api_key = api_key self.metrics_store = metrics_store or [] # Store cho analysis self._request_count = 0 self._lock = asyncio.Lock() # Connection pooling self.limits = httpx.Limits( max_keepalive_connections=20, max_connections=100 ) self.timeout = httpx.Timeout(60.0, connect=10.0) async def __aenter__(self): self.client = httpx.AsyncClient( limits=self.limits, timeout=self.timeout, headers={ "Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json" } ) return self async def __aexit__(self, exc_type, exc_val, exc_tb): await self.client.aclose() def _calculate_cost(self, model: str, prompt_tokens: int, completion_tokens: int) -> float: """Tính chi phí theo HolySheep pricing""" pricing = HOLYSHEEP_PRICING.get(model, {'input': 8.0, 'output': 8.0}) prompt_cost = (prompt_tokens / 1_000_000) * pricing['input'] completion_cost = (completion_tokens / 1_000_000) * pricing['output'] return round(prompt_cost + completion_cost, 6) # Precision: 6 decimal async def chat_completion( self, messages: list, model: str = "deepseek-v3.2", # Default sang model rẻ nhất temperature: float = 0.7, max_tokens: int = 1000, request_id: str = None ) -> dict: """Chat completion với full metrics""" request_id = request_id or f"req_{int(time.time()*1000)}" metrics = RequestMetrics( request_id=request_id, start_time=time.perf_counter(), model=model ) async with self._lock: self._request_count += 1 metrics_store_id = self._request_count try: payload = { "model": model, "messages": messages, "temperature": temperature, "max_tokens": max_tokens } # Track request size request_json = json.dumps(payload).encode() REQUEST_SIZE.labels(endpoint='chat/completions').observe(len(request_json)) start = time.perf_counter() response = await self.client.post( f"{self.BASE_URL}/chat/completions", json=payload, content=request_json ) metrics.end_time = time.perf_counter() metrics.status_code = response.status_code if response.status_code == 200: data = response.json() # Track response size response_json = response.content RESPONSE_SIZE.labels(endpoint='chat/completions').observe(len(response_json)) # Extract usage usage = data.get('usage', {}) metrics.tokens_prompt = usage.get('prompt_tokens', 0) metrics.tokens_completion = usage.get('completion_tokens', 0) # Calculate cost metrics.cost_usd = self._calculate_cost( model, metrics.tokens_prompt, metrics.tokens_completion ) # Record metrics COST_USD.labels(model=model, operation='chat').inc(metrics.cost_usd) return data else: metrics.error = f"HTTP {response.status_code}" raise RuntimeError(f"API Error: {response.text}") except Exception as e: metrics.end_time = time.perf_counter() metrics.error = type(e).__name__ raise finally: # Store metrics self.metrics_store.append(metrics) def get_cost_summary(self) -> dict: """Tính tổng chi phí từ metrics store""" total_cost = 0.0 by_model = {} for m in self.metrics_store: total_cost += m.cost_usd by_model[m.model] = by_model.get(m.model, 0) + m.cost_usd return { 'total_cost_usd': round(total_cost, 4), 'by_model': {k: round(v, 4) for k, v in by_model.items()}, 'total_requests': len(self.metrics_store) } def export_prometheus(self) -> bytes: """Export metrics cho Prometheus""" return generate_latest(REGISTRY)

Usage example

async def main(): async with AsyncMetricsClient("YOUR_HOLYSHEEP_API_KEY") as client: response = await client.chat_completion( messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain metrics collection in 3 sentences."} ], model="deepseek-v3.2" ) print(f"Response: {response['choices'][0]['message']['content']}") print(f"Cost: ${client.get_cost_summary()['total_cost_usd']}") if __name__ == "__main__": asyncio.run(main())

Dashboard Grafana cho AI API Monitoring

Để visualize metrics, bạn cần cấu hình Prometheus scrape endpoint và import dashboard này:

# docker-compose.yml cho full monitoring stack
version: '3.8'

services:
  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.enable-lifecycle'

  grafana:
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana_data:/var/lib/grafana
    depends_on:
      - prometheus

  your-app:
    build: .
    ports:
      - "8000:8000"
    environment:
      - HOLYSHEEP_API_KEY=${HOLYSHEEP_API_KEY}
    volumes:
      - ./app:/app

volumes:
  prometheus_data:
  grafana_data:
# prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'ai-api-metrics'
    static_configs:
      - targets: ['your-app:8000']
    metrics_path: '/metrics'
    scrape_interval: 5s  # Frequent cho latency tracking

Tính Năng Nâng Cao: Real-time Cost Alerting

Với HolySheep AI, việc track chi phí real-time cực kỳ quan trọng. Đây là script alert khi chi phí vượt ngưỡng:

# cost_alert.py
import asyncio
import httpx
from datetime import datetime, timedelta
from prometheus_client import Counter, Gauge

Budget thresholds

DAILY_BUDGET_USD = 100.0 HOURLY_BUDGET_USD = 10.0 class CostAlertManager: """Monitor và alert khi chi phí vượt ngưỡng""" def __init__(self, api_key: str, webhook_url: str = None): self.api_key = api_key self.webhook_url = webhook_url self.client = httpx.AsyncClient() # Tracking metrics self.hourly_cost = 0.0 self.daily_cost = 0.0 self.request_history = [] async def check_cost_threshold(self): """Kiểm tra ngưỡng chi phí""" now = datetime.utcnow() hour_ago = now - timedelta(hours=1) day_ago = now - timedelta(days=1) # Filter requests trong timeframe recent_requests = [ r for r in self.request_history if r['timestamp'] > hour_ago ] self.hourly_cost = sum(r['cost_usd'] for r in recent_requests) daily_requests = [ r for r in self.request_history if r['timestamp'] > day_ago ] self.daily_cost = sum(r['cost_usd'] for r in daily_requests) alerts = [] # Hourly alert if self.hourly_cost > HOURLY_BUDGET_USD: alerts.append({ 'severity': 'warning', 'message': f"Hourly cost ${self.hourly_cost:.4f} exceeds budget ${HOURLY_BUDGET_USD}" }) # Daily alert if self.daily_cost > DAILY_BUDGET_USD: alerts.append({ 'severity': 'critical', 'message': f"Daily cost ${self.daily_cost:.4f} exceeds budget ${DAILY_BUDGET_USD}" }) return alerts async def send_alert(self, alert: dict): """Gửi alert qua webhook""" if not self.webhook_url: print(f"ALERT: {alert}") return payload = { 'text': f"🚨 AI API Cost Alert", 'attachments': [{ 'color': 'danger' if alert['severity'] == 'critical' else 'warning', 'fields': [ {'title': 'Message', 'value': alert['message'], 'short': False}, {'title': 'Time', 'value': datetime.utcnow().isoformat(), 'short': True} ] }] } await self.client.post(self.webhook_url, json=payload) async def track_request(self, model: str, tokens: int, cost_usd: float): """Track request và kiểm tra alert""" self.request_history.append({ 'timestamp': datetime.utcnow(), 'model': model, 'tokens': tokens, 'cost_usd': cost_usd }) # Chỉ keep 7 days data cutoff = datetime.utcnow() - timedelta(days=7) self.request_history = [ r for r in self.request_history if r['timestamp'] > cutoff ] # Check thresholds alerts = await self.check_cost_threshold() for alert in alerts: await self.send_alert(alert) async def get_cost_report(self) -> dict: """Generate cost report chi tiết""" now = datetime.utcnow() return { 'current_hour': { 'cost': round(self.hourly_cost, 4), 'budget': HOURLY_BUDGET_USD, 'usage_percent': round((self.hourly_cost / HOURLY_BUDGET_USD) * 100, 1) }, 'current_day': { 'cost': round(self.daily_cost, 4), 'budget': DAILY_BUDGET_USD, 'usage_percent': round((self.daily_cost / DAILY_BUDGET_USD) * 100, 1) }, 'total_requests': len(self.request_history) }

Lỗi Thường Gặp và Cách Khắc Phục

1. Lỗi 401 Unauthorized - Invalid API Key

Mô tả: Khi bạn nhận được response {"error": {"message": "Invalid API key provided", "type": "invalid_request_error", "code": 401}}

Nguyên nhân:

Cách khắc phục:

# Kiểm tra và validate API key
import httpx

async def validate_holysheep_key(api_key: str) -> dict:
    """Validate HolySheep API key trước khi sử dụng"""
    
    client = httpx.Client(
        base_url="https://api.holysheep.ai/v1",
        headers={"Authorization": f"Bearer {api_key}"}
    )
    
    try:
        # Test với lightweight request
        response = client.post(
            "/chat/completions",
            json={
                "model": "deepseek-v3.2",
                "messages": [{"role": "user", "content": "hi"}],
                "max_tokens": 5
            }
        )
        
        if response.status_code == 200:
            return {"valid": True, "credits_remaining": "check dashboard"}
        else:
            return {"valid": False, "error": response.json()}
            
    except httpx.HTTPStatusError as e:
        if e.response.status_code == 401:
            return {
                "valid": False,
                "error": "Invalid API key. Get your key from https://www.holysheep.ai/register"
            }
        raise
    finally:
        client.close()

2. Lỗi Connection Timeout - Request exceeds 60s

Mô tả: httpx.ConnectTimeout: Connection timeout after 10s hoặc httpx.ReadTimeout: Read timeout after 60s

Nguyên nhân:

Cách khắc phục:

# Retry logic với exponential backoff
import asyncio
import httpx
from tenacity import retry, stop_after_attempt, wait_exponential

class ResilientHolySheepClient:
    """Client với retry tự động và fallback"""
    
    BASE_URL = "https://api.holysheep.ai/v1"
    
    def __init__(self, api_key: str):
        self.api_key = api_key
        self.timeout = httpx.Timeout(120.0, connect=15.0)  # Tăng timeout
        self.client = httpx.Client(timeout=self.timeout)
    
    @retry(
        stop=stop_after_attempt(3),
        wait=wait_exponential(multiplier=1, min=2, max=30)
    )
    def chat_with_retry(self, messages: list, model: str = "deepseek-v3.2"):
        """Gửi request với automatic retry"""
        
        response = self.client.post(
            f"{self.BASE_URL}/chat/completions",
            json={
                "model": model,
                "messages": messages,
                "max_tokens": 1000
            }
        )
        
        # Retry only on these status codes
        if response.status_code in [408, 429, 500, 502, 503, 504]:
            raise httpx.HTTPStatusError(
                f"Temporary error: {response.status_code}",
                request=response.request,
                response=response
            )
        
        return response.json()
    
    def get_health_status(self) -> dict:
        """Check API health status"""
        try:
            test_response = self.chat_with_retry(
                messages=[{"role": "user", "content": "test"}],
                model="deepseek-v3.2"
            )
            return {"status": "healthy", "latency_ms": "low"}
        except Exception as e:
            return {"status": "unhealthy", "error": str(e)}

3. Lỗi Rate Limit - 429 Too Many Requests

Mô tả: {"error": {"message": "Rate limit exceeded", "type": "rate_limit_error", "code": 429}}

Nguyên nhân:

Cách khắc phục:

# Rate limiter với semaphore và automatic throttling
import asyncio
import time
from collections import deque
from httpx import AsyncClient, Timeout

class RateLimitedClient:
    """Client với built-in rate limiting"""
    
    BASE_URL = "https://api.holysheep.ai/v1"
    
    def __init__(self, api_key: str, requests_per_minute: int = 60):
        self.api_key = api_key
        self.rpm_limit = requests_per_minute
        
        # Token bucket algorithm
        self.tokens = requests_per_minute
        self.last_update = time.time()
        self.refill_rate = requests_per_minute / 60.0  # tokens per second
        
        # Semaphore để limit concurrent requests
        self.semaphore = asyncio.Semaphore(10)
        
        # Queue để track requests
        self.request_times = deque(maxlen=1000)
    
    async def _wait_for_token(self):
        """Đợi cho đến khi có token available"""
        while self.tokens < 1:
            # Refill tokens
            now = time.time()
            elapsed = now - self.last_update
            self.tokens += elapsed * self.refill_rate
            self.last_update = now
            
            if self.tokens < 1:
                await asyncio.sleep(0.1)
        
        self.tokens -= 1
    
    async def chat_completion(self, messages: list, model: str = "deepseek-v3.2") -> dict:
        """Gửi request với rate limiting tự động"""
        
        async with self.semaphore:  # Max 10 concurrent
            await self._wait_for_token()
            
            async with AsyncClient(
                base_url=self.BASE_URL,
                timeout=Timeout(60.0),
                headers={
                    "Authorization": f"Bearer {self.api_key}",
                    "Content-Type": "application/json"
                }
            ) as client:
                start = time.perf_counter()
                
                response = await client.post(
                    "/chat/completions",
                    json={
                        "model": model,
                        "messages": messages,
                        "max_tokens": 1000
                    }
                )
                
                latency = time.perf_counter() - start
                self.request_times.append(time.time())
                
                if response.status_code == 429:
                    # Parse retry-after từ response
                    retry_after = int(response.headers.get('retry-after', 60))
                    print(f"Rate limited. Waiting {retry_after}s...")
                    await asyncio.sleep(retry_after)
                    # Retry once
                    response = await client.post(
                        "/chat/completions",
                        json={
                            "model": model,
                            "messages": messages,
                            "max_tokens": 1000
                        }
                    )
                
                response.raise_for_status()
                return response.json()
    
    def get_rate_limit_status(self) -> dict:
        """Check current rate limit status"""
        now = time.time()
        recent_requests = sum(1 for t in self.request_times if now - t < 60)
        
        return {
            "requests_last_minute": recent_requests,
            "limit": self.rpm_limit,
            "available_tokens": round(self.tokens, 2),
            "utilization_percent": round((recent_requests / self.rpm_limit) * 100, 1)
        }

4. Lỗi Model Not Found - Invalid Model Name

Mô tả: {"error": {"message": "Model 'gpt-5' not found", "type": "invalid_request_error"}}

Nguyên nhân:

Cách khắc phục:

# Model registry với validation
from dataclasses import dataclass
from typing import List, Optional

@dataclass
class ModelInfo:
    name: str
    input_cost_per_mtok: float
    output_cost_per_mtok: float
    max_tokens: int
    supports_streaming: bool

HolySheep supported models (updated 2026)

HOLYSHEEP_MODELS = { "gpt-4.1": ModelInfo( name="gpt-4.1", input_cost_per_mtok=8.0, output_cost_per_mtok=8.0, max_tokens=128000, supports_streaming=True ), "claude-sonnet-4.5": ModelInfo( name="claude-sonnet-4.5", input_cost_per_mtok=15.0, output_cost_per_mtok=15.0, max_tokens=200000, supports_streaming=True ), "gemini-2.5-flash": ModelInfo( name="gemini-2.5-flash", input_cost_per_mtok=2.50, output_cost_per_mtok=2.50, max_tokens=1000000, supports_streaming=True ), "deepseek-v3.2": ModelInfo( name="deepseek-v3.2", input_cost_per_mtok=0.42, output_cost_per_mtok=0.42, max_tokens=64000, supports_streaming=True ), } class ModelValidator: """Validate và suggest models""" def __init__(self, available_models: dict = HOLYSHEEP_MODELS): self.models = available_models def validate_model(self, model_name: str) -> tuple[bool, Optional[str]]: """Validate model name""" if model_name in self.models: return True, None # Suggest similar model suggestions = self._find_similar_models(model_name) if suggestions: return False, f"Model '{model_name}' not found. Did you mean: {', '.join(suggestions)}?" return False, f"Model '{model_name}' not found. Available models: {', '.join(self.models.keys())}" def _find_similar_models(self, query: str) -> List[str]: """Tìm models tương tự bằng simple matching""" query_lower = query.lower() suggestions = [] for model_name in self.models: # Check prefix match if model_name.startswith(query_lower.split('-')[0]): suggestions.append(model_name) return suggestions[:3] # Return top 3 def get_cheapest_model(self) -> str: