LiteLLM

BerriAI/litellm 访问 GitHub ↗
🔤 Python ★ 60658 ⑂ 0 daily #7 (+95) 抓取 2026-10-10

一句话简介

统一调用100+大语言模型的Python SDK,支持OpenAI兼容接口、负载均衡、故障转移与成本追踪。

标签

  • 大语言模型
  • Python SDK
  • API网关
  • LLM编排
  • 成本监控

适用应用场景

  • 统一封装OpenAI、Anthropic、Cohere等多模型API调用
  • 在生产环境中实现多模型负载均衡与自动故障转移
  • 统计和追踪各模型调用成本与用量
  • 为AI应用构建可切换模型供应商的统一接口层

README 中文摘要

LiteLLM 概述

LiteLLM 是一个开源的 AI 网关(AI Gateway),通过 OpenAI 兼容格式提供统一的接口,可调用 100 多家 LLM 提供商,包括 OpenAI、Anthropic、Gemini、Bedrock、Azure 等。它既可作为 Python SDK 直接集成到代码中,也可部署为 AI 网关(代理服务器)作为团队或组织的中心化服务。

解决的痛点

在不同提供商之间管理 LLM 调用会迅速变得繁琐——每个模型都有不同的 SDK、鉴权模式、请求格式和错误类型。LiteLLM 通过统一接口消除了这些摩擦:

  • 统一 API:单一接口对接 100 多个 LLM,无需在各提供商的 SDK 间切换
  • OpenAI 兼容:可替换底层提供商而无需重写业务代码
  • 生产级网关:内置虚拟密钥、费用统计、Guardrails、负载均衡与管理控制台
  • 低延迟:在 1k RPS 压力下 P95 延迟仅 8 毫秒

Python SDK 用法

安装:

uv add litellm

调用示例:

from litellm import completion
import os

os.environ["OPENAI_API_KEY"] = "your-openai-key"
os.environ["ANTHROPIC_API_KEY"] = "your-anthropic-key"

# OpenAI
response = completion(model="openai/gpt-4o", messages=[{"role": "user", "content": "Hello!"}])

# Anthropic
response = completion(model="anthropic/claude-sonnet-4-20250514", messages=[{"role": "user", "content": "Hello!"}])

LiteLLM 同时提供独立的 litellm-core 发行版,仅包含 Python SDK 与运行时依赖,不附带 CLI、控制台和可选扩展。每个环境应只安装其中一个发行版。

AI 网关(代理服务器)用法

安装并启动代理:

uv tool install 'litellm[proxy]'
litellm --model gpt-4o

通过 OpenAI 兼容方式调用:

import openai

client = openai.OpenAI(api_key="anything", base_url="http://0.0.0.0:4000")
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)

MCP 工具集成

Python SDK 中转 MCP

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from litellm import experimental_mcp_client
import litellm

server_params = StdioServerParameters(command="python", args=["mcp_server.py"])

async with stdio_client(server_params) as (read, write):
    async with ClientSession(read, write) as session:
        await session.initialize()
        tools = await experimental_mcp_client.load_mcp_tools(session=session, format="openai")
        response = await litellm.acompletion(
            model="gpt-4o",
            messages=[{"role": "user", "content": "What's 3 + 5?"}],
            tools=tools
        )

网关形式调用 MCP

curl -X POST 'http://0.0.0.0:4000/v1/chat/completions' \
  -H 'Authorization: Bearer <your-master-key>' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Summarize the latest open PR"}],
    "tools": [{
      "type": "mcp",
      "server_url": "litellm_proxy/mcp/github",
      "server_label": "github_mcp",
      "require_approval": "never"
    }]
  }'

A2A 智能体网关

通过 A2A 协议调用 LangGraph、Vertex AI Agent Engine、Azure AI Foundry、Bedrock AgentCore、Pydantic AI 等平台的智能体。网关路径形如 http://localhost:4000/a2a/<agent-name>,使用主密钥或虚拟密钥鉴权。

智能体执行框架

可在任意模型上运行 Claude Code、Codex、OpenCode 或 Deep Agents:

import litellm
from litellm import Harness, sandbox

result = litellm.agent(
    Harness.CLAUDE_CODE,
    "Find why tests/test_router.py is flaky and fix it.",
    sandbox=sandbox.local("./repo"),
    model="litellm_proxy/claude-sonnet-4-5",
)

print(result.text, result.cost, [f.path for f in result.files])

设置 LITELLM_PROXY_API_BASE 和 LITELLM_PROXY_API_KEY 后,智能体的所有模型调用都会经过 AI 网关,并打上 harness,claude_code 标签。

部署

支持一键部署到 Render、Railway、AWS、GCP。生产级推荐使用 Terraform 模块(AWS ECS Fargate + Aurora + ElastiCache + ALB,或 GCP 对应组件),模块已在公共 Terraform Registry 发布。Stable 版本通过带 -stable 标签的 Docker 镜像发布,需经过 12 小时负载测试。

摘要更新于 2026-10-10 00:30:37 · 原文 37502 字符 · md5 8e87faa8641e…