Trong bài viết này, tôi sẽ chia sẻ kinh nghiệm thực chiến 3 năm triển khai CI/CD pipeline kết hợp AI agent, và lý do tại sao HolySheep AI trở thành lựa chọn tối ưu khi cần xử lý các tác vụ phức tạp với chi phí thấp nhất.

Bảng So Sánh: HolySheep vs API Chính Thức vs Dịch Vụ Relay

Tiêu chí HolySheep AI API Chính Thức Dịch Vụ Relay (API Mirror)
GPT-4.1 ($/MTok) $8.00 $15.00 $10-12
Claude Sonnet 4.5 ($/MTok) $15.00 $22.00 $18-20
Gemini 2.5 Flash ($/MTok) $2.50 $3.50 $3.00
DeepSeek V3.2 ($/MTok) $0.42 $1.20 $0.80
Độ trễ trung bình <50ms 100-300ms 80-200ms
Thanh toán WeChat/Alipay/USD Chỉ USD USD/Hybrid
Tín dụng miễn phí Không Không
Tiết kiệm vs chính thức 85%+ Baseline 30-50%

Multi-Model Task Decomposition: Tại Sao Cần Nhiều Model?

Trong thực tế triển khai, tôi nhận ra rằng không có model nào tối ưu cho mọi tác vụ. Một pipeline hoàn chỉnh cần:

Cấu Hình HolySheep Cline Agent

import requests
import json
from typing import List, Dict, Optional
from dataclasses import dataclass
from enum import Enum
import time

class ModelType(Enum):
    DEEPSEEK_V32 = "deepseek-v3.2"
    GEMINI_FLASH = "gemini-2.5-flash"
    CLAUDE_SONNET = "claude-sonnet-4.5"
    GPT_41 = "gpt-4.1"

@dataclass
class TaskResult:
    model_used: str
    response: str
    tokens_used: int
    latency_ms: float
    cost_usd: float
    success: bool
    error_message: Optional[str] = None

class HolySheepClineAgent:
    """
    HolySheep AI Multi-Model Agent cho tự động hóa phát triển
    Base URL: https://api.holysheep.ai/v1
    """
    
    BASE_URL = "https://api.holysheep.ai/v1"
    
    # Bảng giá thực tế 2026 (tiết kiệm 85%+ so với API chính thức)
    PRICING = {
        ModelType.DEEPSEEK_V32: 0.42,    # $/MTok
        ModelType.GEMINI_FLASH: 2.50,    # $/MTok
        ModelType.CLAUDE_SONNET: 15.00,  # $/MTok
        ModelType.GPT_41: 8.00,          # $/MTok
    }
    
    def __init__(self, api_key: str):
        self.api_key = api_key
        self.session = requests.Session()
        self.session.headers.update({
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        })
        self.total_cost = 0.0
        self.total_tokens = 0
        self.request_history: List[TaskResult] = []
    
    def chat_completion(
        self,
        model: ModelType,
        messages: List[Dict],
        temperature: float = 0.7,
        max_tokens: int = 4096
    ) -> TaskResult:
        """
        Gọi API với automatic retry và error handling
        """
        url = f"{self.BASE_URL}/chat/completions"
        payload = {
            "model": model.value,
            "messages": messages,
            "temperature": temperature,
            "max_tokens": max_tokens
        }
        
        max_retries = 3
        retry_delay = 1.0
        
        for attempt in range(max_retries):
            start_time = time.time()
            try:
                response = self.session.post(url, json=payload, timeout=30)
                latency_ms = (time.time() - start_time) * 1000
                
                if response.status_code == 200:
                    data = response.json()
                    usage = data.get("usage", {})
                    input_tokens = usage.get("prompt_tokens", 0)
                    output_tokens = usage.get("completion_tokens", 0)
                    total_tokens = input_tokens + output_tokens
                    
                    cost = (total_tokens / 1_000_000) * self.PRICING[model]
                    self.total_cost += cost
                    self.total_tokens += total_tokens
                    
                    result = TaskResult(
                        model_used=model.value,
                        response=data["choices"][0]["message"]["content"],
                        tokens_used=total_tokens,
                        latency_ms=latency_ms,
                        cost_usd=cost,
                        success=True
                    )
                    self.request_history.append(result)
                    return result
                    
                elif response.status_code == 429:
                    # Rate limit - retry với exponential backoff
                    wait_time = retry_delay * (2 ** attempt)
                    print(f"Rate limited. Waiting {wait_time}s before retry...")
                    time.sleep(wait_time)
                    continue
                    
                elif response.status_code == 500:
                    # Server error - retry
                    print(f"Server error. Retry {attempt + 1}/{max_retries}")
                    time.sleep(retry_delay)
                    continue
                    
                else:
                    return TaskResult(
                        model_used=model.value,
                        response="",
                        tokens_used=0,
                        latency_ms=latency_ms,
                        cost_usd=0,
                        success=False,
                        error_message=f"HTTP {response.status_code}: {response.text}"
                    )
                    
            except requests.exceptions.Timeout:
                print(f"Request timeout. Retry {attempt + 1}/{max_retries}")
                time.sleep(retry_delay)
                continue
                
            except Exception as e:
                return TaskResult(
                    model_used=model.value,
                    response="",
                    tokens_used=0,
                    latency_ms=0,
                    cost_usd=0,
                    success=False,
                    error_message=str(e)
                )
        
        return TaskResult(
            model_used=model.value,
            response="",
            tokens_used=0,
            latency_ms=0,
            cost_usd=0,
            success=False,
            error_message="Max retries exceeded"
        )

