﻿---
title: "通过 LiteLLM 连接模型服务"
summary: "用户 config 写 [model_providers.litellm]，base_url 是 http://localhost:4000/v1，env_key = LITELLM_API_KEY，wire_api = responses。再用 ~/.codex/litellm.config.toml 和 --profile litellm。"
category: config
level: intermediate
surfaces: [cli, app]
tags: ["model_providers", "LiteLLM", "wire_api", "profile"]
canonical: /tips/litellm-codex-gateway/
---

# 通过 LiteLLM 连接模型服务

用户 config 写 [model_providers.litellm]，base_url 是 http://localhost:4000/v1，env_key = LITELLM_API_KEY，wire_api = responses。再用 ~/.codex/litellm.config.toml 和 --profile litellm。

LiteLLM 官方 Codex 网关：用户层 [model_providers.litellm]，base_url 是 http://localhost:4000/v1，env_key 读 LITELLM_API_KEY。

这是换 Codex **使用的模型**，不是添加 MCP 服务。需要 LiteLLM v1.66.3.dev5 以上。先把 proxy 起在 4000 端口；yaml 必须有 `litellm_settings.drop_params: true`，否则多余参数会导致上游请求失败。

起代理（本机或 Docker 二选一）：

```bash
litellm --config /path/to/litellm_config.yaml
```

```bash
docker run \
  -v "$(pwd)/litellm_config.yaml:/app/config.yaml" \
  -p 4000:4000 \
  docker.litellm.ai/berriai/litellm:latest \
  --config /app/config.yaml
```

yaml 骨架（模型名按你的 proxy 目录改，不要把示例密钥写进文件）：

```yaml
model_list:
  - model_name: gpt-5.6-terra
    litellm_params:
      model: openai/gpt-5.6-terra
      api_key: os.environ/OPENAI_API_KEY
litellm_settings:
  drop_params: true
```

供应商表放**用户** `~/.codex/config.toml`。官方教程会把顶层 `model` / `model_provider` 写成全局默认，不要一上来覆盖所有会话：

```toml
# ~/.codex/config.toml
[model_providers.litellm]
name = "litellm"
base_url = "http://localhost:4000/v1"
env_key = "LITELLM_API_KEY"
wire_api = "responses"
stream_idle_timeout_ms = 7200000
```

`env_key` 是变量**名**。密钥必须出现在**启动 Codex 的那个进程**里。不要把 `sk-1234` 写进 TOML。从已经 `export LITELLM_API_KEY` 的终端启动；Dock / Finder 打开的桌面不会读你刚改的 zshrc。官方 macOS 补救是 `launchctl setenv LITELLM_API_KEY …` 后重启应用，或从终端拉起桌面。

更稳妥是独立 profile，不要改成全局默认：

```toml
# ~/.codex/litellm.config.toml
model_provider = "litellm"
model = "gpt-5.6-terra"
```

```bash
codex --profile litellm
codex --profile litellm -m claude-sonnet-5
```

0.134 起不要再写 `[profiles.litellm]`。自定义供应商必须 `wire_api = "responses"`。模型名以 LiteLLM `/v1/models` 为准，例如官方示例 `gpt-5.6-terra`、`claude-sonnet-5`。

`lite codex` 是包装器，不是持久配置。它会 export `OPENAI_BASE_URL`（Codex **忽略** 这个变量）再用 `-c` 覆盖走 HTTP/SSE Responses，因为代理不支持 Responses WebSocket。包装器读 `LITELLM_PROXY_API_KEY` / `LITELLM_PROXY_URL`；Codex 表读的是 `LITELLM_API_KEY`。两种配置应分别使用。不要抄 `lite claude` 的 `ANTHROPIC_BASE_URL`。

配置说明：

- 不要把 `openai_base_url` / `OPENAI_BASE_URL` 当成 Codex 路径。Codex 不读 `OPENAI_BASE_URL`；抄完会去 GET `/responses` 拿到 405。
- 不要写进项目 `.codex/config.toml`。项目文件无法修改 `model_provider` / `model_providers`。
- 不要覆盖内置 ID `openai`、`ollama`、`lmstudio`。`litellm` 是新 ID，可以。
- 不要开 `supports_websockets = true`，除非代理真的讲 Responses WS。
- 不要把官方示例里的 `approvals_reviewer`、`[tui.model_availability_nux]`、项目 `trust_level` 当成这张网关表的必填项。
- 不要把密钥写进 `http_headers`。

桌面读同一份 `~/.codex/config.toml`。自定义供应商没有应用内模型选择器（openai/codex#15364）；改 `model` 后必须**新开会话**。改完用 `codex --profile litellm` 启动后，发送一条简短请求验证连接。连不上先看 4000 端口；401 先看进程里有没有密钥。

## 来源

- [LiteLLM · OpenAI Codex](https://docs.litellm.ai/docs/tutorials/openai_codex)
- [LiteLLM · Proxy CLI](https://docs.litellm.ai/docs/proxy/management_cli)
- [BerriAI/litellm](https://github.com/BerriAI/litellm)
