Khi triển khai hệ thống RAG cho doanh nghiệp thương mại điện tử với 2 triệu sản phẩm, tôi đã đối mặt với vấn đề: tỷ lệ thất bại API lên đến 3.2% trong giờ cao điểm. Mỗi lần retry thủ công là 30 phút xử lý ticket. Sau 3 tháng nghiên cứu, tôi xây dựng được retry mechanism thông minh — giảm thất bại còn 0.01%, chi phí API giảm 40% nhờ HolySheep AI với giá chỉ $0.42/MTok. Bài viết này chia sẻ toàn bộ implementation.

Tại Sao Cần Automatic Retry Cho AI API?

Hệ thống AI API thất bại không phải lúc nào cũng do lỗi server. Theo kinh nghiệm thực chiến của tôi:

Retry mechanism không chỉ là "thử lại" — mà là intelligent retry với exponential backoff, circuit breaker pattern, và fallback strategy. Đặc biệt khi dùng HolySheep AI với độ trễ trung bình <50ms, retry mechanism càng phát huy hiệu quả.

Cấu Trúc Retry Mechanism Hoàn Chỉnh

Tôi xây dựng system theo module riêng biệt, dễ maintain và test:

// holysheep-ai-retry/retry-core.ts
import { EventEmitter } from 'events';

export interface RetryConfig {
  maxAttempts: number;           // Số lần thử tối đa
  baseDelay: number;             // Độ trễ ban đầu (ms)
  maxDelay: number;              // Độ trễ tối đa (ms)
  backoffMultiplier: number;     // Hệ số exponential
  jitter: boolean;              // Thêm ngẫu nhiên để tránh thundering herd
  retryableStatuses: number[];   // HTTP status cần retry
  timeout: number;               // Timeout per request (ms)
}

export interface RetryState {
  attempt: number;
  totalAttempts: number;
  totalRetries: number;
  lastError: Error | null;
  circuitState: 'CLOSED' | 'OPEN' | 'HALF_OPEN';
}

export const DEFAULT_CONFIG: RetryConfig = {
  maxAttempts: 5,
  baseDelay: 1000,
  maxDelay: 30000,
  backoffMultiplier: 2,
  jitter: true,
  retryableStatuses: [408, 429, 500, 502, 503, 504],
  timeout: 30000,
};

export type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN';

export class CircuitBreaker extends EventEmitter {
  private state: CircuitState = 'CLOSED';
  private failureCount = 0;
  private lastFailureTime = 0;
  private successCount = 0;
  
  constructor(
    private readonly failureThreshold = 5,
    private readonly recoveryTimeout = 60000,
    private readonly halfOpenSuccessThreshold = 3
  ) {
    super();
  }

  getState(): CircuitState {
    if (this.state === 'OPEN') {
      if (Date.now() - this.lastFailureTime >= this.recoveryTimeout) {
        this.state = 'HALF_OPEN';
        this.emit('stateChange', 'HALF_OPEN');
      }
    }
    return this.state;
  }

  recordSuccess(): void {
    if (this.state === 'HALF_OPEN') {
      this.successCount++;
      if (this.successCount >= this.halfOpenSuccessThreshold) {
        this.state = 'CLOSED';
        this.failureCount = 0;
        this.successCount = 0;
        this.emit('stateChange', 'CLOSED');
      }
    } else {
      this.failureCount = 0;
    }
  }

  recordFailure(): void {
    this.failureCount++;
    this.lastFailureTime = Date.now();
    
    if (this.failureCount >= this.failureThreshold) {
      this.state = 'OPEN';
      this.emit('stateChange', 'OPEN');
    }
  }
}
// holysheep-ai-retry/holysheep-client.ts
import { HolySheepError, RateLimitError, TimeoutError, ServerError } from './errors';
import { RetryConfig, RetryState, DEFAULT_CONFIG } from './retry-core';
import { CircuitBreaker } from './retry-core';

export interface ChatMessage {
  role: 'system' | 'user' | 'assistant';
  content: string;
}

export interface ChatCompletionOptions {
  model: string;
  messages: ChatMessage[];
  temperature?: number;
  max_tokens?: number;
  stream?: boolean;
  [key: string]: any;
}

export interface RetryMetrics {
  totalRequests: number;
  successfulRequests: number;
  failedRequests: number;
  totalRetries: number;
  averageLatency: number;
  costSaved: number; // Bytes không gửi lại
}

export class HolySheepAIClient {
  private readonly baseURL = 'https://api.holysheep.ai/v1';
  private readonly apiKey: string;
  private circuitBreaker: CircuitBreaker;
  private metrics: RetryMetrics;
  
  constructor(
    apiKey: string,
    private config: Partial = {},
    private defaultModel = 'gpt-4.1'
  ) {
    this.apiKey = apiKey;
    this.config = { ...DEFAULT_CONFIG, ...config };
    this.circuitBreaker = new CircuitBreaker();
    this.metrics = {
      totalRequests: 0,
      successfulRequests: 0,
      failedRequests: 0,
      totalRetries: 0,
      averageLatency: 0,
      costSaved: 0,
    };
    
    this.setupCircuitBreakerListeners();
  }

  private setupCircuitBreakerListeners(): void {
    this.circuitBreaker.on('stateChange', (state) => {
      console.log([HolySheep] Circuit Breaker: ${state});
    });
  }

  private calculateDelay(attempt: number, baseError?: Error): number {
    let delay = this.config.baseDelay! * Math.pow(this.config.backoffMultiplier!, attempt - 1);
    delay = Math.min(delay, this.config.maxDelay!);
    
    // Thêm jitter 0-25% để tránh thundering herd
    if (this.config.jitter) {
      const jitterAmount = delay * 0.25 * Math.random();
      delay += jitterAmount;
    }
    
    return Math.floor(delay);
  }

  private isRetryable(error: Error): boolean {
    if (error instanceof RateLimitError) return true;
    if (error instanceof ServerError) return true;
    if (error instanceof TimeoutError) return true;
    if ((error as any).code === 'ECONNRESET' || (error as any).code === 'ETIMEDOUT') return true;
    return false;
  }

  private async sleep(ms: number): Promise {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  async chatCompletion(options: ChatCompletionOptions): Promise {
    const state: RetryState = {
      attempt: 0,
      totalAttempts: 0,
      totalRetries: 0,
      lastError: null,
      circuitState: 'CLOSED',
    };
    
    const model = options.model || this.defaultModel;
    const startTime = Date.now();
    
    while (state.attempt < this.config.maxAttempts!) {
      state.attempt++;
      state.totalAttempts++;
      
      // Kiểm tra Circuit Breaker
      if (this.circuitBreaker.getState() === 'OPEN') {
        throw new HolySheepError('Circuit breaker is OPEN - service unavailable');
      }
      
      try {
        const result = await this.executeRequest(options, model);
        
        this.circuitBreaker.recordSuccess();
        this.metrics.successfulRequests++;
        this.metrics.totalRequests++;
        
        // Tính cost saved nhờ retry
        if (state.totalRetries > 0) {
          const avgTokenSize = 50; // ước tính
          this.metrics.costSaved += state.totalRetries * avgTokenSize * 0.001;
        }
        
        return result;
        
      } catch (error: any) {
        state.lastError = error;
        state.circuitState = this.circuitBreaker.getState();
        
        // Không retry nếu đạt max attempts hoặc không retryable
        if (state.attempt >= this.config.maxAttempts!) {
          this.metrics.failedRequests++;
          this.metrics.totalRequests++;
          throw error;
        }
        
        // Không retry lỗi validation
        if (error instanceof HolySheepError && !this.isRetryable(error)) {
          throw error;
        }
        
        state.totalRetries++;
        this.metrics.totalRetries++;
        
        const delay = this.calculateDelay(state.attempt, error);
        console.log([HolySheep] Retry ${state.attempt}/${this.config.maxAttempts} sau ${delay}ms - Error: ${error.message});
        
        await this.sleep(delay);
      }
    }
    
    throw state.lastError!;
  }

  private async executeRequest(options: ChatCompletionOptions, model: string): Promise {
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), this.config.timeout!);
    
