Tối hôm qua, hệ thống production của mình bị sập 3 lần trong 2 tiếng. Mỗi lần, log đều hiển thị ConnectionError: timeout after 30s. Sau 4 tiếng debug mất ăn tối, mình đã tìm ra nguyên nhân gốc và xây dựng một bộ công cụ debug hoàn chỉnh. Bài viết này sẽ chia sẻ toàn bộ kinh nghiệm thực chiến để bạn không phải đi con đường vòng như mình.

🎯 Kịch Bản Lỗi Thực Tế

Đây là log thực tế từ hệ thống của mình trước khi fix:

=== HOLYSHEEP API DEBUG REPORT ===
Timestamp: 2026-01-15 14:23:45.123
Endpoint: https://api.holysheep.ai/v1/chat/completions
Method: POST

[REQUEST]
Headers:
  Authorization: Bearer sk-***redacted***
  Content-Type: application/json
  User-Agent: holy-debug/1.0

Body Size: 523 bytes
Model: gpt-4.1

[RESPONSE]
Status Code: 401 Unauthorized
Response Time: 12ms
Headers:
  X-Request-Id: hs-7f8a9b2c
  X-RateLimit-Remaining: 0
  X-Error-Code: INVALID_API_KEY

[ERROR STACKTRACE]
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

Retries attempted: 3
Total time spent: 89.4 seconds
=== END REPORT ===

Nhìn vào log này, bạn có thể thấy 3 vấn đề cùng lúc: 401 Unauthorized (API key không hợp lệ), certificate verify failed (SSL), và retry không hoạt động đúng. Đây là những lỗi kinh điển mà mình sẽ hướng dẫn bạn debug từng cái.

🔍 Bước 1: Cài Đặt Môi Trường Debug

Trước tiên, hãy tạo một script debug toàn diện. Mình khuyên dùng httpx thay vì requests vì nó hỗ trợ async và có better error messages.

# requirements.txt
httpx==0.27.0
python-dotenv==1.0.0
loguru==0.7.2
respx==0.21.1  # cho mock testing

Cài đặt:

pip install httpx python-dotenv loguru respx
"""
HolySheep AI API Debug Client
Một công cụ debug toàn diện cho việc gọi API AI
"""

import httpx
import json
import time
import asyncio
from typing import Optional, Dict, Any
from loguru import logger
from datetime import datetime

Cấu hình logging

logger.add( "debug_{time}.log", rotation="10 MB", retention="7 days", level="DEBUG", format="{time:YYYY-MM-DD HH:mm:ss.SSS} | {level} | {message}" ) class HolySheepDebugClient: """Client debug cho HolySheep AI API""" BASE_URL = "https://api.holysheep.ai/v1" DEFAULT_TIMEOUT = 30.0 # 30 giây timeout MAX_RETRIES = 3 def __init__(self, api_key: str): if not api_key or api_key == "YOUR_HOLYSHEEP_API_KEY": raise ValueError("API key không được để trống!") self.api_key = api_key self.client = httpx.Client( base_url=self.BASE_URL, timeout=httpx.Timeout(DEFAULT_TIMEOUT), follow_redirects=True, http2=True # Hỗ trợ HTTP/2 cho hiệu suất tốt hơn ) def debug_request( self, model: str, messages: list, temperature: float = 0.7, max_tokens: int = 1000 ) -> Dict[str, Any]: """ Gửi request với full debugging information """ start_time = time.perf_counter() request_id = f"hs-{datetime.now().strftime('%Y%m%d%H%M%S')}" headers = { "Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json", "X-Request-ID": request_id, "User-Agent": "HolySheep-Debug-Client/1.0" } payload = { "model": model, "messages": messages, "temperature": temperature, "max_tokens": max_tokens } logger.info(f"=== REQUEST START ===") logger.info(f"Request ID: {request_id}") logger.info(f"Model: {model}") logger.info(f"Payload: {json.dumps(payload, indent=2, ensure_ascii=False)}") last_error = None for attempt in range(1, self.MAX_RETRIES + 1): try: logger.info(f"Attempt {attempt}/{self.MAX_RETRIES}") response = self.client.post( "/chat/completions", json=payload, headers=headers ) elapsed = time.perf_counter() - start_time # Log response details logger.info(f"=== RESPONSE RECEIVED ===") logger.info(f"Status: {response.status_code}") logger.info(f"Response Time: {elapsed*1000:.2f}ms") logger.info(f"Headers: {dict(response.headers)}") if response.status_code == 200: data = response.json() logger.info(f"Response Data: {json.dumps(data, indent=2, ensure_ascii=False)}") return { "success": True, "data": data, "response_time_ms": elapsed * 1000, "attempt": attempt } else: error_data = response.json() if response.content else {} logger.warning(f"Error Response: {error_data}") last_error = f"HTTP {response.status_code}: {error_data}" # Retry cho các lỗi có thể phục hồi if response.status_code in [429, 500, 502, 503, 504]: wait_time = 2 ** attempt # Exponential backoff logger.info(f"Retrying in {wait_time}s...") time.sleep(wait_time) continue else: break # Không retry cho 401, 400, etc. except httpx.TimeoutException as e: elapsed = time.perf_counter() - start_time logger.error(f"Timeout sau {elapsed*1000:.2f}ms: {e}") last_error = f"Timeout: {e}" continue except httpx.ConnectError as e: elapsed = time.perf_counter() - start_time logger.error(f"Connection Error sau {elapsed*1000:.2f}ms: {e}") last_error = f"ConnectionError: {e}" continue except httpx.SSLError as e: elapsed = time.perf_counter() - start_time logger.error(f"SSL Error: {e}") last_error = f"SSLError: {e}" break except Exception as e: