[model-pricing] LiteLLM pricing fetch failed [model-pricing] OpenRouter pricing ...

2026年04月29日 12:02 processing

错误信息

[model-pricing] LiteLLM pricing fetch failed [model-pricing] OpenRouter pricing fetch failed

AI 解决方案

报错分析报告

1.错误翻译


原文:[model-pricing] LiteLLM pricing fetch failed
译文:[模型定价] LiteLLM 定价获取失败

原文:[model-pricing] OpenRouter pricing fetch failed  
译文:[模型定价] OpenRouter 定价获取失败

2. 原因分析


  • 根本原因:程序调用LiteLLM/OpenRouter定价API时未成功获取数据

  • 发生位置:模型定价模块的API请求逻辑中

  • 可能诱因:网络连接故障、服务不可用、API密钥失效、接口变更或超时
  • 3. 解决方案


    import requests
    import os
    from tenacity import retry, stop_after_attempt, wait_exponential
    
    @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
    def fetch_provider_pricing(provider):
        api_urls = {
            "litellm": "https://api.litellm.com/pricing",
            "openrouter": "https://openrouter.ai/api/v1/pricing"
        }
        
        headers = {"Authorization": f"Bearer {os.getenv(f'{provider.upper()}_API_KEY')}"}
        response = requests.get(api_urls[provider], headers=headers, timeout=10)
        response.raise_for_status()
        return response.json()
    
    # 使用示例
    try:
        litellm_pricing = fetch_provider_pricing("litellm")
        openrouter_pricing = fetch_provider_pricing("openrouter")
    except requests.exceptions.RequestException as e:
        print(f"定价获取失败: {e}")
        # 降级到缓存数据
        pricing_data = load_cached_pricing_fallback()
    

    4.️ 预防措施


  • 最佳实践1:对外部API调用实施指数退避重试机制,配合合理超时设置(如10秒)

  • 最佳实践2:维护本地定价缓存快照作为fallback方案,定期更新但允许离线访问
    • 推荐工具

    • - tenacity:Python重试库
      - requests:带超时控制的HTTP客户端
      - Redis/Memcached:分布式缓存存储



      分析完成 ✅ 建议优先检查网络连接和API密钥有效性。