Codex is a programming agent product from OpenAI. Its interface, configuration options, and supported capabilities may change across Codex versions. This guide is based on the official Kimi API calling method and describes a general integration approach: forwarding Codex model requests to the Kimi API through a compatibility layer.
When To Use This
If you want to use Kimi’s code models inside Codex, follow this guide to complete the setup. After integration, you still describe requirements, make code changes, run tests, and perform reviews inside Codex. Model requests are forwarded to the Kimi API through a local compatibility layer.Recommended Models
| Scenario | Recommended model |
|---|---|
| Complex coding, long-context work, multi-step agent tasks | kimi-k2.7-code |
| General coding tasks where output speed matters more | kimi-k2.7-code-highspeed |
kimi-k2.7-code and kimi-k2.7-code-highspeed belong to the same model family. Both support a 256K context window. The high-speed version is suitable for development scenarios that are more sensitive to response latency.
How The Integration Works
The Kimi API provides an OpenAI SDK-compatible Chat Completions API at:Prerequisites
Before you begin, make sure you have:- Installed Codex and confirmed that it works normally.
- Created an API key in Kimi Open Platform or Kimi Code.
- Prepared a local compatibility-layer tool that can forward Codex requests to an OpenAI Chat Completions-compatible backend.
- Confirmed that there are no high-risk automated tasks currently running in the project. Programming agents may call models and tools across multiple rounds, so set a budget first and keep monitoring the session.
Step 1: Verify That The Kimi API Works
Before connecting Codex, first confirm that your API key and model can be called successfully.Step 2: Configure A Local Compatibility Layer With CC Switch
CC Switch is a graphical local model-routing tool. It can provide protocol compatibility and request forwarding between Codex and the Kimi API. If you want a quick setup path, tools like this can be a convenient first choice. CC Switch is a third-party tool and is not part of the Kimi API. Its installer, UI labels, ports, and routing capabilities may change by version. Use the actual version you installed as the source of truth. On the Kimi side, make sure the API key, model name, and Kimi API address are configured correctly.2.1 Download And Install
Go to the CC Switch release page and choose the installer for your operating system. It usually provides builds for Windows, macOS, Linux, and other platforms. After installation, start CC Switch and confirm that the application opens normally.2.2 Automatically Configure The Kimi Provider
The automatic configuration feature in CC Switch modifies your existing Codex configuration file. Back up the user-level Codex configuration file before continuing:| Setting | Kimi | Kimi For Coding |
|---|---|---|
| API Base URL | https://api.moonshot.ai/v1 | https://api.kimi.ai/coding/v1 |
| API Key | MOONSHOT_API_KEY created in Kimi Open Platform | MOONSHOT_API_KEY created in Kimi Code |
| Model | kimi-k2.7-code or kimi-k2.7-code-highspeed | kimi-for-coding |
| Upstream API | OpenAI-compatible Chat Completions | OpenAI-compatible Chat Completions |
2.3 Enable Codex Routing
In CC Switch, enable the route or Responses API compatibility mode for Codex. After it is enabled, CC Switch starts a local service address such as:2.4 Confirm The Configuration In Codex
After completing the Codex configuration and restarting Codex, check the Codex settings page to confirm that the current provider or model configuration has taken effect.Optional: Manually Configure The Codex Provider
Open the user-level Codex configuration file:PORT with the actual port of your local compatibility layer.
env_key again in Codex.
After saving the configuration, restart Codex or open a new Codex session so the configuration takes effect.
Note: Codex provider configuration should be written to the user-level ~/.codex/config.toml. Project-level .codex/config.toml is not suitable for local machine settings such as provider authentication and provider selection.
Optional: Configure Another Local Compatibility Layer
For any local compatibility layer that supports model routing, complete the following setup:- Add or select a Moonshot/Kimi provider.
- Set the API Base URL to
https://api.moonshot.ai/v1. - Set the API key to the
MOONSHOT_API_KEYyou created in Kimi Open Platform. - Select
kimi-k2.7-codeorkimi-k2.7-code-highspeedas the model. - Enable a Responses API-compatible route for Codex.
- Record the address exposed to Codex by the local compatibility layer, such as
http://127.0.0.1:PORT/v1.
Step 3: Verify In Codex
After reopening Codex, use a small task to verify the full request path:- Codex requests the local compatibility-layer address.
- The local compatibility layer forwards requests to
https://api.moonshot.ai/v1/chat/completionsorhttps://api.kimi.ai/coding/v1/chat/completions. - The requested model is
kimi-k2.7-code,kimi-k2.7-code-highspeed, orkimi-for-coding. - Codex receives streaming output normally and can continue with later steps.
Troubleshooting
| Issue | Possible cause | Fix |
|---|---|---|
| Codex still uses the original model after startup | The configuration file did not take effect, or the settings were written to the project-level configuration | Confirm that the configuration is written to ~/.codex/config.toml, then restart Codex |
| Failed to connect to the local address | The local compatibility layer is not running, or the port is different | Start the compatibility layer and check the port in base_url |
| 401 or authentication error | The API key is missing, expired, or not forwarded correctly | Reconfigure the API key in the compatibility layer or in the MOONSHOT_API_KEY environment variable |
| Request format or streaming response error | Codex and the backend protocol do not match | Confirm that the compatibility layer has enabled a Responses API-compatible route |
| Model parameter error | Kimi K2.7 Code has fixed requirements for some sampling parameters | Do not force overrides for temperature, top_p, n, penalty parameters, or similar fields in the compatibility layer |
| Token usage grows quickly | The agent is making multi-round calls, retrying, or looping through tools | Set a daily project budget, monitor compatibility-layer logs, and interrupt the current Codex task when needed |
Usage Tips
- Prefer
kimi-k2.7-codefor complex code changes, cross-file understanding, and long-context tasks. - Try
kimi-k2.7-code-highspeedwhen faster response speed is more important. - Do not manually override sampling parameters for Kimi K2.7 Code. Keep the default configuration.
- For large repositories, ask Codex to perform read-only analysis first, then make changes and run tests in stages.
- When running Codex for a long time, keep monitoring logs, budget usage, and terminal behavior.