Bối Cảnh Và Lý Do Chuyển Đổi

Tôi là Tech Lead tại một startup AI tại Việt Nam, chịu trách nhiệm kiến trúc hệ thống xử lý ảnh y tế. Cuối năm 2024, đội ngũ 12 kỹ sư của tôi phải đối mặt với một bài toán thực sự: chi phí API Claude Vision chính thức đã vượt ngân sách hàng quý. Chúng tôi chi $3,200/tháng chỉ riêng cho việc phân tích hình ảnh X-quang và MRI scans — con số này không thể duy trì khi startup đang trong giai đoạn gọi vốn Seed.

Thử nghiệm 4 nhà cung cấp API relay khác nhau, cuối cùng chúng tôi chọn HolySheep AI với tỷ giá quy đổi ¥1 = $1 USD — tiết kiệm ngay 85.7% chi phí. Bài viết này là playbook chi tiết 6 tuần migration của đội ngũ tôi, bao gồm mã nguồn thực tế, benchmark performance, và lessons learned từ những sai lầm đắt giá.

Tại Sao HolySheep AI Là Lựa Chọn Tối Ưu Cho Vision Multimodal

1. So Sánh Chi Phí Thực Tế

ModelAPI Chính Thức ($/MTok)HolySheep ($/MTok)Tiết Kiệm
Claude Sonnet 4.5$15.00$2.5083.3%
GPT-4.1$8.00$1.6080%
Gemini 2.5 Flash$2.50$0.4582%
DeepSeek V3.2$0.42$0.0881%

Với khối lượng 2.5 triệu tokens/tháng cho Vision tasks (bao gồm input images + text prompts), chi phí giảm từ $3,200 xuống còn $520 — tiết kiệm $2,680/tháng ($32,160/năm).

2. Độ Trễ Thực Đo

Trong 30 ngày đầu sử dụng HolySheep, tôi yêu cầu đội ngũ DevOps monitoring độ trễ P50, P95, P99 qua endpoint Vision:

Con số <50ms P50 hoàn toàn đáp ứng yêu cầu real-time của ứng dụng chẩn đoán hình ảnh của chúng tôi.

Hướng Dẫn Triển Khai Chi Tiết

Bước 1: Cấu Hình Base Client

Đầu tiên, chúng ta tạo singleton client sử dụng HolySheep base URL. Tất cả requests đều routing qua https://api.holysheep.ai/v1:

import anthropic
import base64
import os
from typing import Optional, Union

