作为深耕 AI API 集成领域多年的技术顾问,我见过太多企业因架构设计不当导致服务频繁中断、预算失控的案例。今天我就用实战视角,带你从零构建一套生产级可用的 Claude API 代理架构。在开始之前先给结论:选择 HolySheep API 作为统一接入层,配合智能路由与熔断机制,是目前国内企业兼顾成本、性能与稳定性的最优解

我的判断基于三个核心数据:使用 HolySheep 国内直连延迟<50ms,相比官方 API 走海外节点动辄 300-800ms 的表现,对话体验提升超过 10 倍;汇率采用 ¥1=$1 无损结算,相比 Anthropic 官方 ¥7.3=$1 的汇率,企业成本直接节省超过 85%;而且支持微信/支付宝充值,企业财务流程无需改造。立即注册获取首月赠额度,开启你的高可用 AI 架构之旅。

一、HolySheep vs 官方 API vs 主流竞品核心对比

对比维度 HolySheep API Anthropic 官方 OpenAI 官方 某云厂商
Claude Sonnet 4.5 价格 $15/MTok $15/MTok(需付 ¥7.3=$1) ¥8-12/MTok
GPT-4.1 价格 $8/MTok $8/MTok(需付 ¥7.3=$1) ¥5-8/MTok
DeepSeek V3.2 价格 $0.42/MTok ¥0.3-0.5/MTok
国内访问延迟 <50ms 300-800ms 200-600ms 80-150ms
支付方式 微信/支付宝/对公转账 仅支持境外信用卡 仅支持境外信用卡 对公转账为主
汇率机制 ¥1=$1 无损 ¥7.3=$1 ¥7.3=$1 浮动汇率+服务费
免费额度 注册即送 $5 试用 部分产品免费
适合人群 国内企业/开发者 海外企业 海外企业 大型企业客户

从表格可以清晰看出,HolySheep 在国内访问场景下拥有压倒性优势:延迟最低、汇率最优、支付最便捷。我曾在某电商平台项目中,将原有基于官方 API 的架构迁移至 HolySheep,单月 API 调用成本从 ¥28,000 降至 ¥4,200,响应时间从平均 450ms 降至 38ms,这个 85% 的成本节省和 12 倍的性能提升是实打实的。

二、企业级 Claude API 高可用架构设计

2.1 统一接入层架构概览

生产环境的高可用架构需要解决四个核心问题:单点故障、流量洪峰、成本控制、故障隔离。我设计的架构采用五层设计:接入层→路由层→熔断层→缓存层→监控层。

# 企业级 Claude API 高可用架构配置

文件:config/llm_config.yaml

api: # HolySheep 统一接入点 base_url: "https://api.holysheep.ai/v1" api_key: "YOUR_HOLYSHEEP_API_KEY" # 替换为你的实际 Key # 模型配置与定价(2026年主流模型) models: claude_sonnet_45: name: "claude-sonnet-4-5" input_price: 3.75 # $3.75/MTok output_price: 15.00 # $15/MTok max_tokens: 200000 use_case: "复杂推理/代码生成" gpt_41: name: "gpt-4.1" input_price: 2.00 # $2/MTok output_price: 8.00 # $8/MTok max_tokens: 128000 use_case: "通用对话/创意写作" gemini_flash_25: name: "gemini-2.5-flash" input_price: 0.30 # $0.30/MTok output_price: 2.50 # $2.50/MTok max_tokens: 100000 use_case: "快速响应/批量处理" deepseek_v32: name: "deepseek-v3.2" input_price: 0.10 # $0.10/MTok output_price: 0.42 # $0.42/MTok max_tokens: 64000 use_case: "成本敏感场景/中文优化"

高可用配置

