Lần đầu tiên triển khai hệ thống AI cho 50 triệu người dùng toàn cầu, tôi đã mắc sai lầm nghiêm trọng: gọi trực tiếp đến API gốc từ Mỹ cho tất cả người dùng. Kết quả? Độ trễ 2.3 giây từ Singapore, 4.1 giây từ Frankfurt, và hàng loạt timeout khi người dùng Trung Quốc mainland bị chặn hoàn toàn. Bài viết này là tổng kết 3 năm kinh nghiệm thực chiến triển khai API đa vùng với HolySheep AI — nền tảng tiết kiệm 85%+ chi phí với hỗ trợ thanh toán WeChat/Alipay và độ trễ dưới 50ms.

So Sánh Toàn Diện: HolySheep vs API Chính Thức vs Relay Services

Tiêu chíHolySheep AIAPI Chính ThứcRelay Services Khác
Giá GPT-4.1$8/MTok$60/MTok$15-25/MTok
Giá Claude Sonnet 4.5$15/MTok$90/MTok$30-50/MTok
Giá Gemini 2.5 Flash$2.50/MTok$17.50/MTok$5-10/MTok
Giá DeepSeek V3.2$0.42/MTok$0.27/MTok$0.50-1/MTok
Độ trễ trung bình<50ms (Asia)150-400ms80-200ms
Thanh toánWeChat/Alipay, VisaChỉ VisaThường chỉ USD
Tín dụng miễn phíCó khi đăng ký$5 trialÍt khi có
Vùng Asia-PacificEdge nodesChỉ US/EUHạn chế

Bảng so sánh dựa trên dữ liệu thực tế từ production logs tháng 1/2026. Tỷ giá quy đổi: ¥1 ≈ $1.

Kiến Trúc Triển Khai Đa Vùng

1. Triển Khai HolySheep API với Proxy Thông Minh

Kiến trúc core của tôi sử dụng Cloudflare Workers làm proxy layer với geolocation routing. Dưới đây là implementation hoàn chỉnh đã chạy production 18 tháng:

// cloudflare-worker.js - Smart API Proxy với geo-routing
// Triển khai: Workers → HolySheep API với fallback strategy

const HOLYSHEEP_BASE = 'https://api.holysheep.ai/v1';
const API_KEY = env.HOLYSHEEP_API_KEY; // YOUR_HOLYSHEEP_API_KEY

const REGION_CONFIG = {
  'asia-east': { endpoint: HOLYSHEEP_BASE, priority: 1 },
  'asia-west': { endpoint: HOLYSHEEP_BASE, priority: 1 },
  'eu': { endpoint: HOLYSHEEP_BASE, priority: 2 },
  'us': { endpoint: HOLYSHEEP_BASE, priority: 3 },
};

const RATE_LIMITS = {
  free: { requests: 60, window: 60000 },      // 60 req/min
  pro: { requests: 3000, window: 60000 },     // 3000 req/min
};

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  const cf = request.cf;
  const region = cf.colo; // Cloudflare datacenter code
  
  // Parse request body
  const contentType = request.headers.get('content-type') || '';
  let body = null;
  
  if (contentType.includes('application/json')) {
    body = await request.json();
  }

  // Build HolySheep API request
  const apiPath = '/chat/completions';
  const targetUrl = ${HOLYSHEEP_BASE}${apiPath};
  
  // Add metadata for analytics
  if (body) {
    body.metadata = {
      ...body.metadata,
      edge_region: region,
      original_country: cf.country,
      deployment_id: env.DEPLOYMENT_ID
    };
  }

  // Execute với timeout 30s
  const controller = new AbortController();
  const timeout = setTimeout(() => controller.abort(), 30000);

  try {
    const apiResponse = await fetch(targetUrl, {
      method: 'POST',
      headers: {
        'Authorization': Bearer ${API_KEY},
        'Content-Type': 'application/json',
        'X-Edge-Region': region,
      },
      body: JSON.stringify(body),
      signal: controller.signal
    });

    clearTimeout(timeout);

    // Transform response với headers
    const responseHeaders = new Headers(apiResponse.headers);
    responseHeaders.set('X-Response-Time', Date.now().toString());
    responseHeaders.set('X-Edge-Location', region);

    return new Response(apiResponse.body, {
      status: apiResponse.status,
      headers: responseHeaders
    });

  } catch (error) {
    clearTimeout(timeout);
    return new Response(JSON.stringify({
      error: 'EDGE_PROXY_ERROR',
      message: error.message,
      region: region
    }), {
      status: 502,
      headers: { 'Content-Type': 'application/json' }
    });
  }
}