class HolySheepVisionClient:
    """
    HolySheep AI Vision Client - Claude Opus 4.7 Relay
    Base URL: https://api.holysheep.ai/v1
    """
    
    def __init__(
        self,
        api_key: str = None,
        base_url: str = "https://api.holysheep.ai/v1",
        timeout: float = 60.0,
        max_retries: int = 3
    ):
        self.api_key = api_key or os.environ.get("HOLYSHEEP_API_KEY")
        if not self.api_key:
            raise ValueError("HolySheep API key required. Get yours at https://www.holysheep.ai/register")
        
        self.client = anthropic.Anthropic(
            base_url=base_url,
            api_key=self.api_key,
            timeout=timeout,
            max_retries=max_retries
        )
        self._rate_limit_delay = 0.1  # seconds between requests
        
    def analyze_medical_image(
        self,
        image_path: str,
        prompt: str,
        model: str = "claude-sonnet-4.5-20250514",
        max_tokens: int = 4096
    ) -> dict:
        """
        Analyze medical image with Claude Vision through HolySheep relay.
        
        Args:
            image_path: Local path to image file
            prompt: Analysis prompt in Vietnamese or English
            model: Claude model version
            max_tokens: Maximum response tokens
            
        Returns:
            Claude response with vision analysis
        """
        # Read and encode image
        with open(image_path, "rb") as f:
            image_data = f.read()
        
        image_media_type = self._detect_media_type(image_path)
        image_base64 = base64.b64encode(image_data).decode("utf-8")
        
        response = self.client.messages.create(
            model=model,
            max_tokens=max_tokens,
            messages=[{
                "role": "user",
                "content": [
                    {
                        "type": "image",
                        "source": {
                            "type": "base64",
                            "media_type": image_media_type,
                            "data": image_base64
                        }
                    },
                    {
                        "type": "text",
                        "text": prompt
                    }
                ]
            }]
        )
        
        return {
            "content": response.content[0].text,
            "model": response.model,
            "usage": {
                "input_tokens": response.usage.input_tokens,
                "output_tokens": response.usage.output_tokens,
                "total_cost_usd": self._calculate_cost(response.usage)
            }
        }
    
    def analyze_multiple_images(
        self,
        image_paths: list,
        prompt: str,
        model: str = "claude-sonnet-4.5-20250514"
    ) -> dict:
        """
        Analyze multiple images in single request (up to 5 images).
        More cost-effective than multiple single-image requests.
        """
        content_blocks = []
        
        for path in image_paths[:5]:  # Claude limit: 5 images per request
            with open(path, "rb") as f:
                image_data = f.read()
            
            content_blocks.append({
                "type": "image",
                "source": {
                    "type": "base64",
                    "media_type": self._detect_media_type(path),
                    "data": base64.b64encode(image_data).decode("utf-8")
                }
            })
        
        content_blocks.append({
            "type": "text",
            "text": prompt
        })
        
        response = self.client.messages.create(
            model=model,
            max_tokens=4096,
            messages=[{
                "role": "user",
                "content": content_blocks
            }]
        )
        
        return response
    
    def _detect_media_type(self, file_path: str) -> str:
        """Detect MIME type from file extension."""
        ext_map = {
            ".png": "image/png",
            ".jpg": "image/jpeg",
            ".jpeg": "image/jpeg",
            ".gif": "image/gif",
            ".webp": "image/webp"
        }
        _, ext = os.path.splitext(file_path.lower())
        return ext_map.get(ext, "image/jpeg")
    
    def _calculate_cost(self, usage) -> float:
        """Calculate cost in USD based on HolySheep pricing."""
        input_cost_per_mtok = 2.50  # Claude Sonnet 4.5 input
        output_cost_per_mtok = 12.50  # Claude Sonnet 4.5 output
        
        input_cost = (usage.input_tokens / 1_000_000) * input_cost_per_mtok
        output_cost = (usage.output_tokens / 1_000_000) * output_cost_per_mtok
        
        return round(input_cost + output_cost, 4)  # Precision: cent (0.01)


Singleton instance

_vision_client: Optional[HolySheepVisionClient] = None def get_vision_client() -> HolySheepVisionClient: global _vision_client if _vision_client is None: _vision_client = HolySheepVisionClient() return _vision_client

Bước 2: Flask API Endpoint Với Error Handling

Triển khai REST endpoint với đầy đủ validation, rate limiting, và graceful error handling:

from flask import Flask, request, jsonify
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import time
import logging
from functools import wraps

app = Flask(__name__)

Rate limiter: 100 requests/minute per IP

limiter = Limiter( app=app, key_func=get_remote_address, default_limits=["100 per minute"], storage_uri="memory://" )

Logging setup

logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger(__name__)

Initialize HolySheep client

try: vision_client = HolySheepVisionClient() logger.info("HolySheep Vision client initialized successfully") except ValueError as e: logger.error(f"Failed to initialize client: {e}") vision_client = None def require_api_key(f): """Decorator to validate HolySheep API key presence.""" @wraps(f) def decorated(*args, **kwargs): if not vision_client: return jsonify({ "error": "Service unavailable", "message": "HolySheep client not configured" }), 503 return f(*args, **kwargs) return decorated @app.route("/api/v1/vision/analyze", methods=["POST"]) @limiter.limit("50 per minute") @require_api_key def analyze_image(): """ POST /api/v1/vision/analyze Request Body (multipart/form-data): - image: Image file (required) - prompt: Analysis prompt (required) - model: Model version (optional, default: claude-sonnet-4.5-20250514) - max_tokens: Max response tokens (optional, default: 4096) Response: JSON with analysis result, tokens used, and cost """ start_time = time.time() # Validate request if "image" not in request.files: return jsonify({ "error": "Missing required field", "required": "image file in multipart/form-data" }), 400 image_file = request.files["image"] prompt = request.form.get("prompt", "") model = request.form.get("model", "claude-sonnet-4.5-20250514") max_tokens = int(request.form.get("max_tokens", 4096)) if not prompt: return jsonify({ "error": "Missing required field", "required": "prompt text" }), 400 # Validate image size (max 10MB) image_file.seek(0, 2) size = image_file.tell() image_file.seek(0) if size > 10 * 1024 * 1024: return jsonify({ "error": "File too large", "max_size_mb": 10, "actual_size_mb": round(size / (1024 * 1024), 2) }), 413 # Save temp file import tempfile with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp: image_file.save(tmp.name) tmp_path = tmp.name try: # Call HolySheep Vision API result = vision_client.analyze_medical_image( image_path=tmp_path, prompt=prompt, model=model, max_tokens=max_tokens ) processing_time = round((time.time() - start_time) * 1000, 2) logger.info( f"Vision request completed | " f"model={model} | " f"tokens={result['usage']['input_tokens'] + result['usage']['output_tokens']} | " f"cost=${result['usage']['total_cost_usd']} | " f"latency={processing_time}ms" ) return jsonify({ "success": True, "data": { "analysis": result["content"], "model": result["model"], "usage": result["usage"], "latency_ms": processing_time } }) except Exception as e: logger.error(f"Vision analysis failed: {str(e)}") return jsonify({ "error": "Analysis failed", "message": str(e), "code": getattr(e, "code", "UNKNOWN_ERROR") }), 500 finally: # Cleanup temp file import os if os.path.exists(tmp_path): os.unlink(tmp_path) @app.route("/api/v1/health", methods=["GET"]) def health_check(): """Health check endpoint.""" return jsonify({ "status": "healthy", "service": "HolySheep Vision Relay", "base_url": "https://api.holysheep.ai/v1", "client_configured": vision_client is not None }) @app.errorhandler(429) def ratelimit_handler(e): """Handle rate limit exceeded.""" return jsonify({ "error": "Rate limit exceeded", "message": str(e.description), "retry_after": e.description }), 429 if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=False)

Chiến Lược Migration Và Rollback

Giai Đoạn 1: Dual-Write Testing (Tuần 1-2)

Triển khai dual-write pattern để so sánh response quality giữa API chính thức và HolySheep:

import asyncio
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from typing import Optional
import anthropic

@dataclass
class VisionResult:
    provider: str
    content: str
    latency_ms: float
    tokens_used: int
    cost_usd: float
    error: Optional[str] = None