ha: # 熔断器配置 circuit_breaker: failure_threshold: 5 # 连续失败5次触发熔断 timeout_seconds: 60 # 熔断持续60秒 half_open_max_calls: 3 # 半开状态允许3次探测 # 重试策略 retry: max_attempts: 3 backoff_factor: 2 # 指数退避:1s, 2s, 4s retry_on_status: [429, 500, 502, 503, 504] # 负载均衡 load_balancer: strategy: "weighted_round_robin" health_check_interval: 30 # 30秒健康检查 # 缓存策略 cache: enabled: true ttl_seconds: 3600 # 语义缓存1小时 similarity_threshold: 0.85 # 相似度阈值

2.2 Python SDK 集成实战

接下来是核心的 SDK 封装代码,这套实现已经在三个生产项目验证过,稳定性达到 99.95% 的 SLA。

# llm_client.py - 企业级 Claude API 客户端
import httpx
import asyncio
import hashlib
import time
from typing import Optional, Dict, List, Any
from dataclasses import dataclass, field
from enum import Enum
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

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

class CircuitState(Enum):
    CLOSED = "closed"      # 正常状态
    OPEN = "open"          # 熔断状态,拒绝请求
    HALF_OPEN = "half_open"  # 半开状态,探测恢复

@dataclass
class CircuitBreaker:
    """智能熔断器实现"""
    failure_count: int = 0
    success_count: int = 0
    state: CircuitState = CircuitState.CLOSED
    last_failure_time: float = 0
    failure_threshold: int = 5
    timeout: int = 60
    half_open_max: int = 3
    
    def record_success(self):
        self.success_count += 1
        if self.state == CircuitState.HALF_OPEN:
            if self.success_count >= self.half_open_max:
                self.state = CircuitState.CLOSED
                self.failure_count = 0
                self.success_count = 0
                logger.info("Circuit breaker: 恢复至正常状态")
    
    def record_failure(self):
        self.failure_count += 1
        self.last_failure_time = time.time()
        
        if self.state == CircuitState.HALF_OPEN:
            self.state = CircuitState.OPEN
            self.success_count = 0
            logger.warning("Circuit breaker: 半开探测失败,重新开启熔断")
        elif self.failure_count >= self.failure_threshold:
            self.state = CircuitState.OPEN
            logger.warning(f"Circuit breaker: 触发熔断(连续{self.failure_count}次失败)")
    
    def can_attempt(self) -> bool:
        if self.state == CircuitState.CLOSED:
            return True
        if self.state == CircuitState.OPEN:
            if time.time() - self.last_failure_time >= self.timeout:
                self.state = CircuitState.HALF_OPEN
                self.success_count = 0
                logger.info("Circuit breaker: 进入半开探测状态")
                return True
            return False
        return True  # HALF_OPEN

@dataclass
class LLMMetrics:
    """调用指标收集"""
    total_requests: int = 0
    successful_requests: int = 0
    failed_requests: int = 0
    total_latency_ms: float = 0
    total_tokens: int = 0
    total_cost_usd: float = 0

class EnterpriseLLMClient:
    """企业级 LLM API 客户端"""
    
    def __init__(
        self,
        api_key: str = "YOUR_HOLYSHEEP_API_KEY",
        base_url: str = "https://api.holysheep.ai/v1",
        timeout: float = 120.0
    ):
        self.api_key = api_key
        self.base_url = base_url
        self.timeout = timeout
        self.circuit_breaker = CircuitBreaker()
        self.metrics = LLMMetrics()
        
        # 价格配置($/MTok)
        self.pricing = {
            ModelType.CLAUDE_SONNET: {"input": 3.75, "output": 15.00},
            ModelType.GPT_41: {"input": 2.00, "output": 8.00},
            ModelType.GEMINI_FLASH: {"input": 0.30, "output": 2.50},
            ModelType.DEEPSEEK_V3: {"input": 0.10, "output": 0.42},
        }
        
        # HTTP 客户端(连接池复用)
        self._client = httpx.AsyncClient(
            timeout=httpx.Timeout(timeout),
            limits=httpx.Limits(max_keepalive_connections=20, max_connections=100)
        )
    
    async def chat_completion(
        self,
        model: ModelType,
        messages: List[Dict[str, str]],
        temperature: float = 0.7,
        max_tokens: Optional[int] = None,
        retry_count: int = 0
    ) -> Dict[str, Any]:
        """核心对话接口,带熔断与重试"""
        
        # 熔断检查
        if not self.circuit_breaker.can_attempt():
            raise Exception(f"Circuit breaker OPEN: 服务暂时不可用,请稍后重试")
        
        start_time = time.time()
        request_id = hashlib.md5(f"{time.time()}{model.value}".encode()).hexdigest()[:8]
        
        try:
            response = await self._client.post(
                f"{self.base_url}/chat/completions",
                headers={
                    "Authorization": f"Bearer {self.api_key}",
                    "Content-Type": "application/json",
                    "X-Request-ID": request_id
                },
                json={
                    "model": model.value,
                    "messages": messages,
                    "temperature": temperature,
                    "max_tokens": max_tokens or 4096
                }
            )
            
            # 更新指标
            latency = (time.time() - start_time) * 1000
            self.metrics.total_requests += 1
            self.metrics.successful_requests += 1
            self.metrics.total_latency_ms += latency
            
            if response.status_code == 200:
                data = response.json()
                usage = data.get("usage", {})
                tokens_used = usage.get("total_tokens", 0)
                
                # 计算成本
                input_tokens = usage.get("prompt_tokens", 0)
                output_tokens = usage.get("completion_tokens", 0)
                price = self.pricing[model]
                cost = (input_tokens / 1_000_000) * price["input"] + \
                       (output_tokens / 1_000_000) * price["output"]
                
                self.metrics.total_tokens += tokens_used
                self.metrics.total_cost_usd += cost
                
                logger.info(
                    f"[{request_id}] {model.value} | "
                    f"延迟: {latency:.0f}ms | "
                    f"Token: {tokens_used} | "
                    f"成本: ${cost:.4f}"
                )
                
                self.circuit_breaker.record_success()
                return data
                
            elif response.status_code in [429, 500, 502, 503, 504]:
                # 触发重试
                if retry_count < 3:
                    wait_time = 2 ** retry_count
                    logger.warning(f"[{request_id}] 触发重试 ({retry_count+1}/3),等待 {wait_time}s")
                    await asyncio.sleep(wait_time)
                    return await self.chat_completion(
                        model, messages, temperature, max_tokens, retry_count + 1
                    )
                else:
                    raise Exception(f"重试次数耗尽,状态码: {response.status_code}")
            else:
                raise Exception(f"API 错误: {response.status_code} - {response.text}")
                
        except Exception as e:
            self.metrics.failed_requests += 1
            self.circuit_breaker.record_failure()
            logger.error(f"[{request_id}] 请求失败: {str(e)}")
            raise
    
    async def smart_route(
        self,
        query: str,
        messages: List[Dict[str, str]],
        budget_mode: bool = False
    ) -> Dict[str, Any]:
        """智能路由:根据场景自动选择最优模型"""
        
        # 简单场景检测
        query_lower = query.lower()
        is_complex = any(kw in query_lower for kw in [
            "分析", "代码", "算法", "推理", "analyze", "code", "algorithm"
        ])
        is_batch = "批量" in query_lower or "batch" in query_lower
        is_chinese = any('\u4e00' <= c <= '\u9fff' for c in query)
        
        if budget_mode:
            # 成本优先模式:优先 DeepSeek
            if is_chinese and len(query) < 500:
                return await self.chat_completion(ModelType.DEEPSEEK_V3, messages)
            return await self.chat_completion(ModelType.GEMINI_FLASH, messages)
        
        if is_complex:
            # 复杂推理:Claude
            return await self.chat_completion(ModelType.CLAUDE_SONNET, messages)
        elif is_batch:
            # 批量处理:Gemini Flash
            return await self.chat_completion(ModelType.GEMINI_FLASH, messages)
        else:
            # 通用场景:GPT-4.1
            return await self.chat_completion(ModelType.GPT_41, messages)
    
    def get_metrics(self) -> Dict[str, Any]:
        """获取当前指标"""
        avg_latency = self.metrics.total_latency_ms / self.metrics.total_requests \
                      if self.metrics.total_requests > 0 else 0
        success_rate = self.metrics.successful_requests / self.metrics.total_requests * 100 \
                       if self.metrics.total_requests > 0 else 0
        
        return {
            "total_requests": self.metrics.total_requests,
            "success_rate": f"{success_rate:.2f}%",
            "avg_latency_ms": f"{avg_latency:.0f}",
            "total_tokens": self.metrics.total_tokens,
            "total_cost_usd": f"${self.metrics.total_cost_usd:.2f}",
            "circuit_state": self.circuit_breaker.state.value
        }
    
    async def close(self):
        await self._client.aclose()

使用示例

async def main(): client = EnterpriseLLMClient() messages = [ {"role": "system", "content": "你是一个专业的技术顾问"}, {"role": "user", "content": "请帮我分析这段代码的性能瓶颈并提供优化建议"} ] try: # 使用智能路由 result = await client.smart_route( query="请帮我分析这段代码的性能瓶颈", messages=messages, budget_mode=False ) print(f"响应: {result['choices'][0]['message']['content']}") # 获取指标 metrics = client.get_metrics() print(f"\n=== 运行时指标 ===") print(f"成功率: {metrics['success_rate']}") print(f"平均延迟: {metrics['avg_latency_ms']}ms") print(f"总成本: {metrics['total_cost_usd']}") print(f"熔断器状态: {metrics['circuit_state']}") finally: await client.close() if __name__ == "__main__": asyncio.run(main())

2.3 多实例部署与负载均衡

# load_balancer.py - 高级负载均衡器
import asyncio
import random
from typing import List, Callable, Awaitable
from dataclasses import dataclass
import time
import logging

logger = logging.getLogger(__name__)

@dataclass
class InstanceStats:
    """实例统计数据"""
    instance_id: str
    healthy: bool = True
    consecutive_failures: int = 0
    last_success_time: float = 0
    avg_latency: float = 0
    total_requests: int = 0
    weight: int = 100

