﻿---
title: "通过 Bifrost 连接模型服务"
summary: "用户层 [model_providers.bifrost]，base_url 是 http://localhost:8080/openai/v1，env_key = OPENAI_API_KEY，wire_api = responses，supports_websockets = false。再用 ~/.codex/bifrost.config.toml 和 --profile bifrost。"
category: config
level: intermediate
surfaces: [cli, app, ide]
tags: ["model_providers", "Bifrost", "wire_api", "profile"]
canonical: /tips/bifrost-codex-gateway/
---

# 通过 Bifrost 连接模型服务

用户层 [model_providers.bifrost]，base_url 是 http://localhost:8080/openai/v1，env_key = OPENAI_API_KEY，wire_api = responses，supports_websockets = false。再用 ~/.codex/bifrost.config.toml 和 --profile bifrost。

Bifrost 官方 Codex 网关：profile 写 [model_providers.bifrost]，base_url 是 http://localhost:8080/openai/v1，env_key 读 OPENAI_API_KEY。

这是换 Codex **使用的模型**，流量打到本机 Bifrost 的 OpenAI 形 Responses 入口，不是添加 MCP 服务。Codex 会优先走 ChatGPT OAuth，配网关前先在会话里 `/logout`。`env_key` 只能写变量**名**；进程环境里的 `OPENAI_API_KEY` 填 Bifrost 虚拟密钥 `YOUR_BIFROST_VIRTUAL_KEY`，

供应商表放**用户** `~/.codex/config.toml`。官方 Codex 页会写项目 `.codex/config.toml` 也能改；那是错的。Bedrock runbook 才对：项目文件无法修改 `model_provider` / `model_providers`。不要写 `openai_base_url`：内置 `openai` 会带上客户端 `web` 命名空间，Bedrock 会报 `User-defined namespace 'web' collides with an existing tool namespace`。具名供应商才会改走托管 `web_search`。

更稳妥是独立 profile（用户层 `$CODEX_HOME`，不是项目 `.codex`）：

```toml
# ~/.codex/bifrost.config.toml
model = "openai/gpt-5.4"
model_provider = "bifrost"

[model_providers.bifrost]
name = "Bifrost"
base_url = "http://localhost:8080/openai/v1"
env_key = "OPENAI_API_KEY"
wire_api = "responses"
supports_websockets = false
```

```bash
export OPENAI_API_KEY="YOUR_BIFROST_VIRTUAL_KEY"
codex --profile bifrost
codex --profile bifrost -m openai/gpt-5-codex
```

0.134 起不要再写 `[profiles.bifrost]`。自定义供应商必须 `wire_api = "responses"`。非 OpenAI 模型还要 `supports_websockets = false`，否则 WebSocket 路径会去要服务端维持会话。密钥必须出现在**启动 Codex 的那个进程**里。从已经 export 的终端启动；Dock 打开的桌面读不到你刚改的 zshrc。

非 OpenAI 模型 slug 用 `厂商/型号`，例如 `anthropic/claude-sonnet-4-5-20250929`、`gemini/gemini-2.5-pro`、`bedrock/gpt-5.5`。`/model` 选择器默认看不到这些 slug，会警告 metadata 找不到。把 `~/.codex/models_cache.json` 里一条完整条目拷进 `YOUR_HOME/.codex/bifrost_catalog.json`，只改 `slug` / `display_name` / `context_window`，再在用户 config 写 `model_catalog_json = "YOUR_HOME/.codex/bifrost_catalog.json"`。桌面下拉菜单**不会**合并这份本地目录，会话里用 `/model bedrock/...`。不要把整份目录 JSON 贴进手册。

托管 Bedrock 那张表是另一回事：ID 是 `bifrost_bedrock`，`env_key = "BIFROST_API_KEY"`，`base_url` 才换成 `https://gateway.example.com/openai/v1`。不要和本机 `[model_providers.bifrost]` 混抄。备选 `[model_providers.openai_http]` 也要 `supports_websockets = false`，同样不要覆盖内置 `openai`。

配置说明：

- 不要写 `openai_base_url`，也不要覆盖内置 ID `openai`、`ollama`、`lmstudio`。`bifrost` 是新 ID，可以。
- 不要写进项目 `.codex/config.toml`。
- 不要把 Bifrost 自己的 `OPENAI_BASE_URL` 环境变量当 Codex 主路径。
- 不要把密钥写进 `http_headers` 或 TOML 字面量。

改完新开会话。`codex --profile bifrost` 启动后，发送一条简短请求验证连接。401 先看进程里有没有该变量。

## 来源

- [Bifrost · Codex CLI](https://docs.getbifrost.ai/cli-agents/codex-cli)
- [Bifrost · Codex + Amazon Bedrock](https://docs.getbifrost.ai/runbooks/codex-bedrock)
- [Bifrost · CLI agents overview](https://docs.getbifrost.ai/cli-agents/overview)
