# Codex CLI (/docs/integrations/codex)

> For AI agents, the [documentation index](https://bitrouter.ai/llms.txt) lists all pages. Append .md to a documentation URL to read its Markdown.

<IntegrationHeader id="codex" />

Codex talks to model providers over the OpenAI Responses API, which Spare serves at `/v1/responses`.
You add Spare as a provider in `~/.codex/config.toml`, then pick one of two setups:

* **[Run on DeepSeek V4.1 Flash](https://bitrouter.ai/docs/integrations/codex#run-on-deepseek-v41-flash).** Every request goes to Spare, at the
  live price.
* **[Keep your OpenAI model, add the Router](https://bitrouter.ai/docs/integrations/codex#keep-your-openai-model-add-the-router).** The CLI's
  [Cost routing](https://bitrouter.ai/docs/fleet/cost-routing) sends easy turns to Spare; the rest stay on your OpenAI model.

## Before you start \[#before-you-start]

* A Spare API key, exported in your shell profile:

  ```bash
  export SPARE_API_KEY="sk-spare-..."
  ```

* For the Router: the CLI, signed in to your OpenAI model with an OpenAI key or a
  ChatGPT Plus or Pro subscription.

Provider settings must go in your user config, `~/.codex/config.toml`. Codex ignores
`model_providers` in a project's `.codex/config.toml`.

## Run on DeepSeek V4.1 Flash \[#run-on-deepseek-v41-flash]

```toml title="~/.codex/config.toml"
model = "deepseek-v4.1-flash"
model_provider = "spare"
model_context_window = 1000000

[model_providers.spare]
name = "Spare Inference"
base_url = "https://api.spareinference.ai/v1"
env_key = "SPARE_API_KEY"
wire_api = "responses"
```

* `env_key` names the environment variable Codex reads your key from; the key itself stays out of the
  file.
* `model_context_window` tells Codex the model's context size, which it uses to decide when to
  compact history.

### Fallbacks \[#fallbacks]

If a provider fails, the request moves to the next provider serving DeepSeek V4.1 Flash on its own. To fall
back to your OpenAI model instead of failing, chain the two models in the CLI; see
[Fall back to a different model](https://bitrouter.ai/docs/fleet/fallbacks).

To run a [routing preset](https://bitrouter.ai/docs/fleet/provider-selection) instead of a fixed model, set `model = "@<name>"`.

## Keep your OpenAI model, add the Router \[#keep-your-openai-model-add-the-router]

Spare's API runs the model you name and doesn't route between models; the Router runs in the CLI on your machine. Set it up once, following [Cost routing](https://bitrouter.ai/docs/fleet/cost-routing#set-up), with your OpenAI model as
the strong route and `deepseek-v4.1-flash` as the economy route. Then start Codex through the CLI:

```bash
spare codex --model spare/auto
```

You don't need the `spare` provider block for this: the CLI talks to Spare and to OpenAI for you.

## Switch back and forth \[#switch-back-and-forth]

To keep your normal setup as the default, put the Spare settings in a profile file instead,
`~/.codex/spare.config.toml`, and start Codex with it when you want Spare:

```bash
codex --profile spare
```

## Check it works \[#check-it-works]

Run one prompt non-interactively:

```bash
codex exec "Reply with OK"
```

Then find the request on the console's **Activity** page, or send one directly to get a request id for
[settlement](https://bitrouter.ai/docs/api/settlement):

```bash
curl -si https://api.spareinference.ai/v1/responses \
  -H "Authorization: Bearer $SPARE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"deepseek-v4.1-flash","input":"Reply with OK"}' \
  | grep -i '^x-bitrouter-request-id'
```

## Good to know \[#good-to-know]

* **Spare's Responses API is stateless.** `previous_response_id` isn't supported, so each request
  carries its history.
* **OpenAI-hosted tools aren't available** on DeepSeek V4.1 Flash, like web search. Codex's local
  tools, shell commands and file edits, work as usual.
* **Reasoning effort** settings are accepted and ignored by DeepSeek V4.1 Flash.