=== SỬ DỤNG MẪU ===

agent = HolySheepClineAgent(api_key="YOUR_HOLYSHEEP_API_KEY")

Task 1: Extract structured data (dùng DeepSeek - rẻ nhất)

result1 = agent.chat_completion( model=ModelType.DEEPSEEK_V32, messages=[{"role": "user", "content": "Extract all email addresses from: [email protected], [email protected], [email protected]"}] ) print(f"DeepSeek V3.2 - Latency: {result1.latency_ms:.2f}ms, Cost: ${result1.cost_usd:.6f}")

Task 2: Generate code (dùng Gemini Flash - nhanh và rẻ)

result2 = agent.chat_completion( model=ModelType.GEMINI_FLASH, messages=[{"role": "user", "content": "Write a Python function to validate email addresses"}] ) print(f"Gemini 2.5 Flash - Latency: {result2.latency_ms:.2f}ms, Cost: ${result2.cost_usd:.6f}")

In báo cáo chi phí dự án

print(f"\n=== PROJECT COST REPORT ===") print(f"Total Tokens: {agent.total_tokens:,}") print(f"Total Cost: ${agent.total_cost:.6f}") print(f"Requests: {len(agent.request_history)}")

Pipeline Task Decomposition: Từ Requirement Đến Deployment

import asyncio
from typing import List, Tuple
from collections import defaultdict

class TaskDecomposer:
    """
    Tự động phân tách task thành các subtask và chọn model tối ưu
    """
    
    # Phân loại task theo độ phức tạp và chọn model phù hợp
    TASK_MODEL_MAP = {
        "extract": ModelType.DEEPSEEK_V32,      # Structured extraction - rẻ nhất
        "summarize": ModelType.GEMINI_FLASH,    # Tóm tắt - nhanh
        "generate": ModelType.GEMINI_FLASH,     # Code generation - nhanh
        "analyze": ModelType.CLAUDE_SONNET,     # Phân tích phức tạp
        "review": ModelType.CLAUDE_SONNET,      # Code review
        "finalize": ModelType.GPT_41,           # Quality assurance
    }
    
    # Từ điển keywords để classify task
    TASK_KEYWORDS = {
        "extract": ["trích xuất", "extract", "parse", "parse JSON", "đọc dữ liệu"],
        "summarize": ["tóm tắt", "summarize", "brief", "overview"],
        "generate": ["viết code", "generate", "create function", "implement"],
        "analyze": ["phân tích", "analyze", "evaluate", "compare", "đánh giá"],
        "review": ["review", "kiểm tra", "check code", "validate"],
        "finalize": ["hoàn thiện", "finalize", "complete", "done"],
    }
    
    def classify_task(self, task_description: str) -> str:
        """Phân loại task dựa trên keywords"""
        task_lower = task_description.lower()
        for task_type, keywords in self.TASK_KEYWORDS.items():
            if any(kw in task_lower for kw in keywords):
                return task_type
        return "generate"  # Default
    
    def decompose(self, requirement: str) -> List[Tuple[str, ModelType]]:
        """
        Phân tách requirement thành các subtask với model phù hợp
        
        Returns:
            List of (subtask_description, model) tuples
        """
        subtasks = []
        
        # Bước 1: Nếu có data extraction
        if any(kw in requirement.lower() for kw in ["input", "dữ liệu", "data", "file"]):
            subtasks.append(("Extract and parse input data", ModelType.DEEPSEEK_V32))
        
        # Bước 2: Generate code chính
        subtasks.append(("Generate core implementation", ModelType.GEMINI_FLASH))
        
        # Bước 3: Review code
        subtasks.append(("Review for bugs and improvements", ModelType.CLAUDE_SONNET))
        
        # Bước 4: Finalize và optimize
        subtasks.append(("Finalize and optimize", ModelType.GPT_41))
        
        return subtasks