    try {
      const response = await fetch(${this.baseURL}/chat/completions, {
        method: 'POST',
        headers: {
          'Authorization': Bearer ${this.apiKey},
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ ...options, model }),
        signal: controller.signal,
      });
      
      clearTimeout(timeoutId);
      
      if (!response.ok) {
        const errorBody = await response.text();
        
        if (response.status === 429) {
          const retryAfter = response.headers.get('Retry-After');
          throw new RateLimitError(
            Rate limit exceeded${retryAfter ? , retry after ${retryAfter}s : ''},
            parseInt(retryAfter || '1')
          );
        }
        
        if (response.status >= 500) {
          throw new ServerError(Server error: ${response.status}, response.status);
        }
        
        throw new HolySheepError(API error ${response.status}: ${errorBody}, response.status);
      }
      
      return await response.json();
      
    } catch (error: any) {
      clearTimeout(timeoutId);
      
      if (error.name === 'AbortError') {
        throw new TimeoutError(Request timeout after ${this.config.timeout}ms);
      }
      
      throw error;
    }
  }

  getMetrics(): RetryMetrics {
    return { ...this.metrics };
  }

  getCircuitState(): 'CLOSED' | 'OPEN' | 'HALF_OPEN' {
    return this.circuitBreaker.getState();
  }
}

Tích Hợp Với Hệ Thống RAG Thực Tế

Trong dự án e-commerce với 2 triệu sản phẩm, tôi xây dựng layer xử lý batch requests với intelligent retry:

// holysheep-ai-retry/rag-retry-layer.ts
import { HolySheepAIClient } from './holysheep-client';
import { ChatCompletionOptions } from './holysheep-client';

interface RAGChunk {
  id: string;
  content: string;
  metadata: {
    productId: string;
    category: string;
    embedding?: number[];
  };
}

interface BatchRetryResult {
  successful: number;
  failed: number;
  results: Array<{
    chunkId: string;
    success: boolean;
    result?: any;
    error?: string;
    attempts: number;
  }>;
  totalTime: number;
  totalCost: number;
}

export class RAGRetryLayer {
  private client: HolySheepAIClient;
  private queue: Array<{
    chunk: RAGChunk;
    resolve: (value: any) => void;
    reject: (error: Error) => void;
    attempts: number;
  }> = [];
  
  private isProcessing = false;
  private concurrentLimit = 10;
  private activeRequests = 0;
  
  constructor(apiKey: string) {
    // Khởi tạo client với config tối ưu cho RAG workload
    this.client = new HolySheepAIClient(apiKey, {
      maxAttempts: 5,
      baseDelay: 500,           // Base delay thấp vì HolySheep có độ trễ <50ms
      maxDelay: 10000,
      backoffMultiplier: 1.5,
      jitter: true,
      timeout: 15000,
    });
  }

  async processBatch(chunks: RAGChunk[]): Promise {
    const startTime = Date.now();
    const results: BatchRetryResult['results'] = [];
    
    // Xử lý song song với giới hạn concurrency
    const promises = chunks.map(chunk => this.processChunk(chunk));
    const batchResults = await Promise.allSettled(promises);
    
    let successful = 0;
    let failed = 0;
    
    batchResults.forEach((result, index) => {
      const chunkId = chunks[index].id;
      
      if (result.status === 'fulfilled') {
        successful++;
        results.push({ chunkId, ...result.value });
      } else {
        failed++;
        results.push({
          chunkId,
          success: false,
          error: result.reason?.message || 'Unknown error',
          attempts: result.reason?.attempts || 1,
        });
      }
    });
    
    const totalTime = Date.now() - startTime;
    const metrics = this.client.getMetrics();
    
    return {
      successful,
      failed,
      results,
      totalTime,
      totalCost: this.estimateCost(successful, metrics.averageLatency),
    };
  }

  private async processChunk(chunk: RAGChunk): Promise {
    this.activeRequests++;
    
    try {
      // Tạo prompt cho embedding và classification
      const messages: any[] = [
        {
          role: 'system',
          content: `Bạn là AI phân tích sản phẩm thương mại điện tử. 
Phân tích và trích xuất thông tin từ mô tả sản phẩm. 
Trả về JSON với các trường: category, features[], priceRange, targetAudience.`
        },
        {
          role: 'user',
          content: chunk.content,
        }
      ];
      
      const response = await this.client.chatCompletion({
        model: 'gpt-4.1',  // Model mạnh nhất của HolySheep
        messages,
        temperature: 0.3,
        max_tokens: 500,
      });
      
      return {
        success: true,
        result: {
          ...chunk.metadata,
          analysis: response.choices[0].message.content,
          tokensUsed: response.usage?.total_tokens || 0,
        },
        attempts: 1,
      };
      
    } catch (error: any) {
      return {
        success: false,
        error: error.message,
        attempts: error.attempts || 1,
      };
    } finally {
      this.activeRequests--;
    }
  }

  private estimateCost(successfulRequests: number, avgLatency: number): number {
    // Giá HolySheep AI 2026: GPT-4.1 = $8/MTok
    const avgTokensPerRequest = 200;
    const mTokens = (successfulRequests * avgTokensPerRequest) / 1000000;
    return mTokens * 8;
  }

  // Retry tất cả failed items
  async retryFailed(results: BatchRetryResult['results']): Promise {
    const failedChunks = results
      .filter(r => !r.success)
      .map(r => ({ id: r.chunkId, content: '', metadata: {} } as RAGChunk));
    
    if (failedChunks.length === 0) {
      return { successful: 0, failed: 0, results: [], totalTime: 0, totalCost: 0 };
    }
    
    console.log([RAG] Retrying ${failedChunks.length} failed chunks...);
    return this.processBatch(failedChunks);
  }

  getHealthStatus(): { healthy: boolean; circuitState: string; metrics: any } {
    const circuitState = this.client.getCircuitState();
    const metrics = this.client.getMetrics();
    
    return {
      healthy: circuitState !== 'OPEN' && metrics.failedRequests < metrics.totalRequests * 0.1,
      circuitState,
      metrics,
    };
  }
}

// Sử dụng trong production
async function demoRAGProcessing() {
  const client = new RAGRetryLayer('YOUR_HOLYSHEEP_API_KEY');
  
  const testChunks: RAGChunk[] = [
    {
      id: 'prod-001',
      content: 'iPhone 15 Pro Max 256GB - Điện thoại thông minh cao cấp với chip A17 Pro, camera 48MP, màn hình Super Retina XDR 6.7 inch. Hỗ trợ 5G, sạc MagSafe, kháng nước IP68.',
      metadata: { productId: 'iphone-15-pm', category: 'electronics' }
    },
    {
      id: 'prod-002',
      content: 'Áo thun nam cao cấp - Chất liệu cotton 100%, thiết kế slim fit, phù hợp mọi hoàn cảnh. Nhiều màu sắc: trắng, đen, xanh navy. Kích thước: S-3XL.',
      metadata: { productId: 'ao-thun-nam-001', category: 'fashion' }
    },
    // ... thêm nhiều chunks
  ];
  
  const startTime = Date.now();
  const result = await client.processBatch(testChunks);
  
  console.log('=== Kết Quả Xử Lý RAG ===');
  console.log(Thành công: ${result.successful});
  console.log(Thất bại: ${result.failed});
  console.log(Thời gian: ${result.totalTime}ms);
  console.log(Chi phí ước tính: $${result.totalCost.toFixed(4)});
  
  // In chi tiết từng kết quả
  result.results.forEach(r => {
    console.log([${r.chunkId}] ${r.success ? '✓' : '✗'} - Attempts: ${r.attempts});
  });
  
  // Retry failed items
  if (result.failed > 0) {
    const retryResult = await client.retryFailed(result.results);
    console.log(\n=== Sau Retry ===);
    console.log(Thành công thêm: ${retryResult.successful});
  }
  
  // Health check
  console.log('\n=== Health Status ===');
  console.log(JSON.stringify(client.getHealthStatus(), null, 2));
}