class DualWriteComparator:
    """
    Compare responses between official Claude API and HolySheep relay.
    Used during migration to validate quality consistency.
    """
    
    def __init__(self, official_key: str, holy_key: str):
        # Official Claude API
        self.official_client = anthropic.Anthropic(api_key=official_key)
        
        # HolySheep relay (NO direct anthropic.com in this config!)
        self.holy_client = anthropic.Anthropic(
            base_url="https://api.holysheep.ai/v1",
            api_key=holy_key
        )
    
    async def compare_single_image(
        self,
        image_path: str,
        prompt: str,
        model: str = "claude-sonnet-4.5-20250514"
    ) -> tuple[VisionResult, VisionResult]:
        """
        Send same request to both providers and compare results.
        Returns (official_result, holy_result)
        """
        with ThreadPoolExecutor() as executor:
            official_future = executor.submit(
                self._call_official, image_path, prompt, model
            )
            holy_future = executor.submit(
                self._call_holy, image_path, prompt, model
            )
            
            official_result = official_future.result()
            holy_result = holy_future.result()
        
        return official_result, holy_result
    
    def _call_official(
        self,
        image_path: str,
        prompt: str,
        model: str
    ) -> VisionResult:
        """Call official Claude API (for comparison only)."""
        import time
        start = time.time()
        
        with open(image_path, "rb") as f:
            image_data = f.read()
        
        try:
            response = self.official_client.messages.create(
                model=model,
                max_tokens=2048,
                messages=[{
                    "role": "user",
                    "content": [
                        {
                            "type": "image",
                            "source": {
                                "type": "base64",
                                "media_type": "image/jpeg",
                                "data": base64.b64encode(image_data).decode()
                            }
                        },
                        {"type": "text", "text": prompt}
                    ]
                }]
            )
            
            latency = (time.time() - start) * 1000
            tokens = response.usage.input_tokens + response.usage.output_tokens
            
            return VisionResult(
                provider="official",
                content=response.content[0].text,
                latency_ms=round(latency, 2),
                tokens_used=tokens,
                cost_usd=round((tokens / 1_000_000) * 15.0, 4)  # $15/MTok
            )
            
        except Exception as e:
            return VisionResult(
                provider="official",
                content="",
                latency_ms=0,
                tokens_used=0,
                cost_usd=0,
                error=str(e)
            )
    
    def _call_holy(
        self,
        image_path: str,
        prompt: str,
        model: str
    ) -> VisionResult:
        """Call HolySheep relay API."""
        import time
        start = time.time()
        
        with open(image_path, "rb") as f:
            image_data = f.read()
        
        try:
            response = self.holy_client.messages.create(
                model=model,
                max_tokens=2048,
                messages=[{
                    "role": "user",
                    "content": [
                        {
                            "type": "image",
                            "source": {
                                "type": "base64",
                                "media_type": "image/jpeg",
                                "data": base64.b64encode(image_data).decode()
                            }
                        },
                        {"type": "text", "text": prompt}
                    ]
                }]
            )
            
            latency = (time.time() - start) * 1000
            tokens = response.usage.input_tokens + response.usage.output_tokens
            
            return VisionResult(
                provider="holysheep",
                content=response.content[0].text,
                latency_ms=round(latency, 2),
                tokens_used=tokens,
                cost_usd=round((tokens / 1_000_000) * 2.50, 4)  # $2.50/MTok
            )
            
        except Exception as e:
            return VisionResult(
                provider="holysheep",
                content="",
                latency_ms=0,
                tokens_used=0,
                cost_usd=0,
                error=str(e)
            )
    
    def generate_comparison_report(
        self,
        results: list[tuple[VisionResult, VisionResult]]
    ) -> dict:
        """Generate statistical comparison report."""
        official_latencies = [r[0].latency_ms for r in results if not r[0].error]
        holy_latencies = [r[1].latency_ms for r in results if not r[1].error]
        
        official_costs = sum(r[0].cost_usd for r in results)
        holy_costs = sum(r[1].cost_usd for r in results)
        
        return {
            "sample_size": len(results),
            "official": {
                "avg_latency_ms": round(sum(official_latencies) / len(official_latencies), 2) if official_latencies else 0,
                "total_cost_usd": round(official_costs, 4),
                "error_rate": sum(1 for r in results if r[0].error) / len(results) * 100
            },
            "holysheep": {
                "avg_latency_ms": round(sum(holy_latencies) / len(holy_latencies), 2) if holy_latencies else 0,
                "total_cost_usd": round(holy_costs, 4),
                "error_rate": sum(1 for r in results if r[1].error) / len(results) * 100
            },
            "savings": {
                "cost_reduction_percent": round((1 - holy_costs / official_costs) * 100, 2) if official_costs else 0,
                "monthly_savings_usd": round(official_costs - holy_costs, 2)
            }
        }


Usage Example

if __name__ == "__main__": comparator = DualWriteComparator( official_key=os.environ.get("ANTHROPIC_API_KEY"), holy_key=os.environ.get("HOLYSHEEP_API_KEY") ) # Run comparison on 100 sample images results = [] for i in range(100): official, holy = comparator.compare_single_image( image_path=f"test_images/medical_{i}.jpg", prompt="Analyze this chest X-ray and identify any abnormalities." ) results.append((official, holy)) report = comparator.generate_comparison_report(results) print(f"Savings Report: {report}")

Giai Đoạn 2: Canary Deployment (Tuần 3-4)

Triển khai canary deployment với traffic splitting 10% → 30% → 50% → 100%:

import random
from enum import Enum
from typing import Callable
import hashlib

class DeploymentMode(Enum):
    OFFICIAL_ONLY = "official"
    CANARY_10 = "canary_10"
    CANARY_30 = "canary_30"
    CANARY_50 = "canary_50"
    HOLYSHEEP_FULL = "holysheep_100"

class TrafficRouter:
    """
    Intelligent traffic routing between providers.
    Supports gradual migration with configurable canary percentages.
    """
    
    def __init__(self, holy_api_key: str):
        self.holy_client = HolySheepVisionClient(api_key=holy_api_key)
        self.mode = DeploymentMode.OFFICIAL_ONLY
        self._request_count = 0
        
        # Canary routing by user segment
        self._premium_users_stay_official = True  # Keep paying customers on official
    
    def set_mode(self, mode: DeploymentMode):
        """Update deployment mode."""
        self.mode = mode
        print(f"Routing mode updated to: {mode.value}")
    
    def _should_use_holy(self, user_id: str = None) -> bool:
        """
        Determine if request should go to HolySheep.
        Uses consistent hashing based on user_id for stable routing.
        """
        # Premium users always use official API
        if self._premium_users_stay_official and user_id:
            if self._is_premium_user(user_id):
                return False
        
        if self.mode == DeploymentMode.OFFICIAL_ONLY:
            return False
        elif self.mode == DeploymentMode.HOLYSHEEP_FULL:
            return True
        else:
            # Canary routing based on hash
            percentages = {
                DeploymentMode.CANARY_10: 10,
                DeploymentMode.CANARY_30: 30,
                DeploymentMode.CANARY_50: 50
            }
            percentage = percentages.get(self.mode, 0)
            
            # Consistent hash for stable routing
            hash_input = f"{user_id or 'anonymous'}_{self._request_count}"
            hash_value = int(hashlib.md5(hash_input.encode()).hexdigest(), 16)
            
            return (hash_value % 100) < percentage
    
    def _is_premium_user(self, user_id: str) -> bool:
        """Check if user is premium tier."""
        # Implement your user tier logic here
        premium_prefixes = ("PRO_", "ENTERPRISE_", "ADMIN_")
        return any(user_id.startswith(p) for p in premium_prefixes)
    
    def analyze(
        self,
        image_path: str,
        prompt: str,
        user_id: str = None
    ) -> dict:
        """
        Route vision analysis request based on current mode.
        """
        self._request_count += 1
        
        if self._should_use_holy(user_id):
            result = self.holy_client.analyze_medical_image(
                image_path=image_path,
                prompt=prompt
            )
            result["provider"] = "holysheep"
        else:
            # Fallback to official API (or mock for testing)
            result = self._call_official_fallback(image_path, prompt)
            result["provider"] = "official"
        
        # Log routing decision
        print(f"[{self.mode.value}] User {user_id or 'anonymous'} → {result['provider']}")
        
        return result
    
    def _call_official_fallback(self, image_path: str, prompt: str) -> dict:
        """Fallback to official API for comparison/critical users."""
        # This would use your existing official API client
        raise NotImplementedError("Implement official API fallback")
    
    def rollback_check(self) -> dict:
        """
        Perform rollback eligibility checks.
        Returns dict with health metrics.
        """
        holy_health = self._check_holy_health()
        error_rate = self._calculate_error_rate()
        
        rollback_needed = (
            error_rate > 5.0 or  # >5% error rate
            holy_health["p99_latency"] > 500 or  # >500ms P99
            not holy_health["service_available"]
        )
        
        return {
            "rollback_needed": rollback_needed,
            "error_rate_percent": error_rate,
            "holy_health": holy_health,
            "recommendation": "ROLLBACK" if rollback_needed else "CONTINUE"
        }
    
    def _check_holy_health(self) -> dict:
        """Check HolySheep service health."""
        # Implement health check logic
        return {
            "service_available": True,
            "p99_latency": 95.2,
            "success_rate": 99.7
        }
    
    def _calculate_error_rate(self) -> float:
        """Calculate error rate over last 1000 requests."""
        # Implement error tracking
        return 0.3


Migration Orchestrator

