> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mixroute.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# LiteLLM

> 使用 Docker Compose 部署 LiteLLM，並透過 MixRoute 轉發模型請求

## 概覽

LiteLLM 可以作為內部代理部署在 MixRoute 前面。業務應用程式呼叫 LiteLLM，LiteLLM 再將請求轉發到 MixRoute，同時提供統一鑑權、模型發現、Dashboard 管理、virtual keys 和用量記錄。

LiteLLM 官方 Docker Compose 安裝教學：[https://docs.litellm.ai/docs/proxy/deploy](https://docs.litellm.ai/docs/proxy/deploy)

## Docker Compose 部署

準備三個檔案：

```text theme={null}
.
├── .env
├── config.yaml
└── docker-compose.yml
```

`.env`：

```env theme={null}
MIXROUTE_API_KEY=<mixroute-api-key>
LITELLM_MASTER_KEY=<litellm-master-key>
LITELLM_SALT_KEY=<stable-random-salt-key>
POSTGRES_PASSWORD=<postgres-password>
```

| 變數                   | 說明                                          |
| -------------------- | ------------------------------------------- |
| `MIXROUTE_API_KEY`   | MixRoute API key。LiteLLM 轉發請求到 MixRoute 時使用 |
| `LITELLM_MASTER_KEY` | LiteLLM 管理員 key，可用於呼叫 API 和登入 Dashboard     |
| `LITELLM_SALT_KEY`   | LiteLLM 用於加密資料庫中 key 的穩定隨機值                 |
| `POSTGRES_PASSWORD`  | Docker Compose 內部 PostgreSQL 的資料庫密碼         |

`docker-compose.yml`：

```yaml theme={null}
services:
  postgres:
    image: postgres:17
    container_name: litellm-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: litellm
      POSTGRES_USER: litellm
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U litellm -d litellm"]
      interval: 5s
      timeout: 5s
      retries: 20
    volumes:
      - litellm-postgres-data:/var/lib/postgresql/data

  litellm:
    image: ghcr.io/berriai/litellm-database:main-stable
    container_name: litellm
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - .env
    environment:
      DATABASE_URL: postgresql://litellm:${POSTGRES_PASSWORD}@postgres:5432/litellm
      USE_PRISMA_MIGRATE: "True"
      STORE_MODEL_IN_DB: "True"
    command: ["--config", "/app/config.yaml", "--host", "0.0.0.0", "--port", "4000"]
    ports:
      - "4000:4000"
    volumes:
      - ./config.yaml:/app/config.yaml:ro

volumes:
  litellm-postgres-data:
```

| 變數                   | 說明                                                                 |
| -------------------- | ------------------------------------------------------------------ |
| `DATABASE_URL`       | LiteLLM 連接 PostgreSQL 的連線字串，用於 Dashboard、virtual keys、用量記錄和模型設定持久化 |
| `USE_PRISMA_MIGRATE` | 設為 `True` 時，LiteLLM 啟動時會執行資料庫 migration                            |
| `STORE_MODEL_IN_DB`  | 設為 `True` 時，Dashboard/API 中新增或修改的模型設定會寫入 PostgreSQL                |

啟動 LiteLLM：

```bash theme={null}
docker compose pull
docker compose up -d
```

## 接入 MixRoute

MixRoute Base URL：

```text theme={null}
https://api.mixroute.ai/v1
```

在 `config.yaml` 中設定 LiteLLM 路由：

```yaml theme={null}
model_list:
  - model_name: o3-mini
    litellm_params:
      model: openai/o3-mini-2025-01-31
      api_base: https://api.mixroute.ai/v1
      api_key: os.environ/MIXROUTE_API_KEY
    model_info:
      mode: chat

  - model_name: openai/*
    litellm_params:
      model: openai/*
      api_base: https://api.mixroute.ai/v1
      api_key: os.environ/MIXROUTE_API_KEY

  - model_name: anthropic/*
    litellm_params:
      model: anthropic/*
      api_base: https://api.mixroute.ai
      api_key: os.environ/MIXROUTE_API_KEY

  - model_name: gemini/*
    litellm_params:
      model: gemini/*
      api_base: https://api.mixroute.ai/v1
      api_key: os.environ/MIXROUTE_API_KEY

litellm_settings:
  drop_params: true
  request_timeout: 600
  telemetry: false
  check_provider_endpoint: true

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  database_url: os.environ/DATABASE_URL
  store_model_in_db: true
```

| 設定                              | 作用                                                                 |
| ------------------------------- | ------------------------------------------------------------------ |
| `openai/*`                      | 接入 MixRoute OpenAI-compatible `/v1`，用於文字、embedding、圖片、影片等介面        |
| `anthropic/*`                   | 接入 MixRoute Anthropic-compatible API，LiteLLM 會處理 `/v1/messages` 路徑 |
| `gemini/*`                      | 接入 MixRoute Gemini-compatible API                                  |
| `check_provider_endpoint: true` | 讓 LiteLLM 透過 MixRoute `/v1/models` 自動發現模型                          |
| `drop_params: true`             | 自動丟棄上游不支援的參數，減少跨廠商相容性問題                                            |
| `store_model_in_db: true`       | Dashboard/API 中新增或修改的模型會儲存到 PostgreSQL                             |
| `o3-mini`                       | 範例 alias，將短模型名映射到 MixRoute 實際模型名                                   |

`openai/` 是 LiteLLM 的 OpenAI-compatible provider 前綴，不代表模型來源一定是 OpenAI。

## 客戶端呼叫方式

OpenAI-compatible Base URL：

```text theme={null}
<LITELLM_OPENAI_BASE_URL>
```

請求標頭：

```http theme={null}
Authorization: Bearer <LITELLM_API_KEY>
```

取得模型列表：

```bash theme={null}
curl -sS <LITELLM_OPENAI_BASE_URL>/models \
  -H "Authorization: Bearer <LITELLM_API_KEY>"
```

呼叫時使用回傳的模型 ID，例如 `openai/gpt-5.5`、`anthropic/claude-sonnet-5`、`gemini/gemini-2.5-flash`，或明確設定的 alias `o3-mini`。

## 驗證

```bash theme={null}
curl -sS <LITELLM_BASE_URL>/health/liveliness
```

```bash theme={null}
curl -sS <LITELLM_OPENAI_BASE_URL>/chat/completions \
  -H "Authorization: Bearer <LITELLM_API_KEY>" \
  -H "Content-Type: application/json" \
  --data '{
    "model": "openai/gpt-5.5",
    "messages": [{"role": "user", "content": "Return exactly OK"}],
    "max_tokens": 8
  }'
```

```bash theme={null}
curl -sS <LITELLM_BASE_URL>/v1beta/models/gemini/gemini-2.5-flash:generateContent \
  -H "Authorization: Bearer <LITELLM_API_KEY>" \
  -H "Content-Type: application/json" \
  --data '{
    "contents": [{"role": "user", "parts": [{"text": "Return exactly OK"}]}],
    "generationConfig": {"maxOutputTokens": 8}
  }'
```

Gemini `generateContent` 使用 LiteLLM 前端路徑 `/v1beta/models/gemini/<model-id>:generateContent`；轉發到 MixRoute 時，上游 Base URL 仍為 `https://api.mixroute.ai/v1`。
