Codex: Codex with DeepSeek

Last updated: 2026-08-31

DeepSeek is a leading large language model with excellent performance in code generation. This lesson covers how to use DeepSeek models within Codex.

📋 Prerequisites: Codex CLI installed, basic API Key configuration knowledge

1. What You Will Learn


2. Why Use DeepSeek

Advantage Description
Lower cost DeepSeek API pricing is significantly lower than OpenAI
Direct access No proxy needed for users in China
Strong coding DeepSeek Coder excels at code tasks
Better Chinese High-quality Chinese understanding and generation

(1) Price Comparison

Model Input Price Output Price
GPT-5 Codex $2.50/1M tokens $10.00/1M tokens
DeepSeek V3 ¥1.00/1M tokens ¥2.00/1M tokens
DeepSeek Coder ¥1.00/1M tokens ¥2.00/1M tokens

3. Official Integration Method

Codex CLI supports DeepSeek via the OpenAI-compatible API.

(1) Get a DeepSeek API Key

TEXT 📖 Display only
1. Visit platform.deepseek.com
2. Register / log in
3. Go to the API Keys page
4. Create a new API Key
5. Copy and save (shown only once)

(2) Configure Environment Variables

BASH
# macOS/Linux
export OPENAI_API_KEY="sk-your-deepseek-key"
export OPENAI_BASE_URL="https://api.deepseek.com"

# Windows PowerShell
$env:OPENAI_API_KEY="sk-your-deepseek-key"
$env:OPENAI_BASE_URL="https://api.deepseek.com"

# Permanent (add to ~/.zshrc or ~/.bashrc)
echo 'export OPENAI_API_KEY="sk-your-deepseek-key"' >> ~/.zshrc
echo 'export OPENAI_BASE_URL="https://api.deepseek.com"' >> ~/.zshrc
source ~/.zshrc

(3) Launch Codex

BASH
# Launch with DeepSeek model
codex --model deepseek-coder

# Or use DeepSeek V3
codex --model deepseek-chat

▶ Example 1: Alice Switches to DeepSeek

BASH
# Alice wants to save costs, switches to DeepSeek
export OPENAI_API_KEY="sk-deepseek-xxx"
export OPENAI_BASE_URL="https://api.deepseek.com"

cd my-project
codex --model deepseek-coder

# Alice enters:
> "Add OAuth2 support to the auth module"

# DeepSeek Coder executes the task normally

4. CCSwitch Tool

CCSwitch is a community-developed third-party tool for quickly switching between different model providers.

(1) Install CCSwitch

BASH
npm install -g ccswitch

(2) Configure Model Providers

BASH
# Add DeepSeek config
ccswitch add deepseek \
  --api-key "sk-your-deepseek-key" \
  --base-url "https://api.deepseek.com" \
  --model "deepseek-coder"

# Add OpenAI config
ccswitch add openai \
  --api-key "sk-your-openai-key" \
  --base-url "https://api.openai.com/v1" \
  --model "gpt-5-codex"

(3) Quick Switch

BASH
# Switch to DeepSeek
ccswitch use deepseek

# Switch to OpenAI
ccswitch use openai

# View current config
ccswitch current

▶ Example 2: Bob's Multi-Model Workflow

BASH
# Daily dev with DeepSeek (cost-saving)
ccswitch use deepseek
codex "Fix the CSS issue on the login page"

# Complex architecture tasks with OpenAI (more capable)
ccswitch use openai
codex "Design a microservice decomposition plan, generate architecture docs"

# Code review with DeepSeek Coder
ccswitch use deepseek
codex "Review the code quality of PR #15"

5. DeepSeek vs OpenAI Model Comparison

Dimension DeepSeek Coder GPT-5 Codex
Code generation Excellent Excellent
Complex reasoning Good Stronger
Chinese understanding Better Good
Cost Low High
Response speed Fast Fast
Multi-file refactoring Good Stronger

6. auth.json Configuration

You can also manage multiple API Keys via a configuration file:

JSON
{
  "OPENAI_API_KEY": "sk-your-deepseek-key",
  "OPENAI_BASE_URL": "https://api.deepseek.com",
  "model": "deepseek-coder"
}
BASH
# Write config
mkdir -p ~/.codex
cat > ~/.codex/auth.json << 'EOF'
{
  "OPENAI_API_KEY": "sk-deepseek-xxx",
  "OPENAI_BASE_URL": "https://api.deepseek.com"
}
EOF

7. Important Notes

Note Description
API compatibility DeepSeek API is OpenAI-format compatible, but some advanced features may not be supported
Model names Use deepseek-coder or deepseek-chat, not gpt-*
Base URL Must set OPENAI_BASE_URL to point to the DeepSeek endpoint
Switching back to OpenAI Remember to clear the OPENAI_BASE_URL environment variable
Quota DeepSeek offers free credits for new users — check your balance

❓ FAQ

Q Can DeepSeek fully replace OpenAI models?
A For daily development tasks, mostly yes. But for complex architectural reasoning and multi-file global refactoring, OpenAI models are still stronger. Choose based on task complexity.
Q Is CCSwitch safe?
A CCSwitch is an open-source community tool; API Keys are stored in local config files. Review the source code to confirm security.
Q Can I use both models simultaneously?
A Not within the same session. You need to end the current session, switch configuration, and restart.
Q Is DeepSeek's context window large enough?
A DeepSeek V3 supports 128K context, comparable to OpenAI models — sufficient for most projects.
Q Does Codex behave differently after switching models?
A Basic operations remain the same, but different models have different reasoning capabilities and output styles. DeepSeek has better Chinese understanding; OpenAI has stronger complex reasoning.

📖 Summary


📝 Exercises

  1. Basic (⭐): Configure the DeepSeek API and successfully use it in Codex CLI.
  2. Intermediate (⭐⭐): Configure two model providers with CCSwitch and compare their performance on the same task.
  3. Advanced (⭐⭐⭐): Design an auto-switching strategy — simple tasks use DeepSeek, complex tasks use OpenAI — and write a switching script.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