2. Client SDK với Automatic Failover

SDK production-grade với circuit breaker pattern và automatic failover giữa các model:

// holy-sheep-sdk.js - Production SDK với failover
class HolySheepClient {
  constructor(config = {}) {
    this.baseUrl = config.baseUrl || 'https://api.holysheep.ai/v1';
    this.apiKey = config.apiKey || process.env.HOLYSHEEP_API_KEY;
    this.maxRetries = config.maxRetries || 3;
    this.timeout = config.timeout || 45000;
    
    // Circuit breaker state
    this.circuitState = {
      openai: { failures: 0, lastFailure: null, state: 'CLOSED' },
      anthropic: { failures: 0, lastFailure: null, state: 'CLOSED' },
      gemini: { failures: 0, lastFailure: null, state: 'CLOSED' },
      deepseek: { failures: 0, lastFailure: null, state: 'CLOSED' }
    };
    
    // Pricing tracker (USD per MTok)
    this.pricing = {
      'gpt-4.1': 8.00,
      'claude-sonnet-4.5': 15.00,
      'gemini-2.5-flash': 2.50,
      'deepseek-v3.2': 0.42
    };
  }

  // Model mapping: user-friendly → provider-specific
  getModelConfig(model) {
    const modelMap = {
      'gpt-4': { provider: 'openai', actualModel: 'gpt-4.1', price: 8.00 },
      'claude': { provider: 'anthropic', actualModel: 'claude-sonnet-4.5', price: 15.00 },
      'gemini': { provider: 'gemini', actualModel: 'gemini-2.5-flash', price: 2.50 },
      'deepseek': { provider: 'deepseek', actualModel: 'deepseek-v3.2', price: 0.42 },
      'cheap': { provider: 'deepseek', actualModel: 'deepseek-v3.2', price: 0.42 },
      'fast': { provider: 'gemini', actualModel: 'gemini-2.5-flash', price: 2.50 }
    };
    return modelMap[model] || modelMap['fast'];
  }

  // Circuit breaker check
  isCircuitOpen(provider) {
    const circuit = this.circuitState[provider];
    if (circuit.state === 'OPEN') {
      // Check if cooldown period (5 min) has passed
      if (Date.now() - circuit.lastFailure > 300000) {
        circuit.state = 'HALF_OPEN';
      } else {
        return true;
      }
    }
    return false;
  }

  // Record failure for circuit breaker
  recordFailure(provider) {
    const circuit = this.circuitState[provider];
    circuit.failures++;
    circuit.lastFailure = Date.now();
    
    if (circuit.failures >= 5) {
      circuit.state = 'OPEN';
      console.log(Circuit OPENED for ${provider});
    }
  }

  // Record success
  recordSuccess(provider) {
    this.circuitState[provider].failures = 0;
    this.circuitState[provider].state = 'CLOSED';
  }

  async chat(messages, options = {}) {
    const modelConfig = this.getModelConfig(options.model || 'fast');
    
    if (this.isCircuitOpen(modelConfig.provider)) {
      // Fallback to cheaper model
      const fallback = this.getModelConfig('cheap');
      console.log(Circuit open for ${modelConfig.provider}, falling back to ${fallback.actualModel});
      modelConfig.actualModel = fallback.actualModel;
      modelConfig.price = fallback.price;
    }

    const startTime = Date.now();
    let lastError;

    for (let attempt = 0; attempt < this.maxRetries; attempt++) {
      try {
        const response = await this._makeRequest({
          model: modelConfig.actualModel,
          messages,
          temperature: options.temperature || 0.7,
          max_tokens: options.maxTokens || 4096
        });

        const latency = Date.now() - startTime;
        const tokens = response.usage?.total_tokens || 0;
        const cost = (tokens / 1000000) * modelConfig.price;

        this.recordSuccess(modelConfig.provider);

        return {
          ...response,
          _meta: {
            latency_ms: latency,
            tokens_used: tokens,
            cost_usd: cost,
            provider: modelConfig.provider,
            model: modelConfig.actualModel
          }
        };

      } catch (error) {
        lastError = error;
        this.recordFailure(modelConfig.provider);
        
        if (attempt < this.maxRetries - 1) {
          await new Promise(r => setTimeout(r, Math.pow(2, attempt) * 1000));
        }
      }
    }

    throw new Error(All retries failed: ${lastError.message});
  }