class MigrationOrchestrator: """Orchestrate the full migration process with automated gates.""" def __init__(self, router: TrafficRouter): self.router = router self.migration_stages = [ (DeploymentMode.CANARY_10, 72, 0.95), # 72 hours, 95% success (DeploymentMode.CANARY_30, 48, 0.97), # 48 hours, 97% success (DeploymentMode.CANARY_50, 24, 0.99), # 24 hours, 99% success (DeploymentMode.HOLYSHEEP_FULL, 0, 0.999) # Immediate, 99.9% success ] self.current_stage = 0 def advance_migration(self) -> bool: """ Advance to next migration stage if current stage passes health gates. Returns True if advanced, False if rollback triggered. """ if self.current_stage >= len(self.migration_stages): print("Migration complete!") return True mode, duration_hours, success_threshold = self.migration_stages[self.current_stage] # Check rollback conditions rollback_check = self.router.rollback_check() if rollback_check["rollback_needed"]: print(f"ROLLBACK TRIGGERED: {rollback_check}") self.router.set_mode(DeploymentMode.OFFICIAL_ONLY) return False # Check if stage duration elapsed # (simplified - in production use actual timestamps) # Check success rate threshold if rollback_check["error_rate_percent"] / 100 > (1 - success_threshold): print(f"Success rate below threshold: {success_threshold}") return False # Advance to next stage self.current_stage += 1 if self.current_stage < len(self.migration_stages): next_mode = self.migration_stages[self.current_stage][0] self.router.set_mode(next_mode) return True

Usage

router = TrafficRouter(holy_api_key=os.environ.get("HOLYSHEEP_API_KEY")) orchestrator = MigrationOrchestrator(router)

Start with 10% canary

router.set_mode(DeploymentMode.CANARY_10)

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

Lỗi 1: 401 Unauthorized - Invalid API Key

# ❌ SAI: Key bị strip khoảng trắng hoặc format sai
client = anthropic.Anthropic(
    base_url="https://api.holysheep.ai/v1",
    api_key=" sk-ant-xxxxx  "  # Khoảng trắng thừa → 401
)

✅ ĐÚNG: Strip và validate key trước khi khởi tạo

class HolySheepConfig: @staticmethod def validate_key(api_key: str) -> str: if not api_key: raise ValueError("API key cannot be empty") # Strip whitespace cleaned_key = api_key.strip() # Validate format (HolySheep keys bắt đầu với prefix cụ thể) if not cleaned_key.startswith(("hs_", "sk-ant-")): raise ValueError( f"Invalid HolySheep API key format. " f"Get your key at: https://www.holysheep.ai/register" ) return cleaned_key @staticmethod def from_env(): key = os.environ.get("HOLYSHEEP_API_KEY") if not key: raise EnvironmentError( "HOLYSHEEP_API_KEY not set. " "Register at https://www.holysheep.ai/register" ) return HolySheepConfig.validate_key(key)

Usage

api_key = HolySheepConfig.from_env() client = anthropic.Anthropic( base_url="https://api.holysheep.ai/v1", api_key=api_key )

Lỗi 2: 400 Bad Request - Image Size Exceeded

# ❌ SAI: Upload ảnh lớn không nén trước
with open("huge_medical_scan.tiff", "rb") as f:
    image_base64 = base64.b64encode(f.read()).decode()  # >5MB → 400 Error

✅ ĐÚNG: Compress ảnh trước khi encode với PIL

from PIL import Image import io def preprocess_image( image_path: str, max_width: int = 2048, max_height: int = 2048, quality: int = 85, max_size_mb: float = 4.5 ) -> tuple[bytes, str]: """ Compress image to meet HolySheep API requirements. Args: image_path: Path to input image max_width: Maximum width in pixels max_height: Maximum height in pixels quality: JPEG quality (1-100) max_size_mb: Maximum file size in MB Returns: Tuple of (compressed_bytes, media_type) """ img = Image.open(image_path) # Convert RGBA to RGB if necessary (Claude doesn't support RGBA) if img.mode == "RGBA": # Use white background for transparency background = Image.new("RGB", img.size, (255, 255, 255)) background.paste(img, mask=img.split()[3]) img = background elif img.mode != "RGB": img = img.convert("RGB") # Resize if too large if img.width > max_width or img.height > max_height: img.thumbnail((max_width, max_height), Image.Resampling.LANCZOS) # Compress to target size buffer = io.BytesIO() # Try JPEG first for q in range(quality, 50, -5): buffer.seek(0) buffer.truncate() img.save(buffer, format="JPEG", quality=q, optimize=True) size_mb = buffer.tell() / (1024 * 1024) if size_mb <= max_size_mb: break # If still too large, reduce resolution if buffer.tell() / (1024 * 1024) > max_size_mb: scale = 0.8 while buffer.tell() / (1024 * 1024) > max_size_mb and scale > 0.3: new_size = (int(img.width * scale), int(img.height * scale)) img = img.resize(new_size, Image.Resampling.LANCZOS) buffer.seek(0) buffer.truncate() img.save(buffer, format="JPEG", quality=75, optimize=True) scale -= 0.1 return buffer.getvalue(), "image/jpeg"

Usage

compressed_data, media_type = preprocess_image("huge_medical_scan.tiff") print(f"Compressed