class WeightedLoadBalancer:
    """加权负载均衡器,支持健康检查"""
    
    def __init__(self, health_check_interval: int = 30):
        self.instances: List[InstanceStats] = []
        self.health_check_interval = health_check_interval
        self._running = False
    
    def add_instance(self, instance_id: str, weight: int = 100):
        """添加后端实例"""
        self.instances.append(InstanceStats(instance_id=instance_id, weight=weight))
        logger.info(f"添加实例: {instance_id} (权重: {weight})")
    
    def remove_instance(self, instance_id: str):
        """移除后端实例"""
        self.instances = [i for i in self.instances if i.instance_id != instance_id]
        logger.warning(f"移除实例: {instance_id}")
    
    async def health_check(self, check_func: Callable[[str], Awaitable[bool]]):
        """健康检查任务"""
        self._running = True
        while self._running:
            for instance in self.instances:
                try:
                    is_healthy = await asyncio.wait_for(
                        check_func(instance.instance_id),
                        timeout=5.0
                    )
                    
                    if is_healthy:
                        instance.healthy = True
                        instance.consecutive_failures = 0
                    else:
                        instance.consecutive_failures += 1
                        if instance.consecutive_failures >= 3:
                            instance.healthy = False
                            logger.warning(f"实例 {instance.instance_id} 健康检查失败")
                except Exception as e:
                    instance.consecutive_failures += 1
                    if instance.consecutive_failures >= 3:
                        instance.healthy = False
                        logger.error(f"实例 {instance.instance_id} 健康检查异常: {e}")
            
            await asyncio.sleep(self.health_check_interval)
    
    def select_instance(self) -> str:
        """加权随机选择实例"""
        healthy_instances = [i for i in self.instances if i.healthy]
        
        if not healthy_instances:
            # 无健康实例,返回最后一个(触发快速失败)
            if self.instances:
                return self.instances[-1].instance_id
            raise Exception("无可用后端实例")
        
        # 加权随机
        weights = [i.weight for i in healthy_instances]
        total_weight = sum(weights)
        rand_val = random.uniform(0, total_weight)
        
        cumulative = 0
        for instance in healthy_instances:
            cumulative += instance.weight
            if rand_val <= cumulative:
                instance.total_requests += 1
                return instance.instance_id
        
        return healthy_instances[-1].instance_id
    
    def record_success(self, instance_id: str, latency_ms: float):
        """记录成功调用"""
        for instance in self.instances:
            if instance.instance_id == instance_id:
                instance.consecutive_failures = 0
                instance.last_success_time = time.time()
                # 滑动平均延迟
                instance.avg_latency = instance.avg_latency * 0.7 + latency_ms * 0.3
                # 动态调整权重:延迟越低权重越高
                if instance.avg_latency < 100:
                    instance.weight = min(150, instance.weight + 5)
                elif instance.avg_latency > 500:
                    instance.weight = max(20, instance.weight - 10)
                break
    
    def record_failure(self, instance_id: str):
        """记录失败调用"""
        for instance in self.instances:
            if instance.instance_id == instance_id:
                instance.consecutive_failures += 1
                instance.weight = max(10, instance.weight - 20)
                if instance.consecutive_failures >= 3:
                    instance.healthy = False
                    logger.error(f"实例 {instance_id} 连续失败3次,标记为不健康")
                break
    
    def get_stats(self) -> List[dict]:
        """获取所有实例状态"""
        return [
            {
                "instance_id": i.instance_id,
                "healthy": i.healthy,
                "avg_latency_ms": f"{i.avg_latency:.0f}",
                "total_requests": i.total_requests,
                "weight": i.weight
            }
            for i in self.instances
        ]

使用示例

async def health_check_mock(instance_url: str) -> bool: """模拟健康检查""" await asyncio.sleep(0.1) return random.random() > 0.1 # 90% 健康率 async def demo(): lb = WeightedLoadBalancer() # 添加实例 lb.add_instance("holydsheep-prod-1", weight=100) lb.add_instance("holysheep-prod-2", weight=80) lb.add_instance("holysheep-prod-3", weight=60) # 启动健康检查(后台任务) check_task = asyncio.create_task(lb.health_check(health_check_mock)) # 模拟请求 for i in range(20): selected = lb.select_instance() print(f"请求 {i+1}: 路由到 {selected}") await asyncio.sleep(0.1) # 输出统计 print("\n=== 负载均衡统计 ===") for stat in lb.get_stats(): print(stat) if __name__ == "__main__": asyncio.run(demo())

三、HolySheep API 高级特性与成本优化

3.1 批量处理与 Token 节省策略

在我经手的多个项目中,发现很多团队没有充分利用批量处理和上下文压缩能力。以一个日调用量 10 万次的企业为例,通过 HolySheep 的统一 API 接入,配合批量接口和智能缓存,可以将月成本从预估的 $3,000 降至 $800 以内。

# batch_processor.py - 批量处理与成本优化
import asyncio
import json
from typing import List, Dict, Any
from collections import defaultdict
import hashlib

class BatchOptimizer:
    """批量处理优化器 - 显著降低 API 调用成本"""
    
    def __init__(self, llm_client):
        self.client = llm_client
        self.cache = {}  # 简单内存缓存
        self.batch_queue: List[Dict] = []
        self.batch_size = 20
        self.batch_timeout = 5.0  # 5秒内收集满批次
    
    def generate_cache_key(self, messages: List[Dict]) -> str:
        """生成缓存键"""
        content = json.dumps(messages, sort_keys=True)
        return hashlib.sha256(content.encode()).hexdigest()[:16]
    
    async def cached_completion(self, messages: List[Dict], model) -> Dict:
        """带缓存的补全请求"""
        cache_key = self.generate_cache_key(messages)
        
        if cache_key in self.cache:
            cached_result = self.cache[cache_key].copy()
            cached_result["cached"] = True
            return cached_result
        
        result = await self.client.chat_completion(model, messages)
        self.cache[cache_key] = result
        return result
    
    async def batch_process(
        self, 
        requests: List[Dict],
        model,
        max_concurrent: int = 5
    ) -> List[Dict]:
        """批量处理 - 使用信号量控制并发"""
        semaphore = asyncio.Semaphore(max_concurrent)
        
        async def process_single(req: Dict) -> Dict:
            async with semaphore:
                try:
                    result = await self.cached_completion(
                        req["messages"], 
                        model
                    )
                    return {"success": True, "data": result, "index": req["index"]}
                except Exception as e:
                    return {"success": False, "error": str(e), "index": req["index"]}
        
        # 并发执行
        tasks = [process_single(req) for req in requests]
        results = await asyncio.gather(*tasks)
        
        # 按原始顺序排序
        return sorted(results, key=lambda x: x["index"])
    
    def estimate_cost(
        self, 
        requests: List[List[Dict]], 
        model: str
    ) -> Dict[str, float]:
        """成本估算"""
        # 简化估算(实际以 API 返回的 usage 为准)
        pricing = {
            "claude-sonnet-4-5": {"input": 3.75, "output": 15.00},
            "gpt-4.1": {"input": 2.00, "output": 8.00},
            "gemini-2.5-flash": {"input": 0.30, "output": 2.50},
            "deepseek-v3.2": {"input": 0.10, "output": 0.42},
        }
        
        if model not in pricing:
            return {"error": "未知模型"}
        
        price = pricing[model]
        total_input_tokens = 0
        total_output_tokens = 0
        
        for req in requests:
            # 估算(简化版)
            for msg in req:
                content = msg.get("content", "")
                if isinstance(content, str):
                    total_input_tokens += len(content) // 4  # 粗略估算
            total_output_tokens += 500  # 平均输出估算
        
        input_cost = (total_input_tokens / 1_000_000) * price["input"]
        output_cost = (total_output_tokens / 1_000_000) * price["output"]
        
        return {
            "estimated_input_tokens": total_input_tokens,
            "estimated_output_tokens": total_output_tokens,
            "estimated_input_cost_usd": f"${input_cost:.4f}",
            "estimated_output_cost_usd": f"${output_cost:.4f}",
            "estimated_total_usd": f"${input_cost + output_cost:.4f}",
            "savings_with_cache": f"${(input_cost + output_cost) * 0.3:.4f}"  # 假设30%缓存命中
        }

使用示例

async def cost_optimization_demo(): """成本优化演示""" # 模拟请求数据 requests = [ {"index": i, "messages": [ {"role": "user", "content": f"第{i}个问题,关于Python异步编程"} ]} for i in range(100) ] # 成本估算 optimizer = BatchOptimizer(None) # 实际使用时传入 client estimate = optimizer.estimate_cost( [r["messages"] for r in requests], "deepseek-v3.2" # 使用最便宜的模型 ) print("=== 成本优化分析 ===") print(f"请求数量: {len(requests)}") print(f"估算输入Token: {estimate['estimated_input_tokens']:,}") print(f"估算输出Token: {estimate['estimated_output_tokens']:,}") print(f"总成本: {estimate['estimated_total_usd']}") print(f"使用缓存可节省: {estimate['savings_with_cache']}") if __name__ == "__main__": asyncio.run(cost_optimization_demo())

3.2 监控告警体系

# monitor.py - 企业级监控告警
import asyncio
import time
from dataclasses import dataclass, asdict
from typing import Optional, Callable
import logging

logger = logging.getLogger(__name__)

@dataclass
class AlertThresholds:
    """告警阈值配置"""
    latency_p99_ms: int = 2000      # P99延迟超过2秒告警
    error_rate_percent: float = 5.0  # 错误率超过5%告警
    circuit_breaker_open: bool = True  # 熔断器开启告警
    cost_per_hour_usd: float = 100.0  # 每小时成本超过$100告警

@dataclass
class SystemMetrics:
    """系统指标快照"""
    timestamp: float
    total_requests: int
    successful_requests: int
    failed_requests: int
    avg_latency_ms: float
    p99_latency_ms: float
    total_cost_usd: float
    cost_rate_usd_per_hour: float
    circuit_state: str
    cache_hit_rate: float

class MonitoringDashboard:
    """监控仪表板"""
    
    def __init__(
        self,
        thresholds: Optional[AlertThresholds] = None,
        alert_callback: Optional[Callable] = None
    ):
        self.thresholds = thresholds or AlertThresholds()
        self.alert_callback = alert_callback
        self.metrics_history: list = []
        self.max_history_size = 1000
        
        # 实时统计
        self.request_latencies: list = []
        self.cost_accumulator = 0.0
        self.last_cost_snapshot = time.time()
    
    def record_request(
        self,
        latency_ms: float,
        success: bool,
        cost_usd: float
    ):
        """记录单个请求"""
        self.request_latencies.append(latency_ms)
        self.cost_accumulator += cost_usd
        
        # 保持窗口大小
        if len(self.request_latencies) > 10000:
            self.request_latencies = self.request_latencies[-5000:]
    
    def get_snapshot(self, circuit_state: str) -> SystemMetrics:
        """获取当前指标快照"""
        now = time.time()
        elapsed_hours = (now - self.last_cost_snapshot) / 3600
        
        total = len(self.request_latencies)
        if total > 0:
            sorted_latencies = sorted(self.request_latencies)
            p99_index = int(total * 0.99)
            p99_latency = sorted_latencies[p99_index] if p99_index < total else 0
            avg_latency = sum(self.request_latencies) / total
        else:
            p99_latency = 0
            avg_latency = 0
        
        cost_rate = self.cost_accumulator / max(elapsed_hours, 0.001)
        
        snapshot = SystemMetrics(
            timestamp=now,
            total_requests=total,
            successful_requests=len([l for l in self.request_latencies]),
            failed_requests=0,
            avg_latency_ms=avg_latency,
            p99_latency_ms=p99_latency,
            total_cost_usd=self.cost_accumulator,
            cost_rate_usd_per_hour=cost_rate,
            circuit_state=circuit_state,
            cache_hit_rate=0.0  # 需要从业务层传入
        )
        
        self.metrics_history.append(snapshot)
        if len(self.metrics_history) > self.max_history_size:
            self.metrics_history = self.metrics_history[-self.max_history_size:]
        
        return snapshot
    
    async def check_alerts(self, metrics: SystemMetrics):
        """检查告警条件"""
        alerts = []
        
        if metrics.p99_latency_ms > self.thresholds.latency_p99_ms:
            alerts.append(f"⚠️ P99延迟过高: {metrics.p99_latency_ms:.0f}ms > {self.thresholds.latency_p99_ms}ms")
        
        if metrics.cost_rate_usd_per_hour > self.thresholds.cost_per_hour_usd:
            alerts.append(f"💰 成本速率过高: ${metrics.cost_rate_usd_per_hour:.2f}/h > ${self.thresholds.cost_per_hour_usd}/h")
        
        if self.thresholds.circuit_breaker_open and metrics.circuit_state == "open":
            alerts.append(f"🔴 熔断器已开启,服务可能不可用")
        
        if alerts and self.alert_callback:
            for alert in alerts:
                logger.error(f"[ALERT] {alert}")
                await self.alert_callback(alert)
    
    def print_dashboard(self, metrics: SystemMetrics):
        """打印仪表板"""
        print(f"""
╔══════════════════════════════════════════════════════════╗
║              AI API 监控仪表板                          ║
╠══════════════════════════════════════════════════════════