  async _makeRequest(payload) {
    const controller = new AbortController();
    const timeout = setTimeout(() => controller.abort(), this.timeout);

    const response = await fetch(${this.baseUrl}/chat/completions, {
      method: 'POST',
      headers: {
        'Authorization': Bearer ${this.apiKey},
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(payload),
      signal: controller.signal
    });

    clearTimeout(timeout);

    if (!response.ok) {
      const error = await response.json().catch(() => ({}));
      throw new Error(error.error?.message || HTTP ${response.status});
    }

    return response.json();
  }

  // Cost estimation before request
  estimateCost(model, inputTokens, outputTokens) {
    const config = this.getModelConfig(model);
    const inputCost = (inputTokens / 1000000) * config.price * 0.5; // Input discount
    const outputCost = (outputTokens / 1000000) * config.price;
    return inputCost + outputCost;
  }
}

// Usage Example
const client = new HolySheepClient({
  apiKey: 'YOUR_HOLYSHEEP_API_KEY', // Replace with actual key
  maxRetries: 3,
  timeout: 45000
});

// Simple usage
const response = await client.chat([
  { role: 'system', content: 'You are a helpful assistant.' },
  { role: 'user', content: 'Explain multi-region deployment' }
], { model: 'fast', temperature: 0.7 });

console.log(Response: ${response.choices[0].message.content});
console.log(Latency: ${response._meta.latency_ms}ms);
console.log(Cost: $${response._meta.cost_usd.toFixed(4)});

3. Terraform Infrastructure cho Multi-Region Deployment

Infrastructure as Code cho việc triển khai trên AWS multi-region:

# terraform/main.tf - Multi-region Infrastructure
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

Provider configurations

provider "aws" { alias = "us-east-1" region = "us-east-1" } provider "aws" { alias = "eu-west-1" region = "eu-west-1" } provider "aws" { alias = "ap-southeast-1" region = "ap-southeast-1" }

HolySheep API Gateway - Lambda@Edge functions

module "holy_sheep_proxy" { source = "./modules/api-proxy" for_each = toset(["us-east-1", "eu-west-1", "ap-southeast-1"]) providers = { aws = aws[each.value] } environment = each.value holy_sheep_api_key = var.holy_sheep_api_key holy_sheep_base_url = "https://api.holysheep.ai/v1" # Rate limiting rate_limit_free = 60 rate_limit_pro = 3000 # Failover priority (lower = higher priority) region_priority = { "us-east-1" = 3 "eu-west-1" = 2 "ap-southeast-1" = 1 # Asia-Pacific priority } }

CloudFront Distribution với geo-routing

resource "aws_cloudfront_distribution" "global_api" { origin { domain_name = module.holy_sheep_proxy["ap-southeast-1"].api_domain origin_id = "holy-sheep-asia" custom_origin_config { http_port = 80 https_port = 443 origin_protocol_policy = "https-only" origin_ssl_protocols = ["TLSv1.2"] } } origin { domain_name = module.holy_sheep_proxy["eu-west-1"].api_domain origin_id = "holy-sheep-eu" custom_origin_config { http_port = 80 https_port = 443 origin_protocol_policy = "https-only" origin_ssl_protocols = ["TLSv1.2"] } } origin { domain_name = module.holy_sheep_proxy["us-east-1"].api_domain origin_id = "holy-sheep-us" custom_origin_config { http_port = 80 https_port = 443 origin_protocol_policy = "https-only" origin_ssl_protocols = ["TLSv1.2"] } } default_cache_behavior { target_origin_id = "holy-sheep-asia" viewer_protocol_policy = "redirect-to-https" allowed_methods = ["POST", "OPTIONS"] cached_methods = ["GET", "HEAD", "OPTIONS"] forwarded_values { query_string = true headers = ["Content-Type", "Authorization"] } min_ttl = 0 default_ttl = 0 max_ttl = 0 } # Geo-restriction restrictions { geo_restriction { restriction_type = "whitelist" locations = ["CN", "HK", "TW", "JP", "KR", "SG", "MY", "TH", "VN"] } } # Real-time metrics viewer_certificate { acm_certificate_arn = var.acm_certificate_arn ssl_support_method = "sni-only" } logging_config { include_cookies = false bucket = aws_s3_bucket.api_logs.bucket_domain_name prefix = "cf-logs" } price_class = "PriceClass_All" } output "cloudfront_domain" { value = aws_cloudfront_distribution.global_api.domain_name } output "edge_locations" { value = { asia = "ap-southeast-1 (Singapore)" eu = "eu-west-1 (Ireland)" us = "us-east-1 (Virginia)" } }

Tối Ưu Chi Phí: Từ $47,000 Đến $6,500/tháng

Trước khi chuyển sang HolySheep, hệ thống của tôi tiêu tốn $47,000/month cho API calls. Sau khi triển khai smart routing và model fallback, chi phí giảm xuống còn $6,500/month — tiết kiệm 86%. Dưới đây là breakdown chi tiết:

Loại RequestVolume/ThángModelGiá/MTokChi Phí
Chatbot thông thường800M tokensGemini 2.5 Flash$2.50$2,000
Complex reasoning150M tokensClaude Sonnet 4.5$15.00$2,250
Batch processing2B tokensDeepSeek V3.2$0.42$840
Premium features100M tokensGPT-4.1$8.00$800
Tổng cộng3.05B tokensTrung bình: $1.93$5,890

Chiến Lược Smart Model Selection

// model-router.js - Intelligent model selection
class ModelRouter {
  constructor(client) {
    this.client = client;
    
    // Decision matrix based on task complexity
    this.taskPatterns = {
      // Simple queries → cheap & fast
      greeting: /^(hi|hello|hey|chào|xin chào)/i,
      simple_fact: /^(what is|who is|where is|tìm|kiếm)/i,
      
      // Medium complexity → balanced
      explanation: /(explain|giải thích|tại sao|vì sao)/i,
      comparison: /(compare|so sánh|khác nhau|difference)/i,
      
      // High complexity → premium models
      reasoning: /(analyze|analyse|phân tích|reasoning)/i,
      code_generation: /(write code|generate|viết code|tạo code)/i,
      creative: /(write story|create|viết|story)/i
    };
  }

  classifyRequest(message) {
    const lowerMsg = message.toLowerCase();
    
    // Check for complexity indicators
    const wordCount = message.split(/\s+/).length;
    const hasCodeBlocks = /``[\s\S]*?``/.test(message);
    const hasMathSymbols = /[∑∏∫√π∞≤≥±×÷]/.test(message);
    
    return {
      complexity: wordCount > 100 || hasMathSymbols ? 'high' 
                 : wordCount > 30 || hasCodeBlocks ? 'medium' 
                 : 'low',
      estimatedTokens: wordCount * 1.3, // Token estimation
      requiresCode: hasCodeBlocks,
      requiresReasoning: this.taskPatterns.reasoning.test(lowerMsg)
    };
  }

  async routeAndExecute(messages) {
    const lastMessage = messages[messages.length - 1]?.content || '';
    const classification = this.classifyRequest(lastMessage);
    
    // Model selection logic
    let model, temperature, maxTokens;
    
    switch (classification.complexity) {
      case 'high':
        if (classification.requiresCode) {
          model = 'gpt-4';           // GPT-4.1: $8/MTok
          temperature = 0.2;
        } else {
          model = 'claude';          // Claude Sonnet 4.5: $15/MTok
          temperature = 0.3;
        }
        maxTokens = 8192;
        break;
        
      case 'medium':
        model = 'gemini';            // Gemini 2.5 Flash: $2.50/MTok
        temperature = 0.5;
        maxTokens = 4096;
        break;
        
      case 'low':
      default:
        model = 'deepseek';          // DeepSeek V3.2: $0.42/MTok
        temperature = 0.7;
        maxTokens = 2048;
    }

    const startTime = Date.now();
    const response = await this.client.chat(messages, {
      model,
      temperature,
      maxTokens
    });

    // Log for cost analysis
    await this.logCost({
      model,
      classification,
      latency: Date.now() - startTime,
      cost: response._meta.cost_usd
    });

    return response;
  }

  async logCost(data) {
    // Send to analytics (CloudWatch/Datadog)
    console.log(JSON.stringify({
      event: 'api_cost',
      ...data,
      timestamp: Date.now()
    }));
  }
}

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

1. Lỗi Timeout khi gọi API từ Edge Locations

// ❌ SAI: Không có timeout handling
const response = await fetch('https://api.holysheep.ai/v1/chat/completions', {
  method: 'POST',
  headers: { 'Authorization': Bearer ${API_KEY} },
  body: JSON.stringify(payload)
});

// ✅ ĐÚNG: Proper timeout với AbortController
async function callWithTimeout(url, options, timeoutMs = 45000) {
  const controller = new AbortController();
  const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
  
  try {
    const response = await fetch(url, {
      ...options,
      signal: controller.signal
    });
    clearTimeout(timeoutId);
    return response;
  } catch (error) {
    clearTimeout(timeoutId);
    if (error.name === 'AbortError') {
      throw new Error(Request timeout after ${timeoutMs}ms);
    }
    throw error;
  }
}

// Usage
const response = await callWithTimeout(
  'https://api.holysheep.ai/v1/chat/completions',
  {
    method: 'POST',
    headers: {
      'Authorization': Bearer ${process.env.HOLYSHEEP_API_KEY},
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(payload)
  },
  45000 // 45 second timeout
);

2. Lỗi CORS khi gọi từ Frontend

// ❌ SAI: Gọi trực tiếp từ browser, bị CORS block
const response = await fetch('https://api.holysheep.ai/v1/chat/completions', {...});

// ✅ ĐÚNG: Proxy qua backend/serverless function
// File: api/proxy.js (Next.js API Route)
export default async function handler(req, res) {
  if (req.method !== 'POST') {
    return res.status(405).json({ error: 'Method not allowed' });
  }

  // Validate request
  const { messages, model } = req.body;
  if (!messages || !Array.isArray(messages)) {
    return res.status(400).json({ error: 'Invalid request body' });
  }

  // Rate limiting check
  const clientIP = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
  if (await isRateLimited(clientIP)) {
    return res.status(429).json({ error: 'Rate limit exceeded' });
  }

  try {
    const response = await fetch('https://api.holysheep.ai/v1/chat/completions', {
      method: 'POST',
      headers: {
        'Authorization': Bearer ${process.env.HOLYSHEEP_API_KEY},
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        model: model || 'gemini-2.5-flash',
        messages,
        temperature: 0.7,
        max_tokens: 4096
      })
    });

    const data = await response.json();
    
    // Add headers for client caching
    res.setHeader('Cache-Control', 'no-store');
    res.setHeader('X-Response-Time', Date.now() - req.startTime);
    
    return res.status(200).json(data);
  } catch (error) {
    return res.status(500).json({ 
      error: 'Internal server error',
      message: error.message 
    });
  }
}

3. Lỗi Token Limit và Context Overflow

// ❌ SAI: Không kiểm tra token limit, gây overflow
const response = await client.chat([
  { role: 'user', content: veryLongHistory + newMessage }
], { model: 'gpt-4' });

// ✅ ĐÚNG: Smart context truncation
class ContextManager {
  static MAX_TOKENS = {
    'gpt-4.1': 128000,
    'claude-sonnet-4.5': 200000,
    'gemini-2.5-flash': 1048576,
    'deepseek-v3.2': 64000
  };

  static MAX_RESPONSE_TOKENS = 4096;

  static async truncateContext(messages, model) {
    const maxTokens = this.MAX_TOKENS[model] || 32000;
    const availableForContext = maxTokens - this.MAX_RESPONSE_TOKENS;
    
    let totalTokens = 0;
    const truncatedMessages = [];

    // Process messages from newest to oldest
    for (let i = messages.length - 1; i >= 0; i--) {
      const msg = messages[i];
      const estimatedTokens = Math.ceil(msg.content.length / 4); // Rough estimate
      
      if (totalTokens + estimatedTokens > availableForContext) {
        // Keep system message at all costs
        if (msg.role === 'system') {
          truncatedMessages.unshift({
            ...msg,
            content: msg.content.substring(0, (availableForContext - totalTokens) * 4)
          });
        }
        break;
      }
      
      truncatedMessages.unshift(msg);
      totalTokens += estimatedTokens;
    }

    // Ensure system message exists
    if (!truncatedMessages.find(m => m.role === 'system')) {
      truncatedMessages.unshift({
        role: 'system',
        content: 'You are a helpful assistant. Keep responses concise.'
      });
    }

    return truncatedMessages;
  }
}

// Usage in request handler
const truncatedMessages = await ContextManager.truncateContext(
  fullConversationHistory,
  'deepseek-v3.2'  // Model with smallest context
);

const response = await client.chat(truncatedMessages, { 
  model: 'deepseek' 
});

4. Lỗi Authentication Key Exposure

// ❌ NGUY HIỂM: Hardcode API key trong code
const API_KEY = 'sk-xxxxxxxxxxxx'; // KHÔNG BAO GIỜ LÀM THẾ NÀY

// ✅ ĐÚNG: Sử dụng environment variables hoặc secret manager
// Method 1: Environment variable
// Set in .env file (never commit this file!)
// HOLYSHEEP_API_KEY=sk-xxxxxxxxxxxx

// Method 2: Cloudflare Workers secrets
// wrangler secret put HOLYSHEEP_API_KEY

// Method 3: AWS Secrets Manager
async function getApiKey() {
  const client = new AWS.SecretsManager({ region: 'ap-southeast-1' });
  const secret = await client.getSecretValue({
    SecretId: 'prod/holysheep-api-key'
  }).promise();
  return JSON.parse(secret.SecretString).apiKey;
}

// Method 4: Rotate keys periodically
class KeyManager {
  constructor() {
    this.currentKey = null;
    this.rotating = false;
  }

  async getKey() {
    if (!this.currentKey && !this.rotating) {
      await this.rotateKey();
    }
    return this.currentKey;
  }

  async rotateKey() {
    this.rotating = true;
    try {
      // Call HolySheep API to generate new key
      const response = await fetch('https://api.holysheep.ai/v1/api-keys/rotate', {
        method: 'POST',
        headers: {
          'Authorization': Bearer ${this.currentKey},
          'Content-Type': 'application/json'
        }
      });
      
      if (response.ok) {
        const data = await response.json();
        this.currentKey = data.newKey;
        // Update secret manager
        await this.updateSecretManager(data.newKey);
      }
    } finally {
      this.rotating = false;
    }
  }
}

Monitoring và Observability

Để đảm bảo hệ thống hoạt động ổn định, tôi sử dụng dashboard với các metrics quan trọng:

// monitoring-dashboard.js - Real-time metrics collector
class MetricsCollector {
  constructor() {
    this.metrics = {
      requests: { count: 0, errors: 0 },
      latency: [],
      costs: { total: 0, byModel: {} }
    };
  }

  recordRequest(data) {
    const { model, latency, cost, success, errorType } = data;
    
    // Count metrics
    this.metrics.requests.count++;
    if (!success) this.metrics.requests.errors++;
    
    // Latency histogram
    this.metrics.latency.push(latency);
    if (this.metrics.latency.length > 1000) {
      this.metrics.latency.shift();
    }
    
    // Cost tracking
    this.metrics.costs.total += cost;
    this.metrics.costs.byModel[model] = 
      (this.metrics.costs.byModel[model] || 0) + cost;
  }

  getStats() {
    const sorted = [...this.metrics.latency].sort((a, b) => a - b);
    
    return {
      totalRequests: this.metrics.requests.count,
      errorRate: (this.metrics.requests.errors / this.metrics.requests.count * 100).toFixed(2) + '%',
      latency: {
        p50: sorted[Math.floor(sorted.length * 0.5)] || 0,
        p95: sorted[Math.floor(sorted.length * 0.95)] || 0,
        p99: sorted[Math.floor(sorted.length * 0.99)] || 0,
        avg: (sorted.reduce((a, b) => a + b, 0) / sorted.length).toFixed(2) || 0
      },
      costs: {
        total: this.metrics.costs.total.toFixed(4),
        byModel: this.metrics.costs.byModel
      }
    };
  }

  // Send to monitoring service (Datadog/NewRelic/Grafana)
  async flush() {
    const stats = this.getStats();
    
    // Reset counters
    this.metrics.requests.count = 0;
    this.metrics.requests.errors = 0;
    this.metrics.costs.total = 0;
    this.metrics.latency = [];
    
    // Send to Datadog
    await fetch('https://api.datadoghq.com/api/v1/series', {
      method: 'POST',
      headers: {
        'DD-API-KEY': process.env.DD_API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        series: [{
          metric: 'holy_sheep.api.stats',
          points: [[Date.now() / 1000, stats.totalRequests]],
          tags: ['env:production', 'provider:holysheep']
        }, {
          metric: 'holy_sheep.latency.p99',
          points: [[Date.now() / 1000, stats.latency.p99]],
          tags: ['env:production']
        }, {
          metric: 'holy_sheep.cost.total',
          points: