---
title: Codex CLI
description: Use Codex CLI with a Lovely Router API key: add it as a model provider in config.toml, pick a GPT model, and keep prompt caching working.
---

Codex CLI is OpenAI's coding agent for the terminal. It calls models over the OpenAI Responses API, which Lovely Router serves for GPT models, so you add Lovely Router as a model provider in `~/.codex/config.toml` and keep the rest of your setup. You need an [API key](/app/keys) first.

## Set up in one click

On the [API keys](/app/keys) page, use **Use in tools** → **Codex CLI**. The dialog shows the config below with your key filled in, and **Import with CC Switch** adds Lovely Router as a provider in the [CC Switch](https://github.com/farion1231/cc-switch) desktop app, with the models already set. CC Switch must be installed.

Right after you create a key, the dialog already contains it. For an existing key, paste the full key into the dialog: Lovely Router stores only a hash, so it can't show the key again.

## Add a model provider

Add Lovely Router as a model provider:

```toml title="~/.codex/config.toml"
model = "gpt-6.1-sol"
model_provider = "lovely"

[model_providers.lovely]
name = "Lovely Router"
base_url = "https://lovelyrouter.com/v1"
env_key = "LOVELY_API_KEY"
wire_api = "responses"
```

Codex reads the key from the variable named in `env_key`:

```bash title="Shell"
export LOVELY_API_KEY="lr-..."
codex
```

Current Codex versions no longer accept `wire_api = "chat"`; if you copied an older config, change it to `"responses"`.

## Choose a model

Use a GPT model (`gpt-*`): those are the models served over the Responses API. `model` in `config.toml` sets the default, and `codex -m <model>` starts a session with another one. The [model list](/models) has every id.

## Prompt caching

Codex sends a `prompt_cache_key` for each session, so every turn of a session stays on the channel that holds its prompt cache. See [Prompt caching](/docs/models/caching).

## Add data tools over MCP

The MCP server at `https://lovelyrouter.com/mcp` gives Codex the [data tools](/docs/tools) and the [browser agent](/docs/browser-agent), billed from the same balance. It authenticates with the same key:

```bash
codex mcp add lovely --url https://lovelyrouter.com/mcp --bearer-token-env-var LOVELY_API_KEY
```

This adds the server to `~/.codex/config.toml`. Codex reads the key from `LOVELY_API_KEY` when it starts, so the key stays out of the file:

```toml title="~/.codex/config.toml"
[mcp_servers.lovely]
url = "https://lovelyrouter.com/mcp"
bearer_token_env_var = "LOVELY_API_KEY"
```

Run `codex mcp list` to check it. For the full list of tools, see [MCP server](/docs/tools/mcp).

## Troubleshooting

| Symptom | Fix |
| --- | --- |
| `401` with `Missing API key` | Codex isn't sending the key. Export `LOVELY_API_KEY` in the shell that starts `codex`, and check that `env_key` names that variable. |
| `401` with `Invalid or revoked API key` | The key is mistyped, truncated or revoked. Paste it again, or create a new one. |
| `404` with `not served in responses format` | The Responses API serves GPT models only. Set `model` to a `gpt-*` id. |
| `404` with `Unknown endpoint /responses` | `base_url` is missing `/v1`. Set it to `https://lovelyrouter.com/v1`. |
| Codex rejects `wire_api = "chat"` | Change it to `wire_api = "responses"`. |
| `402` | Your balance is empty or the key reached its spend limit. See [Credits & billing](/docs/account/billing). |

Every other status is covered in [Errors](/docs/api/errors).
