Skip to content

Use Kimi K2 With Codex CLI: Full Setup

Configure Codex CLI to use Kimi K2 through Moonshot's OpenAI-compatible endpoint. config.toml provider, wire_api, model choice, pricing, and the common fixes.

MGMCSA Guru Team July 8, 2026 3 min read
Codex CLI configured to use Kimi K2 via Moonshot on Windows

Codex CLI runs fine on Kimi K2. Moonshot’s API is OpenAI-compatible — exactly what Codex expects — so you add it as a custom provider, set the wire protocol, and point Codex at a K2 model. Kimi’s strong reasoning and Codex’s speed make a capable pairing at pay-per-token rates well below the frontier providers.

This is the working config and the one detail that trips people up. For Codex’s general provider model, see Codex CLI custom provider setup.

Step 1: Codex CLI and a Moonshot key

Install Codex CLI per its docs, then sign up on the Moonshot platform and create an API key. On Windows, env vars are cleaner in WSL — see Codex CLI on Windows with WSL.

Step 2: Add Kimi to config.toml

Codex reads ~/.codex/config.toml. Add Moonshot via its OpenAI-compatible endpoint:

model = "kimi-k2-thinking"
model_provider = "moonshot"

[model_providers.moonshot]
name = "Moonshot Kimi"
base_url = "https://api.moonshot.ai/v1"
env_key = "MOONSHOT_API_KEY"
wire_api = "chat"

Export the key and run:

export MOONSHOT_API_KEY="sk-your-key"
codex

Confirm the base URL and model ID in Moonshot’s docs.

Step 3: Pick the model

Kimi K2 models in Codex

kimi-k2-thinking Reasoning mode; multi-step problems
kimi-k2 (turbo variants) Faster, cheaper everyday coding

Use Thinking for hard tasks and a faster variant for routine work. Confirm exact IDs on Moonshot.

Pricing

Kimi is pay-per-token with cache-hit discounts and no flat coding plan. Rates are low, and cache hits help during agent sessions that reuse context. Confirm current numbers on Moonshot’s docs — see Kimi K2 pricing explained.

Troubleshooting

  • Format/streaming errors — missing wire_api = "chat", or you used /anthropic.
  • Auth errorsMOONSHOT_API_KEY not set in the shell.
  • “Model not found” — confirm the Kimi model ID on Moonshot.
  • Changes ignored — restart Codex; config.toml loads at startup.

Kimi + Codex checklist

  • Codex CLI installed; Moonshot key created
  • [model_providers.moonshot] block with /v1 base_url
  • wire_api = "chat" set
  • model and model_provider point to Kimi
  • MOONSHOT_API_KEY exported; Codex restarted

Wrapping up

Using Kimi K2 with Codex CLI is a clean config.toml setup: a [model_providers.moonshot] block on the /v1 endpoint, wire_api = "chat", and a K2 model. Export the key, restart Codex, and you’ve got Kimi’s reasoning behind a fast agent at pay-per-token rates.

To run Kimi with other tools, see run Kimi K2 Thinking with Claude Code or Kimi K2.5 Turbo + OpenCode.

Frequently asked questions

Can Codex CLI use Kimi K2?

Yes. Moonshot's API is OpenAI-compatible, so you add it as a custom provider in Codex's config.toml with wire_api set to chat, then point the model at a Kimi K2 ID. It runs like any OpenAI-style provider.

What base URL does Codex use for Kimi?

Moonshot's OpenAI-compatible endpoint (commonly https://api.moonshot.ai/v1), not the Anthropic one. Codex speaks the OpenAI format, so use /v1. Confirm the path in Moonshot's docs.

Which Kimi model should I set?

kimi-k2-thinking for reasoning-heavy work, or a faster K2 variant for quicker responses. Put the exact ID from Moonshot in the model key and match model_provider to your Kimi block.

Why won't a base-URL-only change work?

Codex defaults to OpenAI's responses API. Moonshot uses chat completions, so you must set wire_api = "chat". Without it, requests fail even with the right URL and key.

How much does Kimi cost?

Kimi is pay-per-token with cache-hit discounts; there's no flat coding plan like GLM's. Rates are low. Confirm current numbers on Moonshot's docs since they change with versions.

Sources & further reading

Official vendor documentation referenced while writing this guide.

MG

MCSA Guru Team

IT & Systems Administration

We are working IT pros and system administrators who spend our days in Windows Server, Microsoft 365, and the wider Microsoft stack. MCSA Guru is where we write down the fixes and walkthroughs we wish we had found the first time.

MCSA Guru provides independent, educational IT guidance. Microsoft, Windows, Windows Server, Microsoft 365, Exchange, and Microsoft Teams are trademarks of Microsoft Corporation; Docker is a trademark of Docker, Inc. MCSA Guru is not affiliated with or endorsed by Microsoft or Docker. Always test changes in a safe environment before applying them in production.

Related guides

Fixing something right now?

Jump straight into the guide library or search for the exact error or task you are dealing with.