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

# Use Codex with the LLM Gateway

> Point Codex CLI and Codex Desktop at the Barndoor LLM Gateway using the OpenAI Responses wire format.

OpenAI Codex (CLI and Desktop) talks to models over the **OpenAI Responses API** (`POST /v1/responses`), not Chat Completions. The Barndoor LLM Gateway speaks that wire format natively — configure Codex with a custom provider whose `base_url` is your gateway endpoint and whose `wire_api` is `responses`.

> [!NOTE]
>
> This guide covers the do-it-yourself `~/.codex/config.toml` setup. Copy your gateway base URL from [**Settings → My Models → Gateway Endpoint**](https://app.barndoor.ai/settings/models?section=endpoint) and create a `bd-…` key under [**API Keys**](https://app.barndoor.ai/settings/models?section=api-keys) first. See [Connecting Your Tools to the LLM Gateway](https://docs.barndoor.ai/how-tos/connect-tools-to-llm-gateway.md) if you don't have those yet.

## Prerequisites

* Codex CLI installed, and/or the **Codex desktop app**.
* The **OpenAI SDKs & compatible clients** URL from [**Settings → My Models → Gateway Endpoint**](https://app.barndoor.ai/settings/models?section=endpoint).
* A `bd-…` API key from [**Settings → My Models → API Keys**](https://app.barndoor.ai/settings/models?section=api-keys).
* At least one model your key is allowed to call, served by a provider that supports the Responses API. Use the exact name shown under [**Available Models**](https://app.barndoor.ai/settings/models?section=models).

## Which models Codex can use

The gateway translates between the OpenAI and Anthropic formats on Chat Completions and Messages, but not on the Responses API. A Responses request is passed to the provider in Responses format, so the model you give Codex must be served by a provider that implements it:

| Works from Codex | Doesn't work from Codex |
| - | - |
| OpenAI, Azure OpenAI (with **Azure API Mode** set to **OpenAI v1 paths**, the default), xAI, OpenAI Codex OAuth, and custom OpenAI-compatible servers that implement `/v1/responses` | Anthropic, AWS Bedrock, Google Vertex AI, Google AI |

A Model Route can mix provider types. If a route's first target is an Anthropic model, Codex requests to it fail with `provider '…' does not support endpoint /v1/responses`. Ask your admin for a route whose targets all serve Responses, or use a standalone model (`<provider name>/<model>`) from an OpenAI provider.

> [!NOTE]
>
> **Self-hosted and private-cloud deployments:** swap `app.barndoor.ai` for your organization's portal hostname everywhere below. The path (`/api/llm-gateway/v1`) is the same.

## Why `/v1` matters

Codex appends `/responses` to the provider `base_url`. The gateway's Responses endpoint is `/api/llm-gateway/v1/responses`.

| `base_url` you set | Codex actually calls | Result |
| - | - | - |
| `https://app.barndoor.ai/api/llm-gateway/v1` | `…/llm-gateway/v1/responses` | Correct |
| `https://app.barndoor.ai/api/llm-gateway` | `…/llm-gateway/responses` | Wrong — often a `401` with `invalid JWT header: InvalidToken` |

Always use the **OpenAI SDKs & compatible clients** URL from the [**LLM Gateway Endpoint**](https://app.barndoor.ai/settings/models?section=endpoint) card, which ends in `/v1`. Claude Code is the opposite: it uses the URL without `/v1`, because it appends `/v1/messages` itself.

## Configure Codex CLI

### 1. Add a Barndoor provider

Edit `~/.codex/config.toml` (create the file if it does not exist):

```toml theme={null}
model_provider = "barndoor"
model = "gpt-5.4-mini"   # replace with a name from Settings → My Models → Available Models

[model_providers.barndoor]
name = "Barndoor"
base_url = "https://app.barndoor.ai/api/llm-gateway/v1"
wire_api = "responses"
env_key = "BARNDOOR_API_KEY"
```

* `wire_api = "responses"` selects the Responses wire format Codex requires.
* `env_key` is only the **name** of the environment variable Codex should read for the API key — it is not the secret itself.
* `model` must match a name your key can call: a Model Route name like `gpt-5.4-mini`, or a standalone model in `<provider name>/<model>` form, exactly as **Available Models** lists it.

### 2. Export the gateway key

```bash theme={null}
export BARNDOOR_API_KEY="bd-…"
```

`env_key` points Codex at this variable; the `export` supplies the value. Keeping the secret in the environment (not in `config.toml`) avoids committing it to git.

Add the export to your shell profile if you want it available in every new terminal.

### 3. Run Codex

```bash theme={null}
codex
```

Traffic goes to Barndoor on `POST /v1/responses`. Budgets, rate limits, model access policies, and usage reporting apply the same way as for any other gateway client. When a budget or rate limit blocks a request, Codex shows "You've hit your usage limit. Try again at …" with the time the limit lifts, if it is known, and does not retry.

## Configure Codex Desktop

Codex Desktop uses the same provider configuration as the CLI. Custom providers are selected by `model_provider` / `model` in config — the in-app model picker may not list custom gateway models the way it lists built-in ones.

1. Put the same `model_provider`, `model`, and `[model_providers.barndoor]` block in `~/.codex/config.toml` (or in `$CODEX_HOME/config.toml` if you set `CODEX_HOME`).
2. Make `BARNDOOR_API_KEY` available to the Desktop process. Dock / Spotlight launches on macOS typically do **not** inherit exports from `.zshrc` or `.zprofile`. Prefer one of:
   * Start Desktop from a terminal after exporting the key (for example `export BARNDOOR_API_KEY="bd-…" && open -a "Codex"` — use the exact app name on your machine).
   * A small wrapper script that exports the key, then launches the app.
3. Fully quit and relaunch Desktop so it reloads `config.toml`. Routing follows the `model_provider` and `model` you set. You shouldn't need to pick "Barndoor" from a provider menu.

> [!WARNING]
>
> If Desktop starts without `BARNDOOR_API_KEY` in its environment, requests fail auth even when the same key works in the CLI. Confirm the launch path actually exports the variable — a terminal-only `export` does not help a Dock-launched app.

## ChatGPT subscriptions (OpenAI Codex OAuth providers)

The setup above works for providers with a stored credential, such as an OpenAI API key. Your admin can also add an **OpenAI Codex OAuth** provider under **LLM Management → Providers**, which bills each request against the developer's own ChatGPT subscription instead. Barndoor stores no OpenAI credential for it.

A request to a Codex OAuth provider must carry two credentials:

* your ChatGPT OAuth token in `Authorization: Bearer …`, which the gateway forwards to OpenAI, and
* your Barndoor key in `x-api-key`, which the gateway uses to authenticate you.

The `env_key` setup above sends only your Barndoor key, so a request that lands on a Codex OAuth provider fails with `401 OpenAI provider requires an incoming Codex OAuth bearer token`. If your organization runs Codex on ChatGPT subscriptions, launch it with Barndoor Bridge (`barndoor run codex`, or **Launch Codex Desktop** in the Bridge app) instead of configuring it by hand. See [Launching Claude Code or Codex with Barndoor Bridge](https://docs.barndoor.ai/how-tos/connect-tools-to-llm-gateway.md#launching-claude-code-or-codex-with-barndoor-bridge).

> [!NOTE]
>
> Barndoor Bridge is in early access and may not be enabled for your organization yet. Contact Barndoor to turn it on.

## Smoke-test without Codex

Confirm the gateway accepts Responses traffic with your key before debugging Codex itself:

```bash theme={null}
curl https://app.barndoor.ai/api/llm-gateway/v1/responses \
  -H "Authorization: Bearer bd-…" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-mini",
    "input": "Reply with the single word pong."
  }'
```

A successful JSON response means the endpoint, key, and model name are fine — remaining issues are almost always Codex `base_url` / `wire_api` / env wiring.

## Troubleshooting

| Symptom | Likely cause | Fix |
| - | - | - |
| `401` / `invalid JWT header: InvalidToken` | `base_url` missing `/v1` | Use `…/api/llm-gateway/v1` |
| `401` / `invalid API key` | Wrong, unset, disabled, or revoked `BARNDOOR_API_KEY` | Confirm `env_key` matches the variable you exported. Check the key's status under [**API Keys**](https://app.barndoor.ai/settings/models?section=api-keys), and create a new one if needed |
| `404` / `model '…' not found` | `model` doesn't match a name enabled for your organization | Copy an exact name from [**Available Models**](https://app.barndoor.ai/settings/models?section=models) |
| Desktop ignores the key | GUI process lacks the env var | Launch Desktop from a shell/wrapper that exports `BARNDOOR_API_KEY` (Dock launches usually will not see `.zshrc` exports) |
| Works in `curl` but not in Codex | Wrong `wire_api` or provider block | Set `wire_api = "responses"` under `[model_providers.barndoor]` |
| `does not support endpoint /v1/responses` | The model is served by a provider without a Responses API (Anthropic, Bedrock, Vertex, Google AI) | Pick a model from an OpenAI-family provider. See [Which models Codex can use](#which-models-codex-can-use) |
| `401` / `requires an incoming Codex OAuth bearer token` | The model is served by an OpenAI Codex OAuth provider | See [ChatGPT subscriptions](#chatgpt-subscriptions-openai-codex-oauth-providers) |
| `403` | A model access policy doesn't allow you to use this model | Ask your admin, or pick another name from **Available Models** |

## Related

* [Using the LLM Gateway](https://docs.barndoor.ai/how-tos/use-llm-gateway.md) — overview and architecture
* [Connecting Your Tools to the LLM Gateway](https://docs.barndoor.ai/how-tos/connect-tools-to-llm-gateway.md) — keys, model names, supported endpoints, Cursor, Claude Code, SDKs
* [LLM Gateway Quickstart](https://docs.barndoor.ai/how-tos/llm-gateway-quickstart.md) — admin setup and request reference
* [LLM Controls](https://docs.barndoor.ai/how-tos/use-llm-controls.md) — budgets, rate limits, and model access


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.