Skip to main content
OpenAI’s Codex CLI is an AI coding assistant that runs in your terminal. By setting two environment variables, you can route Codex CLI requests through Flatkey to use compatible GPT, Claude, Gemini, and other text models when they are available through Flatkey.

Prerequisites

  • Codex CLI installed: npm install -g @openai/codex
  • A Flatkey account with an API key — get one here

Configuration

Set the following environment variables before running Codex CLI:
Or add them to your shell profile (~/.bashrc, ~/.zshrc) to make the setting permanent:
~/.zshrc

Configure with CC Switch

CC Switch is a third-party desktop configuration manager for coding assistants. It is not a Flatkey product, and Flatkey is not a built-in preset. The steps below follow the current CC Switch interface. The manual OPENAI_BASE_URL and OPENAI_API_KEY method above and CC Switch are alternative ways to configure Codex. For a clear CC Switch test, unset only stale OPENAI_BASE_URL or OPENAI_API_KEY values that point to another provider or key. Values inherited by your shell can override or conflict with the provider you activate in CC Switch.

1. Install CC Switch

Download CC Switch from ccswitch.io or its official GitHub repository. Install it for your operating system, then open the app.

2. Open the Codex provider panel

Select Codex in CC Switch. This opens the provider panel that controls the configuration used by new Codex sessions. Open the Codex provider panel and select the add-provider button in CC Switch

3. Add Flatkey as a custom provider

Choose Custom Provider and add a provider with these values: Enter the current Flatkey provider fields in CC Switch
Treat your API key as sensitive. Do not include it in screenshots or exported or shared provider configurations. If it is exposed, revoke or rotate the key in Flatkey, then replace it in CC Switch.
Flatkey natively supports the Responses API. CC Switch Local Routing is not required for this setup, so leave it off.

4. Configure the model

Find Flatkey in the Codex provider panel in CC Switch, then click Edit. To change the model used by new Codex sessions, set Default Model to an exact model ID from the Flatkey Model Directory. Do not use a display name or guess an ID. For example, you can enter any of these text-generation models in the CC Switch Default Model field:
To add or edit other models in CC Switch and show them in the Codex CLI /model menu, use Model Mapping:
  1. Click Fetch Models to load available models from Flatkey. If the model you need is missing, click Add Model.
  2. Edit the mapping’s Menu Display Name. This is the label shown in the /model menu.
  3. Set Actual Request Model to the exact ID from the model directory, such as claude-sonnet-4-6 or gemini-2.5-flash. Codex sends this ID in requests, not the menu label.
Model Mapping controls the /model menu entry and its corresponding request ID. It does not make an incompatible model work with Codex. After changing a mapping, follow the steps below to save the provider and restart Codex before the updated model list takes effect. After you save and activate Flatkey, new Codex sessions use the Default Model set in CC Switch. To override it for one command only, pass the same exact ID:
Choose a text-generation model that is available through Flatkey and compatible with the Codex request format. Do not use image, audio, or embedding models with Codex CLI. After sending a request, treat Flatkey Usage Logs as authoritative for the actual model served. Fetch or add other models and configure their Codex CLI model mappings

5. Save and activate Flatkey

Save the provider, then activate Flatkey in the Codex provider panel. Confirm that Flatkey is the active provider before you start Codex. Confirm that the Flatkey Codex provider is in use

6. Restart and verify

Exit the existing Codex session and end its process. Close the specific terminal window or tab that ran Codex, not unrelated terminals. Open a fresh terminal that does not inherit stale relevant OPENAI_BASE_URL or OPENAI_API_KEY values. Move to a small or empty directory and start a new Codex session. Enter /model, confirm that the model you added or edited appears, and select the model you want to use. Then send a minimal request. Verify a new Codex CLI session with a minimal OK response Then open Flatkey Usage Logs and confirm the actual request. Check its model, input and output tokens, latency, and cost.
An agent request can include system prompts, tool definitions, conversation history, files, and command results. Use a small context for connectivity checks so the token count and cost stay easy to inspect.

Switch back to another provider

Open the Codex provider panel in CC Switch, select the provider you want, and activate it. End existing Codex sessions and processes, then open a fresh terminal that does not inherit stale relevant OPENAI_BASE_URL or OPENAI_API_KEY values. Start a new session.

Troubleshooting CC Switch

Running Codex CLI

Once the environment variables are set, use Codex normally:
or in interactive mode:
All requests are routed through Flatkey and billed against your prepaid balance at Flatkey’s discounted rates.

Verify the manual environment-variable setup

This section applies only to the manual OPENAI_BASE_URL and OPENAI_API_KEY method. If you configured Codex with CC Switch, follow Restart and verify above instead. For the manual method, run a Codex command and check Usage Logs. You should see the request logged with the model and token counts. If no request appears, confirm both variables are set correctly in the current shell session.
Use the $200 top-up tier for the best effective rate on sustained Codex usage — the stacked bonus brings costs to as low as 50% of official GPT pricing.

Troubleshoot the manual environment-variable setup

The table below applies only to the manual OPENAI_BASE_URL and OPENAI_API_KEY method. If you configured Codex with CC Switch, use Troubleshooting CC Switch above.