กลับไปหน้าสูตร
#codex#openai#cli#cheatsheet#ai#o1#o4-mini
Codex CLI Cheatsheet
คู่มือฉบับสมบูรณ์ Codex CLI (OpenAI): พลังแห่ง O1/O4-mini ใน Terminal การใช้งาน Sandbox, Image Input และ Automation Workflow
11 มีนาคม 2569อ่านประมาณ 4 นาที
สารบัญสูตร
🚀 Core CLI Commands (จาก Terminal)🛠 Interactive Slash Commands🛡 Approval & Sandbox Modes🖼 Image & Multi-modal Integration📄 Configuration Files📦 Official Install & Sign-in🧾 Official Quickstart Notes✅ Doc-backed Usage Additions🌟 จุดเด่นเฉพาะตัวของ Codex🤖 Subagents & Custom Agents🧩 Config, Skills, Hooks, MCP⌨️ Advanced Tips⚡ Automation Recipes (Real-world)📅 Daily Pro Workflow
🚀 Core CLI Commands (จาก Terminal)
| Command | การใช้งานเจาะลึก |
|---|---|
codex | เริ่มต้นใช้งาน Terminal UI (TUI) แบบเต็มตัว |
codex "prompt" | สั่งงานครั้งเดียว (Single action) แล้วเปิด Interactive mode |
codex resume --last | กลับไปคุยต่อในเซสชันล่าสุดที่เคยรันไว้ |
codex resume <ID> | โหลดเซสชันเฉพาะเจาะจงด้วย ID |
codex app | เปิด Desktop App (macOS) เพื่อใช้งานแบบ GUI ควบคู่กัน |
codex --image <path> | ส่งรูปภาพ (Screenshot/Design) ไปให้ AI วิเคราะห์โค้ด |
codex --version | ดูเวอร์ชันและข้อมูลระบบ |
🛠 Interactive Slash Commands
🧠 Model & Plan
| Command | การใช้งาน |
|---|---|
/model | สลับโมเดลระหว่าง o1, o4-mini, gpt-4o |
/plan | เข้าสู่ Plan Mode ให้ AI อธิบายขั้นตอนทั้งหมดก่อนลงมือทำ |
/permissions | ปรับระดับการขออนุญาต (Suggest, Auto Edit, Full Auto) |
/diff | ดูความแตกต่างของโค้ด (Git Diff) ที่ AI แก้ไขไปในเซสชันนี้ |
/review | ให้ AI ตรวจสอบโค้ดที่เพิ่งแก้ไขไป เพื่อหาบั๊กแฝง |
⚙️ Session & System
| Command | การใช้งาน |
|---|---|
/compact | บีบอัดประวัติการคุยเพื่อล้างหน่วยความจำ Token (ดีสำหรับเซสชันยาว) |
/mcp | จัดการการเชื่อมต่อกับเครื่องมือภายนอกผ่าน Model Context Protocol |
/clear | ล้างหน้าจอและรีเซ็ตประวัติการคุยเริ่มต้นใหม่ |
/exit หรือ /quit | ออกจากการทำงานของ Codex |
🛡 Approval & Sandbox Modes
หัวใจสำคัญของ Codex คือความปลอดภัยในการรันคำสั่ง:
1) Approval Modes (--approval-mode)
suggest(Default): AI จะต้องขออนุญาต (Y/N) ก่อนแก้ไขไฟล์หรือรัน shellauto-edit: แก้ไขไฟล์ได้เลยอัตโนมัติ แต่ต้องขออนุญาตก่อนรัน shellfull-auto: รันงานอัตโนมัติทั้งหมด (ต้องระวังมาก!)
2) Sandbox Policy (--sandbox)
read-only: AI อ่านไฟล์ได้อย่างเดียว ปลอดภัยสูงสุดworkspace-write: AI แก้ไขไฟล์ได้เฉพาะในโฟลเดอร์ปัจจุบันเท่านั้นdanger-full-access: ปลดล็อกการเข้าถึงไฟล์ระบบทั้งหมด (ใช้ในระบบปิดเท่านั้น)
🖼 Image & Multi-modal Integration
Codex รองรับการทำงานกับรูปภาพโดยตรง:
# ส่งภาพ UI Design แล้วให้สร้าง Tailwind CSS
codex --image ./design/mockup.png "ช่วยสร้าง Component ใน @src/components/ ตามรูปนี้"
# ส่งรูป Error จากหน้าจอแล้วให้ AI แก้ไข
codex --image ./logs/browser-error.png "วิเคราะห์สาเหตุของ error นี้ใน @src/app.js"
📄 Configuration Files
| File | บทบาท |
|---|---|
GEMINI.md | ใช้เก็บกฎเกณฑ์และมาตรฐานของโปรเจกต์ (Codex อ่านไฟล์นี้ด้วย) |
AGENTS.md | กำหนดพฤติกรรมเฉพาะของ Agent ในโปรเจกต์นั้นๆ |
📦 Official Install & Sign-in
| วิธี | คำสั่ง / วิธีใช้ |
|---|---|
| npm | npm install -g @openai/codex |
| Homebrew | brew install --cask codex |
| เริ่มใช้งาน | รัน codex แล้วเลือก Sign in with ChatGPT |
| Desktop experience | codex app |
จาก official repo ตอนนี้แนะนำให้ใช้ Codex ผ่านบัญชี ChatGPT plan ได้โดยตรง และถ้าจะใช้ API key ต้องไปตั้งค่าตาม auth docs เพิ่ม
🧾 Official Quickstart Notes
| ความสามารถ | รายละเอียดจาก official repo |
|---|---|
codex | เปิด Terminal agent หลัก |
codex app | เปิด Codex App สำหรับประสบการณ์แบบ desktop |
| Binary release | ดาวน์โหลดจาก GitHub Releases ได้โดยตรง |
| IDE usage | มี flow แยกสำหรับ VS Code / Cursor / Windsurf |
✅ Doc-backed Usage Additions
- Codex CLI เป็น lightweight coding agent ที่รัน local บนเครื่องเรา
- ใช้ได้ทั้งผ่าน ChatGPT sign-in และ API key setup
- มีเส้นทางใช้งานแยกสำหรับ terminal, IDE และ app
- official repo ระบุชัดว่ามี binary releases ให้ติดตั้งตรงได้โดยไม่ต้องผ่าน npm หรือ brew
🌟 จุดเด่นเฉพาะตัวของ Codex
- เด่นเรื่องการใช้
ChatGPT sign-inกับ coding agent ได้ตรงๆ โดยไม่จำเป็นต้องเริ่มจาก API key workflow - docs แยกประสบการณ์ใช้งานเป็น
CLI,App,IDE, และWebค่อนข้างชัด ทำให้ต่อ workflow ข้าม surfaces ได้ดี - custom agents ใช้ไฟล์
.tomlและมี schema ที่เน้น operation จริง เช่นdeveloper_instructions,sandbox_mode,model_reasoning_effort,nickname_candidates - มี subagent fan-out workflow และ
spawn_agents_on_csvสำหรับ batch jobs หลายแถว ซึ่งเป็น pattern ที่เด่นมากสำหรับงาน audit/review จำนวนมาก - ecosystem ของ Codex ผูกกับ
Hooks,Rules,Skills,MCP,AGENTS.md, และ security approvals ค่อนข้างชัด เหมาะกับงาน production ที่ต้องคุม sandbox กับ approval behavior มาก
🤖 Subagents & Custom Agents
Built-in Agents จาก docs
| Agent | ใช้ทำอะไร |
|---|---|
default | agent ทั่วไปสำหรับงานทั่วไป |
worker | เน้น implementation และ fixes |
explorer | เน้นอ่าน codebase และสำรวจข้อมูล |
Subagent Workflow
- Codex จะ spawn subagents เมื่อเรา สั่งอย่างชัดเจน
- เหมาะกับงาน review หลายมุม, codebase exploration, หรือ feature plan หลายส่วน
- ใช้
/agentใน CLI เพื่อสลับดู active agent threads
วิธีเรียก Subagent / Custom Agent
| วิธี | ตัวอย่าง |
|---|---|
| สั่งเป็นภาษาธรรมชาติ | Spawn one agent per point, wait for all of them, and summarize the result for each point. |
| ระบุชื่อ agent ที่อยากใช้ | Have pr_explorer map the affected code paths, reviewer find real risks, and docs_researcher verify the framework APIs. |
| สั่งให้ Codex แตกงานหลายมุม | Use explorer to inspect the codebase, then have worker implement the smallest safe fix. |
| ดูหรือสลับ thread ที่กำลังรัน | ใช้ /agent ใน CLI |
หลักสำคัญจาก docs:
- Codex จะไม่ spawn subagents เองมั่วๆ ถ้าเราไม่ได้ขอชัด
- ถ้าอยากใช้ custom agent ตัวไหน ให้เรียกชื่อมันใน prompt ตรงๆ
- เมื่อมีหลาย agent พร้อมกัน Codex จะรอผลทั้งหมดแล้วค่อยสรุปรวมให้
ตัวอย่าง prompt จาก docs:
I would like to review the following points on the current PR (this branch vs main).
Spawn one agent per point, wait for all of them, and summarize the result for each point.
Custom Agent Files
| Scope | Path |
|---|---|
| Personal | ~/.codex/agents/ |
| Project | .codex/agents/ |
ตัวอย่าง .toml:
name = "reviewer"
description = "PR reviewer focused on correctness, security, and missing tests."
developer_instructions = """
Review code like an owner.
Prioritize correctness, security, behavior regressions, and missing test coverage.
"""
nickname_candidates = ["Atlas", "Delta", "Echo"]
Fields สำคัญ
| Field | ความหมาย |
|---|---|
name | ชื่อ agent |
description | คำอธิบายว่า agent นี้ควรถูกใช้เมื่อไร |
developer_instructions | core instructions ของ agent |
nickname_candidates | ชื่อเล่นสำหรับ UI |
model | model override |
model_reasoning_effort | reasoning effort |
sandbox_mode | เช่น read-only, workspace-write |
mcp_servers | MCP เฉพาะ agent |
skills.config | skills ที่ผูกกับ agent |
Global Agent Settings
| Key | ใช้ทำอะไร |
|---|---|
agents.max_threads | จำนวน agent threads พร้อมกัน |
agents.max_depth | ความลึกของ nested spawning |
agents.job_max_runtime_seconds | timeout ต่อ worker |
🧩 Config, Skills, Hooks, MCP
จาก docs navigation ของ Codex ตอนนี้มีหัวข้อ config/customization ชัดเจน:
AGENTS.mdสำหรับคำสั่งและกติกาในโปรเจกต์Hooksสำหรับ workflow automationMCPสำหรับต่อ external tools/dataSkillsสำหรับ reusable capabilityRulesและ config file สำหรับกำหนด behavior เพิ่ม
ถ้าจะออกแบบ workflow จริงใน Codex ปกติมักจับคู่กันแบบนี้:
- custom agent สำหรับ persona + sandbox + model
- skills สำหรับ reusable expertise
- MCP สำหรับเครื่องมือภายนอก
- hooks/rules สำหรับ guardrail และ automation
⌨️ Advanced Tips
- Reference Scope: ใช้
@src/เพื่อระบุขอบเขตไฟล์ที่ต้องการให้ AI ดู - Shell Commands: Codex สามารถรัน
!npm testหรือ!git statusได้โดยตรง - Multi-line Input: ใช้
Alt + Enterเพื่อพิมพ์ Prompt ยาวๆ หลายบรรทัด - Session ID: ทุกเซสชันจะมี ID เฉพาะตัว สามารถกู้คืนได้ทุกเมื่อ
⚡ Automation Recipes (Real-world)
1) UI Transformation (Image to Code)
codex --image ./figma-export.png "เปลี่ยนรูปนี้เป็น React Component โดยใช้ shadcn/ui"
2) Debugging with Context
codex "รัน !npm test ดู @test-results/ พังตรงไหนแล้วแก้ใน @src/ ให้หน่อย"
3) Bulk Refactoring (Auto Mode)
codex --approval-mode auto-edit "ย้าย Logic ใน @src/pages/ ไปไว้ใน @src/hooks/ ทั้งหมด"
📅 Daily Pro Workflow
- Morning Prep:
codex resume --lastเพื่อเช็กงานที่ค้างไว้ - Strategy: ใช้
/planวางแผนงานใหญ่ประจำวัน - Develop: เขียนโค้ดโดยใช้
@ระบุไฟล์ และให้ AI รันเทสให้ - Safety Check: ใช้
/diffตรวจสอบความเรียบร้อยก่อน commit - Git Commit: สั่ง
!git add . && !git commit -m "..."ผ่าน Codex ได้เลย