OpenAI Codex CLI Custom Provider: Responses Config Guide

Configure OpenAI Codex CLI with a custom model_providers entry in config.toml, choose wire_api responses or chat, keep env_key off disk, and test patch loops.

Published 2026-10-03 · Updated 2026-10-03 · KeepRouter Editorial · 6 minute read

Protocol decision matrix for Responses API and Chat Completions integrations
Validate the request contract and the application outcome when moving an integration. Conceptual illustration.

Configure OpenAI Codex CLI to use KeepRouter by defining a named entry under model_providers in ~/.codex/config.toml, setting base_url = "https://keeprouter.com/v1", referencing your credential by variable name with env_key, and selecting the wire_api protocol required by your chosen catalog model. Always match wire_api to the model's documented tool contract before starting a multi-step repository edit.

Codex CLI is designed around terminal patch generation, file inspection and local test execution. That makes protocol selection far more visible than in a plain chat box: if the wire format does not carry structured function calls and reasoning items properly, the agent fails on its first file read.

Define a clean custom provider in config.toml

The official OpenAI Codex CLI repository documents config.toml provider blocks, env_key credential lookup and wire_api selection. Keep literal secrets out of config.toml so dotfiles remain safe to back up or review.

For Responses-first coding models such as gpt-6.1-sol, whose official model documentation requires POST /v1/responses for function calling, set wire_api = "responses":

model = "gpt-6.1-sol"
model_provider = "keeprouter"

[model_providers.keeprouter]
name = "KeepRouter"
base_url = "https://keeprouter.com/v1"
env_key = "KEEPROUTER_API_KEY"
wire_api = "responses"

Export KEEPROUTER_API_KEY in your shell session before launching codex:

export KEEPROUTER_API_KEY="kr_live_replace_with_your_scoped_key"
codex "Read package.json and list the test script name without editing any files."

Note that env_key takes the environment variable name ("KEEPROUTER_API_KEY"), never the secret value itself. If Codex reports a missing key on startup, verify printenv KEEPROUTER_API_KEY in the exact terminal pane where codex runs.

Choose wire_api by model contract, not guesswork

Catalog model typeRecommended wire_apiWhy the distinction matters
Responses-native coding models (for example gpt-6.1-sol)"responses"Preserves Responses tool calls, function_call_output items and reasoning state
Chat Completions models without a Responses route"chat"Sends POST /v1/chat/completions instead of failing on an unsupported Responses route
Anthropic Messages-only modelsUse Claude Code on /v1/messagesDo not force Messages-only models through an OpenAI wire format

Check each candidate's preferred endpoint in the live model catalog and review the Responses versus Chat Completions guide when switching between model families. Keep separate [model_providers.*] blocks or profile overrides if you test both "responses" and "chat" models so you never leave a mismatched protocol active.

Verify read, patch and approval boundaries in order

Keep Codex CLI's approval prompts enabled and work inside a clean Git repository with no production secrets in the directory tree. Run three progressive checks rather than starting with a broad refactor:

  1. Read-only inspection. Ask Codex to read one small source file and report an exact constant or function signature. Confirm in your terminal that a real tool call executed rather than a guessed answer.
  2. Scoped single-file patch. Ask for one deterministic edge-case fix and its unit test on a disposable branch. Inspect git diff before approving any shell command.
  3. Targeted test execution. Allow Codex to run only the single test file covering that change, then confirm the exit code yourself.
Failure modeFirst boundary to inspectAvoid
Immediate missing-key errorenv_key contains literal sk-... instead of env var nameWriting secrets directly into config.toml
HTTP 404 on first turnbase_url has duplicated /v1 or operation suffixChanging prompts before fixing base_url
Plain text works, first tool call failswire_api mismatch with the selected catalog modelDisabling sandbox approvals to bypass API errors
Incomplete turn on reasoning modelsOutput token cap exhausted by reasoning + patchRetrying identical tight limits in a loop

Reconcile full-loop spend across terminal sessions

Terminal coding agents resend repository context, tool outputs and test stderr across turns within a task. A five-step fix that compiles and passes on the first try can cost less in total than a cheaper per-token model that loops through four failed patches.

Track billed input, cached input and output tokens in the KeepRouter usage log per task, estimate candidate budgets with the API cost calculator, and compare editor-based workflows in the Cursor custom base URL guide or quickstart before standardizing a team configuration.

Frequently asked questions

Should env_key in config.toml contain my literal API key?

No. Set env_key to the name of an environment variable such as KEEPROUTER_API_KEY, and export the secret value in your shell environment.

When should wire_api be set to responses versus chat?

Use responses for models such as gpt-6.1-sol whose tool contract runs on POST /v1/responses, and use chat for routes documented on POST /v1/chat/completions.

What is a safe first verification command in Codex CLI?

Run a read-only prompt in a disposable Git repository that asks Codex to inspect one file and report a known line before testing file edits or shell commands.

Sources reviewed

Article last reviewed 2026-10-03

  1. [1] OpenAI Codex CLI repository and configuration
  2. [2] OpenAI · GPT-6.1 Sol

Related guides

← All posts · Models & pricing · Get an API key

Featured on Twelve Tools