class MultiModelPipeline:
    """
    Pipeline xử lý đa model với automatic failover
    """
    
    def __init__(self, agent: HolySheepClineAgent):
        self.agent = agent
        self.decomposer = TaskDecomposer()
        self.fallback_models = {
            ModelType.DEEPSEEK_V32: ModelType.GEMINI_FLASH,
            ModelType.GEMINI_FLASH: ModelType.CLAUDE_SONNET,
            ModelType.CLAUDE_SONNET: ModelType.GPT_41,
            ModelType.GPT_41: ModelType.GEMINI_FLASH,
        }
    
    async def execute_pipeline(self, requirement: str) -> Dict:
        """
        Thực thi pipeline hoàn chỉnh
        
        Pipeline flow:
        1. Decompose requirement into subtasks
        2. Execute each subtask with optimal model
        3. Automatic failover if model fails
        4. Aggregate results and generate report
        """
        subtasks = self.decomposer.decompose(requirement)
        results = []
        context = ""
        
        print(f"Pipeline started: {len(subtasks)} subtasks")
        
        for i, (subtask_desc, primary_model) in enumerate(subtasks):
            print(f"\n[Step {i+1}/{len(subtasks)}] {subtask_desc}")
            print(f"  Primary model: {primary_model.value}")
            
            messages = [
                {"role": "system", "content": "You are a helpful AI assistant."},
                {"role": "user", "content": f"Task: {subtask_desc}\n\nContext: {context}"}
            ]
            
            result = self.agent.chat_completion(model=primary_model, messages=messages)
            
            # Automatic failover
            if not result.success:
                print(f"  ⚠ Primary failed, trying fallback...")
                fallback_model = self.fallback_models[primary_model]
                print(f"  Fallback model: {fallback_model.value}")
                result = self.agent.chat_completion(model=fallback_model, messages=messages)
            
            if result.success:
                print(f"  ✅ Success - {result.latency_ms:.0f}ms, ${result.cost_usd:.6f}")
                context += f"\n\n[Step {i+1} - {primary_model.value}]:\n{result.response}"
                results.append({
                    "step": i + 1,
                    "description": subtask_desc,
                    "model": result.model_used,
                    "response": result.response,
                    "cost": result.cost_usd
                })
            else:
                print(f"  ❌ Failed: {result.error_message}")
                results.append({
                    "step": i + 1,
                    "description": subttask_desc,
                    "error": result.error_message
                })
        
        return {
            "status": "completed" if all("error" not in r for r in results) else "partial",
            "steps": results,
            "total_cost": self.agent.total_cost,
            "total_tokens": self.agent.total_tokens,
            "final_context": context
        }

=== DEMO PIPELINE ===

async def main(): agent = HolySheepClineAgent(api_key="YOUR_HOLYSHEEP_API_KEY") pipeline = MultiModelPipeline(agent) requirement = """ Viết một REST API endpoint để quản lý danh sách người dùng. API cần hỗ trợ CRUD operations và validation. Input data sẽ được truyền qua JSON body. """ report = await pipeline.execute_pipeline(requirement) print("\n" + "="*50) print("📊 PIPELINE EXECUTION REPORT") print("="*50) print(f"Status: {report['status'].upper()}") print(f"Total Cost: ${report['total_cost']:.6f}") print(f"Total Tokens: {report['total_tokens']:,}") print(f"\nBreakdown by step:") for step in report['steps']: if 'error' in step: print(f" Step {step['step']}: ❌ {step['error']}") else: print(f" Step {step['step']}: ✅ ${step['cost']:.6f} ({step['model']})") if __name__ == "__main__": asyncio.run(main())

