How to Connect AI Coding CLI Tools Directly to Models That Support the Responses API
This article explains why AI coding CLI tools could not simply switch to most model services by changing the base URL, and why the Responses API matters for direct connections. It covers the benefits of keeping the same tool while switching models, the prerequisites, two setup paths (a one-click script and manual configuration in config.toml), and fixes for common errors.
Background: Why Changing the Base URL Did Not Work
For a long time, AI coding CLI tools (hereafter “coding CLIs”) faced a protocol gap when connecting to mainstream large model services. Coding CLIs use the OpenAI-style Responses API, a stateful, conversational interface in which the server maintains context and supports structured round trips for tool calls. At the time, many model services from Chinese providers exposed the Chat Completions interface instead, a stateless, single-turn request-response pattern.
The two differ in more than the URL:
- Request structure: The Responses API takes an items list, where messages, tool call results, and function outputs each exist as separate items. Chat Completions uses a simple messages array.
- State management: The Responses API supports server-side session continuation through
previous_response_id. With Chat Completions, the client must resend the full history on every turn. - Tool call semantics: Coding CLIs rely heavily on tool calls, such as reading files, editing files, and running commands. The Responses API structures these round trips more rigorously.
A protocol mismatch means that changing only the base_url will fail. To work around this, the community commonly built a local gateway that translates protocols: it converts Responses requests into Chat Completions requests, then converts the responses back. These gateways work, but they have clear costs. Streaming conversion can drop fields, tool call JSON structures can become distorted during translation, latency and token usage grow in long sessions, and the setup adds a local process that must be maintained.
The shift came when model services began supporting the Responses API natively. Once a model service supports it, a coding CLI can connect directly, and the gateway layer is no longer needed. The rest of this article assumes a direct connection.
Why It Is Worth Switching: The Shell Stays the Same, the Brain Changes
After you connect a new model service, the coding CLI works almost the same way. It still parses project structure, runs the task loop, calls tools, edits files, executes commands, and displays diffs. What changes is the model behind it, which interprets requirements, designs solutions, and generates code.
This “shell and brain” separation brings several practical benefits:
- Existing tooling carries over. Your AGENTS-style project instruction files, custom prompts, approval policies, and sandbox settings all continue to apply, so migration cost is close to zero.
- Models are easy to switch. Different tasks need different models. Refactoring and architecture design suit models with strong reasoning, while small batch edits suit faster, cheaper models. Once the protocol is connected, switching only requires changing one configuration item.
- Costs are clearer. When you use a third-party model service, usage is billed against that provider’s API balance. Subscription quotas for coding tools cannot cover this usage, because the two billing systems are separate. Before you adopt this setup, run a small task first, observe the token usage of a single task, and then estimate your daily budget.
Another detail is easy to miss. Coding CLIs, desktop apps, and IDE plugins typically share one configuration file, usually config.toml in the ~/.codex/ directory. You configure it once, and it takes effect across all clients, so you do not have to enter settings in each client separately. For developers who move between the terminal, the editor, and the desktop app, the real setup effort is much lower than it first appears.
Choosing a Model
Model services typically offer three types of models. Choose based on the task:
- General-purpose models: Suited to most coding tasks, with a balance of capability and speed.
- Reasoning-enhanced models: Suited to complex refactoring, hard-to-find bugs, and architecture design, where deep thinking is needed. Their reasoning process consumes additional tokens.
- Multimodal or vision models: Accept image input. They suit tasks like “write a page from this design mockup” or “check a UI issue from a screenshot.”
A common pitfall: model names from older tutorials are often deprecated or no longer maintained. Always use the current model IDs listed in the model service’s official documentation. Do not copy model names from outdated articles, or you will get “model not found” errors.
Prerequisites (Common to Both Paths)
Before you start, confirm three things.
First, the coding CLI is installed and up to date. Direct connection to the Responses API is a relatively new capability, and older client versions may not recognize the new configuration fields. Run codex --version to check your version, and upgrade to the latest stable release:
npm i -g @openai/codex# or on macOSbrew install openai-codex
Second, the configuration directory exists. Running codex once creates the ~/.codex directory automatically. You can also create it manually with mkdir ~/.codex. All later configuration goes in this directory.
Third, you have an API key for the target model service. Create a key in the provider’s developer console, usually starting with sk-. Store it securely and never commit it to a code repository. Also confirm that your account has funds or available quota. As noted above, this usage is billed by the model service itself.
Option 1: One-Click Script
If the model service provides an official setup script, this is the fastest path. The script does three things:
- Checks that the local CLI version supports direct connection.
- Writes a provider section to
~/.codex/config.toml, including thebase_urlfor the model service’s Responses API endpoint, the model name, and the name of the authentication environment variable. - Prompts you to store your API key in an environment variable, such as a variable in the style of
DEEPSEEK_API_KEY. Follow the script’s documentation for the exact name.
Notes on using the script:
- Read it before running it. The script modifies the global configuration file. Review its contents first, or back up your existing
config.toml. - Do not run it more than once. Repeated runs can create duplicate configuration sections, which causes parsing conflicts.
- Verify the setup afterward. Run a minimal task, such as “explain the current directory structure,” to confirm that the connection works.
Option 2: Manual Configuration
Manual configuration helps you understand what each field does, and it makes later troubleshooting easier. The core step is adding a model provider section to ~/.codex/config.toml:
[model_providers.my_provider]name = "My Provider"base_url = "https://<model-service-Responses-endpoint>"wire_api = "responses"env_key = "MY_PROVIDER_API_KEY"[profiles.my_provider]model = "<current-model-ID>"model_provider = "my_provider"
Key points:
wire_api = "responses"is required for direct connection. This field tells the CLI to communicate with the endpoint using the Responses protocol. If you set it to"chat", the CLI falls back to Chat Completions mode. For direct connection, always useresponses.base_urlmust point to the correct endpoint root. The Responses endpoint and the Chat endpoint of the same service have different paths, so check which one you are using.env_keynames the environment variable that holds the key. This keeps the plaintext key out of the configuration file:
# Add to your shell configuration file (~/.zshrc or ~/.bashrc)export MY_PROVIDER_API_KEY="sk-your-key"
- Specify the profile at startup with
codex --profile my_provider, or set it as the default in the configuration. You can also use the-mflag to specify a model for a single session.
After you finish the configuration, run a simple task to verify the setup. If output streams normally and tool calls work (for example, reading a file), the protocol connection is working.
Troubleshooting Common Issues
- 401 authentication failure: The environment variable is not loaded (open a new terminal or run
sourceagain), the key is wrong, or theenv_keyname does not match the actual variable name. - 404 or “model not found”: The model ID is outdated or misspelled. Check the current model names in the official documentation.
- No response from tool calls or malformed tool output:
base_urlpoints to the Chat endpoint instead of the Responses endpoint, orwire_apiis not set toresponses. - Interrupted streaming: Check your local network and proxy settings. On corporate networks, confirm that the Responses endpoint is allowed by the firewall or allowlist.
- Higher-than-expected costs: Tokens used for reasoning in reasoning-enhanced models count toward usage. Review the usage breakdown in the provider’s console, and consider switching to a lighter model for simple tasks.
Summary
Once a model service natively supports the Responses API, connecting a coding CLI to a third-party model changes from a task that requires a local gateway into a routine change of a few lines of configuration. Understanding the protocol difference between the Responses API and Chat Completions helps you tell whether a problem is in the configuration or the connection. Because the configuration file is shared across clients, one setup covers the terminal, the desktop app, and the IDE. The safest rollout starts with small tasks to observe costs, then gradually moves daily coding tasks to the new model.
