[litellm] ECONNREFUSEDfetch failed {"message":"fetch failed","code":"ECONNREFUSE...

2026年07月28日 16:48 27.27 秒 success

错误信息

[litellm] ECONNREFUSEDfetch failed {"message":"fetch failed","code":"ECONNREFUSED","modelId":"anthropic/claude-3-7-sonnet-20250219","providerId":"litellm"} (Click "Retry" below)

AI 解决方案

🔍 报错分析

1. 🌐 错误翻译

原文:[litellm] ECONNREFUSEDfetch failed
{"message":"fetch failed","code":"ECONNREFUSED","modelId":"anthropic/claude-3-7-sonnet-20250219","providerId":"litellm"}

(Click "Retry" below)
译文:[litellm] 连接被拒绝,请求失败
{"message":"请求失败","code":"连接被拒绝","modelId":"anthropic/claude-3-7-sonnet-20250219","providerId":"litellm"}

(点击下方的“重试”)

2. 🎯 原因分析

  • 根本原因:客户端尝试连接到 litellm 服务时被拒绝,属于典型的网络连接拒绝错误(ECONNREFUSED)

  • 错误位置:发生在向 litellm 代理服务发起 HTTP 请求时

  • 上下文:系统尝试通过 litellm 代理访问 Anthropic 的 Claude 模型,但 litellm 服务未启动或未正确配置,导致无法建立连接

3. 💡 解决方案

主要解决方向:确保 litellm 服务已正确启动并可访问

# 1. 检查并启动 litellm 服务
litellm --host 0.0.0.0 --port 4000

# 2. 或检查服务是否已在运行,确认端口占用情况
netstat -an | grep 4000

# 3. 确认环境变量配置正确(如 API Key)
export LITELLM_API_KEY="your-key"
export ANTHROPIC_API_KEY="your-anthropic-key"

如果使用容器部署:

# docker-compose.yml 示例
version: '3'
services:
  litellm:
    image: proxylayer/litellm
    ports:
      - "4000:4000"
    environment:
      - LITELLM_API_KEY=your-key
      - ANTHROPIC_API_KEY=your-anthropic-api-key

4. 🛡️ 预防措施

  • 服务健康检查:在应用启动前,先对 litellm 服务进行健康检查,确认服务可用后再发起请求

  •   // 添加健康检查
      async function checkServiceHealth() {
        try {
          await fetch('http://localhost:4000/health');
          return true;
        } catch (error) {
          return false;
        }
      }
      

    • 容器化部署监控:使用 Docker 健康检查或 Kubernetes liveness/readiness probes,自动监控服务状态
    • 配置验证:使用 litellm --check-config 验证配置文件和环境变量是否正确
    • 日志监控:配置 litellm 服务日志,便于快速定位服务启动失败、配置错误等问题