Tích Hợp Retry Logic và Rate Limiting

import threading
import time
from typing import Callable, Any
from functools import wraps

class RateLimiter:
    """
    Token bucket rate limiter cho HolySheep API
    Default: 100 requests/phút, có thể tăng theo tier
    """
    
    def __init__(self, requests_per_minute: int = 100):
        self.rpm = requests_per_minute
        self.tokens = self.rpm
        self.last_update = time.time()
        self.lock = threading.Lock()
    
    def acquire(self) -> bool:
        """Acquire a token, returns True if successful"""
        with self.lock:
            now = time.time()
            elapsed = now - self.last_update
            
            # Refill tokens based on elapsed time
            self.tokens = min(self.rpm, self.tokens + elapsed * (self.rpm / 60))
            self.last_update = now
            
            if self.tokens >= 1:
                self.tokens -= 1
                return True
            return False
    
    def wait_and_acquire(self, timeout: float = 60.0):
        """Wait until a token is available"""
        start = time.time()
        while time.time() - start < timeout:
            if self.acquire():
                return True
            time.sleep(0.1)
        raise TimeoutError("Rate limiter timeout")

class ResilientClient:
    """
    HolySheep Client với automatic retry, rate limiting, và circuit breaker
    """
    
    def __init__(self, api_key: str, rpm: int = 100):
        self.agent = HolySheepClineAgent(api_key)
        self.rate_limiter = RateLimiter(rpm)
        
        # Circuit breaker config
        self.failure_threshold = 5
        self.recovery_timeout = 60
        self.failure_count = 0
        self.circuit_open_time = None
        self.state = "CLOSED"  # CLOSED, OPEN, HALF_OPEN
    
    def call_with_resilience(self, model: ModelType, messages: List[Dict]) -> TaskResult:
        """
        Gọi API với đầy đủ resilience patterns
        """
        # Check circuit breaker
        if self.state == "OPEN":
            if time.time() - self.circuit_open_time > self.recovery_timeout:
                self.state = "HALF_OPEN"
                print("🔄 Circuit breaker: HALF_OPEN")
            else:
                raise Exception("Circuit breaker OPEN - service unavailable")
        
        # Acquire rate limit token
        self.rate_limiter.wait_and_acquire()
        
        try:
            result = self.agent.chat_completion(model=model, messages=messages)
            
            if result.success:
                # Reset circuit breaker on success
                if self.state == "HALF_OPEN":
                    print("✅ Circuit breaker: CLOSED (recovery successful)")
                self.state = "CLOSED"
                self.failure_count = 0
                return result
            else:
                # Handle failure
                self.failure_count += 1
                if self.failure_count >= self.failure_threshold:
                    self.state = "OPEN"
                    self.circuit_open_time = time.time()
                    print(f"⚠️ Circuit breaker: OPEN ({self.failure_threshold} failures)")
                raise Exception(result.error_message)
                
        except Exception as e:
            self.failure_count += 1
            if self.failure_count >= self.failure_threshold:
                self.state = "OPEN"
                self.circuit_open_time = time.time()
            raise

def with_resilience(client: ResilientClient, model: ModelType):
    """Decorator cho resilience pattern"""
    def decorator(func: Callable) -> Callable:
        @wraps(func)
        def wrapper(*args, **kwargs) -> Any:
            messages = func(*args, **kwargs)
            return client.call_with_resilience(model, messages)
        return wrapper
    return decorator

=== SỬ DỤNG VỚI DECORATOR ===

client = ResilientClient(api_key="YOUR_HOLYSHEEP_API_KEY", rpm=100) @with_resilience(client, ModelType.CLAUDE_SONNET) def generate_review_task(code: str) -> List[Dict]: """Task: Code review với automatic retry""" return [ {"role": "system", "content": "You are a senior code reviewer."}, {"role": "user", "content": f"Review this code:\n{code}"} ]

Batch processing với rate limiting

batch_codes = [ "def add(a, b): return a + b", "def divide(a, b): return a / b", "def multiply(a, b): return a * b", ] print("📦 Processing batch with rate limiting...") for i, code in enumerate(batch_codes): result = generate_review_task(code) print(f" [{i+1}/{len(batch_codes)}] ✅ Review complete")

Project-Level Usage Report Generator

from datetime import datetime, timedelta
from typing import Dict, List
import json