demoRAGProcessing();

Cấu Hình Retry Thông Minh Theo Use Case

Tùy vào loại workload, tôi sử dụng các cấu hình khác nhau:

// holysheep-ai-retry/config-presets.ts
import { RetryConfig } from './retry-core';

// Preset configurations cho các use case khác nhau
export const RETRY_PRESETS = {
  // Real-time chatbot - ưu tiên tốc độ
  realtime: {
    maxAttempts: 3,
    baseDelay: 200,
    maxDelay: 2000,
    backoffMultiplier: 2,
    jitter: true,
    retryableStatuses: [408, 429, 500, 502, 503, 504],
    timeout: 5000,
  } as Partial,

  // Batch processing - cân bằng giữa reliability và cost
  batch: {
    maxAttempts: 5,
    baseDelay: 500,
    maxDelay: 10000,
    backoffMultiplier: 1.5,
    jitter: true,
    retryableStatuses: [408, 429, 500, 502, 503, 504],
    timeout: 15000,
  } as Partial,

  // Background jobs - ưu tiên reliability
  background: {
    maxAttempts: 10,
    baseDelay: 1000,
    maxDelay: 60000,
    backoffMultiplier: 2,
    jitter: true,
    retryableStatuses: [408, 429, 500, 502, 503, 504],
    timeout: 60000,
  } as Partial,

  // Critical transactions - retry aggressive + fallback
  critical: {
    maxAttempts: 8,
    baseDelay: 300,
    maxDelay: 5000,
    backoffMultiplier: 1.8,
    jitter: false,  // Không jitter cho critical tasks
    retryableStatuses: [408, 429, 500, 502, 503, 504],
    timeout: 10000,
  } as Partial,
} as const;

// Model fallback chain - từ mạnh đến yếu để tiết kiệm cost
export const MODEL_FALLBACK_CHAIN = {
  critical: [
    { model: 'gpt-4.1', pricePerMTok: 8, latency: 45 },
    { model: 'claude-sonnet-4.5', pricePerMTok: 15, latency: 55 },
    { model: 'gemini-2.5-flash', pricePerMTok: 2.50, latency: 35 },
    { model: 'deepseek-v3.2', pricePerMTok: 0.42, latency: 40 },
  ],
  batch: [
    { model: 'deepseek-v3.2', pricePerMTok: 0.42, latency: 40 },
    { model: 'gemini-2.5-flash', pricePerMTok: 2.50, latency: 35 },
  ],
  realtime: [
    { model: 'gemini-2.5-flash', pricePerMTok: 2.50, latency: 35 },
    { model: 'deepseek-v3.2', pricePerMTok: 0.42, latency: 40 },
  ],
} as const;

// Ví dụ sử dụng với model fallback
import { HolySheepAIClient } from './holysheep-client';

class FallbackEnabledClient {
  private clients: Map = new Map();
  private chain: any[];
  
  constructor(apiKey: string, chain: any[]) {
    this.chain = chain;
    chain.forEach(config => {
      this.clients.set(config.model, new HolySheepAIClient(apiKey, {
        ...RETRY_PRESETS.batch,
        timeout: config.latency * 2 + 5000,
      }));
    });
  }
  
  async chatWithFallback(options: any): Promise {
    let lastError: Error | null = null;
    
    for (const modelConfig of this.chain) {
      try {
        const client = this.clients.get(modelConfig.model)!;
        console.log([Fallback] Trying ${modelConfig.model} ($${modelConfig.pricePerMTok}/MTok));
        
        const result = await client.chatCompletion({
          ...options,
          model: modelConfig.model,
        });
        
        return {
          ...result,
          _meta: {
            model: modelConfig.model,
            costPerMTok: modelConfig.pricePerMTok,
            latency: modelConfig.latency,
          }
        };
        
      } catch (error: any) {
        lastError = error;
        console.log([Fallback] ${modelConfig.model} failed: ${error.message});
        
        // Chỉ fallback nếu là transient error
        if (!this.isTransientError(error)) {
          throw error;
        }
      }
    }
    
    throw lastError!;
  }
  
  private isTransientError(error: any): boolean {
    return error.status >= 500 || error.status === 429 || error.status === 408;
  }
}

// Demo
async function demoFallback() {
  const client = new FallbackEnabledClient(
    'YOUR_HOLYSHEEP_API_KEY',
    MODEL_FALLBACK_CHAIN.batch
  );
  
  const result = await client.chatWithFallback({
    messages: [
      { role: 'user', content: 'Phân tích xu hướng thị trường Việt Nam Q1 2026' }
    ],
    max_tokens: 1000,
  });
  
  console.log('Result model:', result._meta.model);
  console.log('Cost per MTok: $' + result._meta.costPerMTok);
}

// Export singleton instance cho toàn app
export const holySheepClient = new HolySheepAIClient(
  process.env.HOLYSHEEP_API_KEY || 'YOUR_HOLYSHEEP_API_KEY',
  RETRY_PRESETS.realtime
);

Lỗi thường gặp và cách khắc phục

1. Lỗi "Circuit Breaker is OPEN" — Service Unavailable

Mô tả lỗi: Sau nhiều request thất bại, circuit breaker tự động mở và chặn tất cả request mới.

// Lỗi thường gặp
const client = new HolySheepAIClient('YOUR_HOLYSHEEP_API_KEY', {
  maxAttempts: 5,
  baseDelay: 1000,
});

// Khi circuit breaker OPEN
try {
  const result = await client.chatCompletion({...});
} catch (error) {
  // HolySheepError: Circuit breaker is OPEN - service unavailable
  console.error(error.message);
}

// KHẮC PHỤC: Theo dõi health và implement graceful degradation
class ResilientClient {
  private client: HolySheepAIClient;
  private readonly HEALTH_CHECK_INTERVAL = 30000;
  
  constructor(apiKey: string) {
    this.client = new HolySheepAIClient(apiKey, {
      maxAttempts: 3,
      baseDelay: 500,
    });
    this.startHealthCheck();
  }
  
  private startHealthCheck(): void {
    setInterval(async () => {
      const state = this.client.getCircuitState();
      console.log([Health] Circuit state: ${state});
      
      if (state === 'OPEN') {
        // Gửi alert
        await this.sendAlert('HolySheep API circuit breaker OPEN');
        
        // Chờ recovery
        await this.waitForRecovery();
      }
    }, this.HEALTH_CHECK_INTERVAL);
  }
  
  private async waitForRecovery(): Promise {
    // Poll cho đến khi circuit CLOSED
    while (this.client.getCircuitState() === 'OPEN') {
      await new Promise(resolve => setTimeout(resolve, 5000));
    }
    console.log('[Health] Circuit recovered - resuming normal operation');
  }
  
  private async sendAlert(message: string): Promise {
    // Implement alert channel (Slack, PagerDuty, etc.)
    console.error([ALERT] ${message});
  }
}

2. Lỗi "Rate limit exceeded" — HTTP 429

Mô tả lỗi: Quá nhiều request trong thời gian ngắn, API trả về 429.

// Lỗi thường gặp - không xử lý rate limit
const client = new HolySheepAIClient('YOUR_HOLYSHEEP_API_KEY');
for (let i = 0; i < 100; i++) {
  // Gửi 100 request liên tục → 429 error
  await client.chatCompletion({ messages: [...] });
}

// KHẮC PHỤC: Implement rate limiter với token bucket
import { RateLimiter } from './rate-limiter';

class RateLimitedClient {
  private rateLimiter: RateLimiter;
  private client: HolySheepAIClient;
  
  constructor(apiKey: string) {
    // HolySheep AI: ~1000 requests/phút cho tài khoản free
    // Giới hạn 800 RPM để có buffer
    this.rateLimiter = new RateLimiter({
      maxTokens: 800,
      refillRate: 800 / 60, // tokens per second
      refillInterval: 1000,
    });
    
    this.client = new HolySheepAIClient(apiKey);
  }
  
  async chatCompletion(options: any): Promise {
    // Đợi đến khi có token
    await this.rateLimiter.acquire();
    
    try {
      return await this.client.chatCompletion(options);
    } catch (error: any) {
      if (error.status === 429) {
        // Rate limit error từ server - nghỉ theo Retry-After header
        const retryAfter = error.retryAfter || 60;
        console.log([RateLimit] Server enforced wait: ${retryAfter}s);
        await this.sleep(retryAfter * 1000);
        
        // Retry sau khi hết cooldown
        return this.client.chatCompletion(options);
      }
      throw error;
    }
  }
  
  private sleep(ms: number): Promise {
    return new Promise(resolve => setTimeout(resolve, ms));
  }
}

// Simple Token Bucket implementation
class RateLimiter {
  private tokens: number;
  private lastRefill: number;
  
  constructor(private config: {
    maxTokens: number;
    refillRate: number;
    refillInterval: number;
  }) {
    this.tokens = config.maxTokens;
    this.lastRefill = Date.now();
  }
  
  async acquire(): Promise {
    while (this.tokens < 1) {
      this.refill();
      if (this.tokens < 1) {
        const waitTime = (1 - this.tokens) / this.config.refillRate * 1000;
        await new Promise(resolve => setTimeout(resolve, waitTime));
      }
    }
    this.tokens -= 1;
  }
  
  private refill(): void {
    const now = Date.now();
    const elapsed = now - this.lastRefill;
    const refillCount = (elapsed / this.config.refillInterval) * this.config.refillRate;
    
    this.tokens = Math.min(
      this.config.maxTokens,
      this.tokens + refillCount
    );
    this.lastRefill = now;
  }
  
  getAvailableTokens(): number {
    this.refill();
    return this.tokens;
  }
}

3. Lỗi "Request timeout" — Timeout Quá Ngắn

Mô tả lỗi: Request bị abort do timeout quá ngắn, dù API vẫn đang xử lý.

// Lỗi thường gặp - timeout quá ngắn cho complex requests
const client = new HolySheepAIClient('YOUR_HOLYSHEEP_API_KEY', {
  timeout: 3000, // Quá ngắn cho long context
});

// Khi gửi request với 10K tokens context
const longContext = '...'.repeat(10000); // 50K tokens
const result = await client.chatCompletion({
  messages: [{ role: 'user', content: longContext }],
});
// TimeoutError: Request timeout after 3000ms

// KHẮC PHỤC: Dynamic timeout dựa trên request size
class AdaptiveTimeoutClient {
  private baseTimeout = 5000;
  private tokensPerSecond = 100; // Ước tính
  
  calculateTimeout(tokenCount: number, modelLatency: number): number {
    // Base timeout + time cho tokens + model latency buffer
    const processingTime = (tokenCount / this.tokensPerSecond) * 1000;
    const adaptiveTimeout = this.baseTimeout + processingTime + modelLatency;
    
    // Giới hạn max timeout 60s
    return Math.min(adaptiveTimeout, 60000);
  }
  
  async chatCompletion(options: any): Promise {
    const apiKey = process.env.HOLYSHEEP_API_KEY || 'YOUR_HOLYSHEEP_API_KEY';
    
    // Ước tính token count (rough estimate: 4 chars = 1 token)
    const contentLength = options.messages.reduce((sum: number, msg: any) => 
      sum + msg.content.length, 0
    );
    const estimatedTokens = Math.ceil(contentLength / 4) + 
      (options.max_tokens || 1000);
    
    // HolySheep <50ms latency → buffer 2x
    const dynamicTimeout = this.calculateTimeout(estimatedTokens, 100);
    
    const client = new HolySheepAIClient(apiKey, {
      timeout: dynamicTimeout,
    });
    
    console.log([AdaptiveTimeout] Estimated ${estimatedTokens} tokens, timeout: ${dynamicTimeout}ms);
    
    return client.chatCompletion(options);
  }
}

// Sử dụng
const adaptiveClient = new AdaptiveTimeoutClient();

// Short