class UsageReportGenerator:
    """
    Generator báo cáo sử dụng chi tiết theo dự án
    Hỗ trợ tracking chi phí thực tế với HolySheep pricing
    """
    
    def __init__(self, project_name: str):
        self.project_name = project_name
        self.records: List[Dict] = []
        self.start_time = datetime.now()
        
        # Pricing reference (2026)
        self.pricing = {
            "deepseek-v3.2": 0.42,
            "gemini-2.5-flash": 2.50,
            "claude-sonnet-4.5": 15.00,
            "gpt-4.1": 8.00,
        }
    
    def log_request(
        self,
        model: str,
        input_tokens: int,
        output_tokens: int,
        latency_ms: float,
        task_type: str = "general"
    ):
        """Log một request để track usage"""
        total_tokens = input_tokens + output_tokens
        cost = (total_tokens / 1_000_000) * self.pricing.get(model, 0)
        
        self.records.append({
            "timestamp": datetime.now().isoformat(),
            "model": model,
            "input_tokens": input_tokens,
            "output_tokens": output_tokens,
            "total_tokens": total_tokens,
            "latency_ms": latency_ms,
            "cost_usd": cost,
            "task_type": task_type
        })
    
    def generate_report(self) -> Dict:
        """Generate báo cáo chi tiết"""
        if not self.records:
            return {"error": "No records to report"}
        
        total_input = sum(r["input_tokens"] for r in self.records)
        total_output = sum(r["output_tokens"] for r in self.records)
        total_tokens = total_input + total_output
        total_cost = sum(r["cost_usd"] for r in self.records)
        avg_latency = sum(r["latency_ms"] for r in self.records) / len(self.records)
        
        # Breakdown by model
        model_breakdown = {}
        for record in self.records:
            model = record["model"]
            if model not in model_breakdown:
                model_breakdown[model] = {"requests": 0, "tokens": 0, "cost": 0}
            model_breakdown[model]["requests"] += 1
            model_breakdown[model]["tokens"] += record["total_tokens"]
            model_breakdown[model]["cost"] += record["cost_usd"]
        
        # Breakdown by task type
        task_breakdown = {}
        for record in self.records:
            task = record["task_type"]
            if task not in task_breakdown:
                task_breakdown[task] = {"requests": 0, "tokens": 0, "cost": 0}
            task_breakdown[task]["requests"] += 1
            task_breakdown[task]["tokens"] += record["total_tokens"]
            task_breakdown[task]["cost"] += record["cost_usd"]
        
        return {
            "project": self.project_name,
            "report_generated": datetime.now().isoformat(),
            "period_start": self.start_time.isoformat(),
            "period_end": datetime.now().isoformat(),
            "summary": {
                "total_requests": len(self.records),
                "total_input_tokens": total_input,
                "total_output_tokens": total_output,
                "total_tokens": total_tokens,
                "total_cost_usd": round(total_cost, 6),
                "avg_latency_ms": round(avg_latency, 2),
            },
            "model_breakdown": model_breakdown,
            "task_breakdown": task_breakdown,
            "savings_vs_official": {
                "estimated_official_cost": round(total_tokens / 1_000_000 * 15, 2),  # GPT-4.1 official
                "holy_sheep_cost": round(total_cost, 6),
                "savings_usd": round(total_tokens / 1_000_000 * 15 - total_cost, 2),
                "savings_percent": round((1 - total_cost / (total_tokens / 1_000_000 * 15)) * 100, 1)
            }
        }
    
    def export_json(self, filepath: str):
        """Export report ra JSON file"""
        report = self.generate_report()
        with open(filepath, "w", encoding="utf-8") as f:
            json.dump(report, f, indent=2, ensure_ascii=False)
        print(f"📄 Report exported to {filepath}")

=== SỬ DỤNG TRACKER ===

tracker = UsageReportGenerator("MyProject-AutomationPipeline")

Simulate requests

test_data = [ {"model": "deepseek-v3.2", "input": 1200, "output": 800, "latency": 45, "task": "extraction"}, {"model": "gemini-2.5-flash", "input": 500, "output": 1200, "latency": 38, "task": "generation"}, {"model": "claude-sonnet-4.5", "input": 800, "output": 1500, "latency": 52, "task": "analysis"}, {"model": "gpt-4.1", "input": 600, "output": 900, "latency": 48, "task": "finalize"}, ] for data in test_data: tracker.log_request( model=data["model"], input_tokens=data["input"], output_tokens=data["output"], latency_ms=data["latency"], task_type=data["task"] )

Generate và in report

report = tracker.generate_report() print("="*60) print("📊 PROJECT USAGE REPORT") print("="*60) print(f"Project: {report['project']}") print(f"Total Requests: {report['summary']['total_requests']}") print(f"Total Tokens: {report['summary']['total_tokens']:,}") print(f"Total Cost: ${report['summary']['total_cost_usd']}") print(f"Avg Latency: {report['summary']['avg_latency_ms']}ms") print(f"\n💰 SAVINGS vs Official API:") print(f" Official Cost: ${report['savings_vs_official']['estimated_official_cost']}") print(f" HolySheep Cost: ${report['savings_vs_official']['holy_sheep_cost']}") print(f" You Save: ${report['savings_vs_official']['savings_usd']} ({report['savings_vs_official']['savings_percent']}%)")

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

1. Lỗi "401 Unauthorized" - API Key Không Hợp Lệ

# ❌ SAI: Dùng API key trực tiếp hoặc sai format
response = requests.post(
    "https://api.openai.com/v1/chat/completions",  # SAI DOMAIN!
    headers={"Authorization": f"Bearer {api_key}"}
)

✅ ĐÚNG: Dùng HolySheep base URL

response = requests.post( "https://api.holysheep.ai/v1/chat/completions", headers={"Authorization": f"Bearer {api_key}"} )

Kiểm tra API key

def validate_holy_sheep_key(api_key: str) -> bool: """Validate API key bằng cách gọi test request""" try: response = requests.post( "https://api.holysheep.ai/v1/chat/completions", headers={"Authorization": f"Bearer {api_key}"}, json={ "model": "deepseek-v3.2", "messages": [{"role": "user", "content": "test"}], "max_tokens": 5 }, timeout=10 ) if response.status_code == 200: return True elif response.status_code == 401: print("❌ API key không hợp lệ hoặc đã hết hạn") return False else: print(f"⚠️ Lỗi khác: {response.status_code}") return False except Exception as e: print(f"❌ Connection error: {e}") return False

Test

is_valid = validate_holy_sheep_key("YOUR_HOLYSHEEP_API_KEY") print(f"API Key Valid: {is_valid}")

2. Lỗi "429 Rate Limit Exceeded" - Vượt Quá Giới Hạn Request

import time
from threading import Semaphore

❌ SAI: Gửi request liên tục không có rate limiting

for item in items: result = agent.chat_completion(model, messages) # Có thể bị 429

✅ ĐÚNG: Implement exponential backoff với retry

class RateLimitHandler: def __init__(self, max_retries: int = 5): self.max_retries = max_retries self.semaphore = Semaphore(10) # Max 10 concurrent requests def call_with_backoff(self, func, *args, **kwargs): for attempt in range(self.max_retries): try: with self.semaphore: return func(*args, **kwargs) except Exception as e: if "429" in str(e) or "rate limit" in str(e).lower(): wait_time = min(2 ** attempt * 1.0, 60) # Max 60s print(f"⏳ Rate limited. Waiting {wait_time}s (attempt {attempt + 1})") time.sleep(wait_time) else: raise raise Exception("Max retries exceeded due to rate limiting") handler = RateLimitHandler(max_retries=5) result = handler.call_with_backoff( agent.chat_completion, model=ModelType.GEMINI_FLASH, messages=[{"role": "user", "content": "Hello"}] )

3. Lỗi "500 Internal Server Error" - Server HolySheep Quá Tải

# ❌ SAI: Không handle server error, crash ngay
response = session.post(url, json=payload)
response.raise_for_status()  # Sẽ raise exception nếu 500

✅ ĐÚNG: Implement circuit breaker và fallback

class CircuitBreaker: def __init__(self, failure_threshold: int = 3, timeout: int = 30): self.failure_threshold = failure_threshold self.timeout = timeout self.failures = 0 self.last_failure_time = None self.state = "CLOSED" # CLOSED, OPEN, HALF_OPEN def call(self, func, *args, **kwargs): # Check if circuit should transition if self.state == "OPEN": if time.time() - self.last_failure_time > self.timeout: self.state = "HALF_OPEN" else: raise Exception("Circuit OPEN - try fallback model") try: result = func(*args, **kwargs) # Success - reset circuit if self.state == "